This post makes reference to several SOLID principles.
For those who internalize good practice rather than memorize quotes, the open closed principle states:
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
Roughly speaking, when code is added to support a new feature, that code should fit behind an existing abstraction. Since modules already know about that fixed abstraction, it doesn’t need to change. The advantage is that you massively reduce the chances of breaking existing functionality, because you aren’t changing it.
However, we’re already using the other SOLID principles, so thanks to dependency inversion, our modules already depend on interfaces and know nothing of the runtime concrete implementation that will be used to satisfy that interface. Interface segregation means those interfaces are minimal, so it wouldn’t need to change for anything unrelated either.
The only thing that remains for the open closed principle to enforce is that we place our interfaces and implementations in modules in such a way that recompilation of fewest modules is needed to make predicted future extensions. That seems reasonable to me. But doesn’t (Robert Martin’s version of) the single responsibility principle tell us a module should only have one reason to change?
What the principle does seem to (presumably accidentally) encourage, is for people to wildly speculate about future extensions such as big-endian x86, which end up never happening, thus over-complicating the current system.
My suggestion is that rather than abstracting everything you may ever extend up front, you split implementing a feature into two parts. Refactor to add the appropriate abstraction – make room for the feature. Then add it in that space.
Practically speaking you may need a technical spike to understand where the feature could fit first.
My suggestion is that we remove the O from SOLID, and add a Y at the end for “You ain’t gonna need it”.
Don’t the SLIDY principles sound like more fun to follow anyway?