Benefits of the Open/Closed Principle
‘Software’ is soft because it is flexible for change. How change is implemented in a system can have a significant impact on the success and longevity of that software application. The Open/Closed Principle (OCP) states that the behaviors of a system can be extended (Open For Extension) without having to modify the existing system (Closed for Modification). In other words, new features should be implemented by writing new code, not by modifying existing code. The benefits of adhering to the OCP can potentially streamline code maintenance and minimizing risk of breaking the existing code base.
Open For Extension
The first part of the principle covers how software entities can be change by extension. One benefit is that programmers do not have to reinvent the wheel if they can extend upon behaviors that have already been defined. A second benefit is that only new changes will have to deployed as opposed to making changes to the existing system and redeploying the entire code base back into the production environment. Furthermore, if the business determines that the feature implemented no longer serve user needs then the changes can be easily reverted as opposed to going back into the code base.
Closed for Modification
Software entities should be ‘closed for modification’ because making changes directly to those entities may result in unwanted consequences. For one, it is possible that a bug is introduced in the system. Secondly, adding onto an existing code base increases additional complexity to the system, which can get out of hand fast when compounded over time. The worst case scenario would be to break the existing system. Realistically, it might not always be possible to add new features by extension; however, it is important for me to adhere to this principle at all times possible.
A Simple Example
A simple example that illustrated the OCP is by customizing the styling of a bootstrap component. To implement changes and extend on the framework, I would override the designs in my own .css file. The changes would be closed for modification because I would not make changes directly in the bootstrap.css file.
Adhering to OCP means the the architecture would be decoupled. A well designed system would allow software entities to be reused and extended where needed. This means that it is important to spend time upfront designing software entities that further encourages adhere to the OCP. Not doing so will leave behind technical debt that would have to be cleaned up in the future.