The open closed principle – you ain’t gonna need it

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?



About GrahamTheCoder

I'm Graham (the coder). A C# developer based in Cambridge hoping blogging will achieve some or all of the following. Help me organise my thoughts. Practice writing things other than code. Give me a place to refer people to when I'm trying to make a long winded point. I welcome comments and constructive criticism, and hope to look back at my written opinions in the future and laugh at my own naivety.
This entry was posted in Principles and tagged , . Bookmark the permalink.

One Response to The open closed principle – you ain’t gonna need it

  1. Pingback: SOLID refresher | Crafting code in C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s