SOLID is an acronym for 5 object-oriented design principles formulated by Robert Martin - @unclebobmartin. Following SOLID principles will help you build maintainable and extensible software.
So, what makes you SOLID?
S - Single-responsibility principle O - Open-closed principle L - Liskov substitution principle I - Interface segregation principle D - Dependency inversion principle
The Single Responsibility Principle (SRP)
A class should have one and only one reason to change, meaning that a class should have only one job.
You can apply this rule more broadly. Functions, variables, modules. Follow the SRP and don’t try to create a Swiss Army knife everywhere you look.
The Open-closed principle (OCP)
Objects or entities should be open for extension but closed for modification.
At first, it may seem that there’s nothing wrong with modification. Everything changes as they once sang.
The modification issue is that if you have some function that does multiple things, and you need to modify one of these, you might break other clients. And even if you’re so smart and hardworking that you don’t mind rewriting them all, others may be.
So, follow the OCP and add extra behavior on top of the existing one, not by modifying the original. If you failed at SRP, it’d be pretty hard to keep up with OCP.
The Liskov Substitution Principle (LSP)
Instances of a superclass should be replaceable with the instances of its subclasses.
In plain English, LSP means that if you have some
Dog which can
bark(), it’s not very wise to build MechanicalDog that extends Dog and can only
bark() if provided with batteries and would otherwise throw an Error.
Interface Segregation Principle (ISP)
No client should be forced to depend on methods it does not use.
You can consider it an SRP for the interface design.
Declutter and make sure everything is in the right place.
Dependency inversion principle (DIP)
Everything should depend on abstractions (interfaces).
The interfaces should never depend on details.
Seems obvious? But look closer and you’ll see violations of DIP literally everywhere.
Apply the SOLID principles today and you’ll become a much better developer tomorrow!
P. S. Oh, and don’t forget the Agile approach.
Make it work → Make it clever → Make it fast
Nobody will care about how good your code is if it doesn’t solve a problem.