In this week’s post for my blog about the crazy journey of coding, I will be talking about the SOLID principles. The reason I am selecting this topic is because it sets the tone on how to produce quality code. Many times, especially for most students like me, we rush to finish up any coding project we have and don’t live up to the potential that could have been put forth. Therefore, SOLID principle help give a guideline to follow for almost any programmer to outcome the best work.
Well S.O.L.I.D is actually an acronym that stand for five important principle of object-oriented programming by Robert C. Martin, who is commonly known by as Uncle Bob. Now if you have read my first blog you would know that I went to a lot more details on object-oriented programming and I would suggest everyone read up on that before reading this post. The essential purpose of the principles to create software that is easy to maintain and add onto in the future. These principles also help detect any minor problem in the code that could lead to bigger problems and help change around code any time on the code journey. Now let’s get into what these letters actually represent.
The S in the S.O.L.I.D principle stands for Single Responsibility Principle and it states that a class should have one and only one reason to change, essentially meaning a class should only have on job. This concept should be fairly easy to understand as one class should be responsible only for what it represents and all the methods in the class should correspond to that. The second acronym O stands for Open-Closed Principle and it states that objects or software entities should be opened for extension, but closed for modification. What this essentially means is that classes should be able to extend its behavior, but not be modified. This can be done in various different ways such as through inheritance or an interface, but the main guideline is not to modify.
The L in the S.O.L.I.D principle stands for Liskov Substitution Principle and it states that objects of a superclass can be replaced with objects of its subclasses without breaking the software application. What this simplifies too is that the objects of the subclass need to behave in the same way as objects of the superclass. The next acronym I stands for the Interface Segregation Principle and it states that no client should be forced to implement an interface that it doesn’t use or clients shouldn’t depend on methods they don’t use. These interfaces are difficult to maintain and change over time so it is best to avoid. The last acronym in the S.O.L.I.D principles stands for Dependency Inversion Principle and it states that software entities must depend on abstractions not on concretions. This is due to the fact that abstractions change less and so make it easier to change behavior.
I think personally these principles are all key to establishing good solid code. In the future, I expect myself to catch myself when I am coding and follow these principles. With this outlook, I will be able to keep a clear focus from start to finish of a project, and won’t compromise one thing for another. The principles create a clear road to follow and I have benefitted from learning about it.
To read more up on the S.O.L.I.D principles, here are some sources to check out which I have used for research:
https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design
From the blog CS@Worcester – Roller Coaster Coding Journey by fbaig34 and used with permission of the author. All other rights reserved by the author.