You definitely know the SOLID acronym. It stands for five principles of object-oriented programming that, if followed, are supposed to make your code both legible and extensible. They were introduced almost 30 years ago, but have they really made us better programmers in the time since? Do we really understand OOP better thanks to them? Do we write more âlegible and extensibleâ code? I donât think so.
Letâs go one by one and see how they âhelp.â
S
The âSâ refers to the Single Responsibility Principle, which, according to Clean Code by Robert Martin, means that âa class should have only one reason to change.â
This statement sounds extremely vague to me, but the book explains it, stating that objects must be problem-centered and responsible for âone thing.â Itâs up to us to decide what that one thing is, of course.
This is what we know as âhigh cohesionâ since Larry Constantine wrote about it in the IBM Systems Journal in 1974. Why was it necessary to create a new principle 15 years later with an ambiguous name and a very questionable definition?
O
This letter is about the Open/Close Principle, which was introduced by Bertrand Meyer in Object Oriented Software Construction in 1988. Simply put, it means that an object should not be modifiable. I canât agree more with this.
But then it says it should be extendable, literally through implementation inheritance, which is known as an anti-OOP technology. Thus, this principle is not really applicable to objects and OOP. It may work with modules and services, but not with objects.
L
The third letter is for the Liskov Substitution Principle, which was introduced by Barbara Liskov in 1987. This one is the most innocent part in the SOLID pentad. In simple words, it states that if your method expects a Collection, an ArrayList will work.
It is also known as subtyping and is the foundational component of any object-oriented language. Why do we need to call it a principle and âfollowâ it? Is it at all possible to create any object-oriented software without subtyping? If this one is a principle, letâs add âvariablesâ and âmethod callingâ here too.
Honestly, I suspect that this principle was added to SOLID mostly in order to somehow fill the gap between âSOâ and âID.â
I and D
I guess they both were introduced by Robert Martin while he was working at Xerox.
The Interface Segregation Principle states that you must not declare List x if you only need Collection x or even Iterable x. I canât agree more. Letâs see the next one.
The Dependency Inversion Principle means that instead of ArrayList x, you must declare List x and let the provider of the object decide whether it is ArrayList or LinkedList. This one also sounds reasonable to me.
However, how is all this different from the good old âloose couplingâ introduced together with cohesion by Constantine in 1974? Do we really need to simplify and blur in order to learn better? No, not to learn better, but to sell better. Here goes my point.
My point isâŚ
The point being these principles are nothing but an explanation of âcohesion and couplingâ for dummies in a very primitive, ambiguous, and marketable way. Dummies will buy books, seminars, and trainings, but wonât really be able to understand the logic behind them. Do they really need to? They are just monkeys coders, right?
âBut an object must be responsible for one thing!â is what I often hear at conferences. People learn that mantra without even knowing what cohesion is nor understanding what this âone thingâ they are praying for really is. There is no such thing as âone thing,â guys! There are different levels of cohesion.
Who is guilty? Uncle Bob & Co.
They are no better than Ridley Scott and other Hollywood money makers who deliver primitive and easy-to-cry-at movies just to generate a profit. People are getting dumber by watchingâbut this is not of their concern. The same happens with magic OOP principlesâprogrammers rely on them, thinking the truth is right there while the real truth is not understood even by the creators of this âmagic.â
SOLID is a money-making instrument, not an instrument to make code better.
