SOLID Programming Principles
Acronym describing clean coding principles for OOP languages, coined by Uncle Bob Martin.
Definition
- S - Single Responsibility
- A class and/or method should have a single responsibility, and shouldn’t do more than 1 thing
- O - Open/Closed
- The architecture should be “Open” for enhancement through adding additional code, but “Closed” to modifying the existing code
- Realistically, it’s encouragement to do your best to design the solution in such a way that through inheritance or composition you can leave your code open to further evolution without having to modify existing code
- L - Liskov Substitution
- When a class inherits from another class, the overall program shouldn’t break; and it shouldn’t require hacky conditionals to work
- Rather than passing additional arguments to methods or classes, can additional subclasses, interfaces, or updating constructors help?
- I - Interface Segregation
- Make interfaces or parent classes more specific, rather than generic
- Code smell for this one is if your abstract class or interface starts to have a bunch of methods that account for odd edge cases, the question should be: “Should I break this out into a couple of interfaces / classes, instead of one monster one?”
- D - Dependency Inversion
- Classes should depend on abstractions, not on concrete classes
- E.g., A class depends on (meaning it has it passed in as a constructor parameter or something similar) a specific concrete class, but instead should depend on an interface or abstract class
- Classes should depend on abstractions, not on concrete classes