SOLID is an acronym for five design principles that make code easier to understand, change, and extend. Think of them as the "house rules" of object-oriented programming:
S Single Responsibility โ one class, one job
O Open/Closed โ open for adding features, closed for breaking old ones
L Liskov Substitution โ a child class must work anywhere the parent works
I Interface Segregation โ don't force classes to implement methods they don't need
D Dependency Inversion โ depend on abstractions, not concrete details
A class should do one thing and do it well. A class that both builds a pizza and prints the receipt has two reasons to change โ and that's a problem.
When you split responsibilities, each class becomes smaller, easier to test, and easier to reuse. If the receipt format changes, the pizza class doesn't need to know.
Classes should be open for extension but closed for modification. That means you should be able to add new features without editing existing, working code.
For example, instead of editing a giant calculateArea() method every time you add a new shape, give each shape its own class with its own area() method. New shape? New class. Old code untouched.
If class B inherits from class A, then you should be able to use a B anywhere you use an A โ without anything breaking.
The classic trap: a Square inheriting from Rectangle. A square is a rectangle mathematically, but if you set width to 5 and height to 10, a square would break the rectangle's rules. Wrong inheritance!
Don't force a class to implement methods it will never use. If you have an interface Worker with methods work(), eat(), and sleep(), a Robot class shouldn't be forced to implement eat()!
Split big interfaces into small, focused ones: Workable, Feedable, Restable. Each class implements only what it actually needs.
High-level code shouldn't depend directly on low-level code. Both should depend on abstractions (interfaces or abstract classes). This makes it easy to swap out one implementation for another.
For example, a NotificationService shouldn't hard-code EmailSender. It should depend on an interface Sender so you can plug in SmsSender or PushSender later.
Cohesion is how strongly related the things inside a class are. High cohesion = a class does one thing really well. Coupling is how much one class depends on another. Low coupling = classes can change independently.
The goal: high cohesion, low coupling. Classes should be focused and loosely connected โ like LEGO bricks rather than tangled spaghetti.
Inheritance is powerful, but deep inheritance trees get messy fast. Composition means building classes by containing other classes instead of inheriting from them.
For example, instead of Bird extends Animal and Penguin extends Bird, give each bird a FlyBehaviour and SwimBehaviour object. Penguins get "can't fly" + "can swim." Ducks get "can fly" + "can swim." Much more flexible!
A code smell is a warning sign that something in the design is off โ even if the code runs. Examples: giant classes, duplicated code, long parameter lists, and deeply nested if statements.
Refactoring means improving the code's structure without changing what it does. It's like cleaning a messy room: everything still works, but now you can find things. Refactoring techniques include "extract class," "move method," and "replace conditional with polymorphism."
Split the job: Take a "Swiss army knife" class that does three things (e.g., a User class that saves to a database, sends emails, and formats names). Split it into three focused classes using the Single Responsibility principle.
Shape shifter: Write a Shape class with a method area(). Create Circle, Rectangle, and Triangle subclasses. Now add a Hexagon โ do it without changing any existing class (Open/Closed!).
Impostor check: Create a Rectangle and a Square that inherits from it. Try to set different width and height on a square. Explain why it breaks Liskov Substitution.
Interface splitter: Design a Worker interface with too many methods. Split it into two or three smaller interfaces. Then create a Robot class that implements only the ones it actually uses.
Swap the sender: Create a Notifier class that depends on an interface Sender. Implement EmailSender and SmsSender. Swap them without changing Notifier โ that's Dependency Inversion in action!
Smell hunt: Find (or write) a small class with at least two code smells. Refactor it using at least one technique (extract method, extract class, or rename). Write one sentence about what you improved.
By the end of this module, you'll be able to: explain all five SOLID principles, spot code smells in your own projects, choose composition over inheritance, and refactor messy code into clean, flexible, easy-to-change designs.