Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Open-Closed Principle in SOLID Object Orientation Rules Challenged

Open-Closed Principle in SOLID Object Orientation Rules Challenged

This item in japanese

Lire ce contenu en français

The Open-Closed Principle, OCP, part of the object-orientation SOLID principles, was recently criticised by Jon Skeet and Robert Ashton who both believes the principle, as it is defined, is doing more harm than good. Robert C. Martin, who identified the principles in the early 2000s, however, defends the principle, arguing that you have to look at the full description, not just the short definition.

The debate started by a tweet from Jon, author of C# in Depth, questioning the principle:
”I know that doubting things like OCP is pretty close to heresy, but it's just *never* made sense to me.”

Robert C. Martin in an answer looked back at his definition in the original OCP article:

Modules that conform to the open-closed principle have two primary attributes.
1. They are "Open for Extension". This means that the behaviour of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.
2. They are "Closed for Modification". The source code of such a module is inviolate. No one is allowed to make source code changes to it.

In his answer Robert admits that the definition was a bit overstated but also claims that the full article gave a more moderate explanation and that his newer articles has improved the definition.

Jon followed up on Robert’s answer arguing that the definition and description is too vague and unclear, these types of principles must be very carefully defined with the use of correct terminology to make the essence clear. Instead Jon prefers a similar concept coined by Alistair Cockburn, Protected Variation, which he thinks has a better description:
“Identify points of predicted variation and create a stable interface around them”

Robert Ashton later on also criticised the principle which he believes is the cause of many over-designed pieces of software. But he also sees areas where the principle makes sense, e.g. in areas that are repeatedly modified.
He then followed up with a look at each of the four remaining SOLID principles.

Rate this Article