You must be thinking “Great, another schmuck who tries to explain them, the internet doesn’t need another series dedicated to SOLID!” You are right, this topic has been explored many times before, and certainly you will find better explanations out there. Every year thousands of new students enter the workforce in our wonderful industry. Even if there were courses in their University, or made the research themselves, only after they build up more experience can they truly cherish these simple, and in the same time complex principles.
To explain it better, let me use some chess analogy. I love this game and you will see if you follow my writing that I draw great inspiration from it. It heavily influenced the way I work and I apply a lot of habits, which I learned while playing. Try to guess who is better in this position.
If you know how the pieces move, you could probably guess very easily that White is better (according to Stockfish 10, with +8.7 if white to move, and by +7.6 if black to move, a crushing defeat for Black). In chess in the middle game, we try to develop a strong position and if you don’t follow a specific opening strategy it is good to keep in mind the following principles.
- Castling – the King should be safely hidden and the rooks should connect to protect each other.
- Occupy the important squares – in general the more squares your pieces hit, the better is your position (you might have heard the saying “Knights on the rim are dim” or “Rooks should be on open files”)
- Center domination – in the center of the board every piece maximizes the number of squares they hit, and you can easily react if your opponent shifts his forces
- Avoid having double pawn – double pawns are hard to defend and can be a decisive structural weakness.
- Pieces should protect each other – the least good attacking options your opponent has the better is your position.
Very much like in chess, object oriented design has some similar principles that you can apply to improve the quality of your code. It’s hard to define quality but these principles help you reach a more understandable and maintainable code.
The SOLID principles were introduced by Robert C. Martin (“Uncle Bob”) in his paper called “Design Principles and Design Patterns”, but let’s not forget to thank Barbara Liskov and Bertrand Beyer too. The SOLID principles as an acronym stands for the following principles
- Single Responsibility Principle (SRP)
- Open/Closed Principle (OCP)
- Liskov Substitution Principle (LSP)
- Interface Segregation Principle (ISP)
- Dependency Inversion Principles (DIP)
They helped me to become a better and a more respected software developer. To honor each principle, I will write a dedicated post for each, with examples in order to discuss them in better detail. They helped me to become a better and a more respected software developer. See you soon!