Why the Open closed Principle?
SOLID
S: Single Responsibility
O: Open-Closed
L: Liskov Substitution
I: Interface Segregation
D: Dependency Inversion
Do you know why SOLID? If not, this read might help you.
We will discuss why SOLID is important in this section. Why should we practice SOLID? How it helps us? How it makes the complex life 🤯 of software developer easy 😌
Open Closed Principle
In simple terms, it means we should be able to extensible the code without modifying existing artifacts.
Closed for modification
Does that mean we should not write any code for that class?
Definitely no, what about bug fixes?
We have to do that but the best practice is to have proper test cases. Editing the system under test allows you to know the existing code is not affected and the bug is fixed.
Another is making any change to existing code smart enough that it should not force changes to other classes. If they do change, that signifies they are tightly coupled and it is a big red sign.
So yes changes should be done but smartly.
Open for extension
We have closed modifications to the existing class but opening for extension is also important in developing architectures. We should be able to extend existing features without making changes to existing code.
How is that even possible?
Here we can keep on increasing the features. The client doesn’t care what feature is added, it just knows it has to call processFeature(). The feature is loosed coupled with a client and we are able to add more and more features. Awesome!
But yes, building a stable FeatureInterface is also very important because any change in the interface will affect all the child classes. Changes should be nothing or the bare minimum.
Why so?
Fewer changes in existing code= less testing = happy developer
Fewer changes in existing code= fewer bugs in existing code on production = happy product and happy weekends 😁
With this, we are closing the open-closed principle.