Inyesterday’s tip, we examined the second of the SOLID Principles, the Open-Closed Principle, and learned how it is a driving force in the creation of pluggable systems. We identified an OCP violation and subsequently refactored the code to make it OCP-compliant. This principle encourages us to design our systems so we write new code rather than revise what we already have.
Can we take the OCP to its logical extreme where we never need to modify existing code? Where we only ever write new code?
Unfortunately not. It is not possible. We will still need to change code – there is no way getting around that.
Problem with the OCP is that it’s an ideal. When applying the OCP, we are attempting to predict the future. We are making our system pluggable where we think changes will be occurring.
For example, the business has decided to use PayPal as the payment gateway in our bespoke e-Commerce application. Being the well-informed developers we are, we know newer payment gateway options like Stripe and Braintree are becoming popular. We think that it’s foreseeable that in the future, the business will want the application to use a different payment gateway. Consequently, we make the payment gateway pluggable via an IPaymentGateway interface. See here and here for more detail on how to do this.
We have identified a likely axis-of-change and designed our system to be flexible should changes along that axis occur.
However, the future is uncertain. Sometimes the expected does not happen. The robotic uprising of the mid-90s never occurred. ;D
Joking aside, sometimes changes will be unexpected. Maybe the business wants a line manager to receive a notification email for each e-commerce purchase. We had not anticipated that. Or that customers who bought from a particular product category should be sent a promotional email.
There are myriads of unforeseeable ways in which a system could evolve. In those instances, we will need to make changes to the existing code.
So, design your systems with the OCP in mind. However, know that you will still need to modify existing code.
OCP Looks Into The Future
/by Olaf ThielkeOCP Looks Into The Future
In yesterday’s tip, we examined the second of the SOLID Principles, the Open-Closed Principle, and learned how it is a driving force in the creation of pluggable systems. We identified an OCP violation and subsequently refactored the code to make it OCP-compliant. This principle encourages us to design our systems so we write new code rather than revise what we already have.
Can we take the OCP to its logical extreme where we never need to modify existing code? Where we only ever write new code?
Unfortunately not. It is not possible. We will still need to change code – there is no way getting around that.
Problem with the OCP is that it’s an ideal. When applying the OCP, we are attempting to predict the future. We are making our system pluggable where we think changes will be occurring.
For example, the business has decided to use PayPal as the payment gateway in our bespoke e-Commerce application. Being the well-informed developers we are, we know newer payment gateway options like Stripe and Braintree are becoming popular. We think that it’s foreseeable that in the future, the business will want the application to use a different payment gateway. Consequently, we make the payment gateway pluggable via an IPaymentGateway interface. See here and here for more detail on how to do this.
We have identified a likely axis-of-change and designed our system to be flexible should changes along that axis occur.
However, the future is uncertain. Sometimes the expected does not happen. The robotic uprising of the mid-90s never occurred. ;D
Joking aside, sometimes changes will be unexpected. Maybe the business wants a line manager to receive a notification email for each e-commerce purchase. We had not anticipated that. Or that customers who bought from a particular product category should be sent a promotional email.
There are myriads of unforeseeable ways in which a system could evolve. In those instances, we will need to make changes to the existing code.
So, design your systems with the OCP in mind. However, know that you will still need to modify existing code.