A Software Entity Should be Open for Extension
but Closed for Modification
When evaluating the Responsibility and Dependencies of a Software Entity such as a Class or Method consideration should be taken to ensure that all attempts are made to abstract areas that might change.
Compliance with SRP will help to create code that can comply with OCP.
The Open / Close Principle seems like it is an oxymoron:
“How can you be ‘Open’ for Change (Extension) but ‘Closed’ for Change (Modification)”.
The Answer is by Abstracting Areas of Change
When complying with Single Responsibility Principle your objective is to create Classes and Methods that keeps its focus on the outcome of the behavior the “What”, and not the details of “How” that behavior is accomplished.
The “What” will probably change throughout the life cycle of the Software Entity
… But the “How” very well may change
Using private class level and public common assembly level Helper Methods that define the “How” will abstract away the details of the outcome from the responsibility of the software entity.
The Helper Methods are the Areas of Change
Helpers that are used for code reuse are great for encapsulating functionality that is Open for extension but allows the consuming type to be Closed for Modification.
There is a major performance advantage to this philosophy as well.
The Just In-Time Compiler (JIT) looks for short and simple methods that are frequently used, helper methods, and can “In-lined” for better performance.
In-lining substitutes the body of a function for the function call.
The JIT uses Registers and Pointers to catalog the signatures of small methods such as those used to abstract away the details of a Class’s responsibility.
Smaller and simpler functions make it easier for the JIT compiler to support “Enregistration”.
This is the process of selecting which local variables can be stored in registers rather than on the stack.
This register is a single block of Managed Heap memory that can access through pointers possibly hundreds or thousands of discrete method variables.
It will only compile for the Common Language Run-time (CLR) when actually called.
This means if a Case Statement or Nested If Conditionals are never used the CLR will never see the code.
This Enregistration Process Saves
Run Time Memory and CPU cycles
The following two tabs change content below.
I am a Principal Architect at Liquid Hub in the Philadelphia area specializing in Agile Practices as a Certified Scrum Master (CSM). I use Test Driven Development (TDD) and Acceptance Test Driven Development (ATDD) with Behavior Driven Development (BDD) as my bridge to Agile User Stories Acceptance Criteria in a Domain Driven Design (DDD) implementing true RESTful services