How to make a line smaller without touching it?
This article is about OCP of the S.O.L.I.D.
SOLID is mnemonic acronym for five design principles Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle and the last one is Dependency Inversion Principle.
OCP stands for Open–closed principle.
Open-closed is invert terms and it isn't self explainery too, the name open closed principle is bit confusing until we know the exact meaning of it.
Sir Bertrand Meyer is generally credited for having originated the term open/closed principle which appeared in his 1988 book Object Oriented Software Construction.
Lets see what Wikipedia knows about it:
"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
Practically there isn't any order defined for applying open closed principle but generally it comes after the single responsibility principle, assuming that you have at least something on which you would apply the open closed principle. (if you are not aware about single responsibility principle please refer my previous blog)
Knowledge Check
Before we deep dive into the concept of the open closed principle lets discuss the difference between term extension and modification.
Extension : A part that is added to something to enlarge or prolong it.
Modification : The action of modifying something.
"Extension means adding something to the existing and modification means changing the existing." -we
You might be thinking! If we want to extend something then we need to modify it as well. how we would be able to extend something without changing it? Enigma! At some extent, I am agree with your argument but not completely agree with it, Because, I know that it is completely possible to extend something without modifying it.
Lets go for a walk
First lets go through the case where extension needs modification, to fill a half filled glass with water, we need to pour more water in the glass until it get filled completely, Here pouring additional water into glass to make it complete filled isn't really called extension but its straight forward example of the modification.
Lets say, we created student class with first name and last name properties and after some time we received new requirement to add birth date, because there is new requirement to capture the age of the student as well.
The requirement is genuine and its valid to be added birth date field in the student class itself only if the class is not consumed by any other client (classes, modules or client applications), It is wise decision to add new property there in the student class (by modifying it to extend it) because it will not lead ripple effect of changes.
But what if the student class is being used by client applications or other classes? modifying the existing student class can damage the client functionality, or will cause ripple effects for client to update codes which they don't want and like to, such changes are also known as destructive changes.
"The change which cause existing client or consumer to update their implementation is harmful for the reputation and its an example of poor software design. no matter the consumer is your peer developer or third party API user."
Alternatively, the same functionality can be achieved using inheritance by developing another class may be named Student2 which extend the base Student class so that Student2 will have additional birth date property in it along with base properties and consumer of the student class does not required to change their implementations. win win. !!!
Universal Truth
No one can guarantee that requirement we receive would be always fully defined! and there won't be any further change (with labeling enhancement keyword) in it, Thus, it is wise decision to build classes or modules keeping the extensibility in mind, such a way that, it can be extended whenever needed without changing it.
Remember: we can always make line bigger or smaller without changing it.
Feel free to share your thoughts regarding your way of thinking about the open closed principle.
Happy Learning
Thanks