Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / architecture

A Call To Drop The Open Closed Principle From The SOLID Design Principles

4.06/5 (8 votes)
16 Jun 2013CPOL3 min read 32.6K  
We are used to repeat the SOLID principles, sometimes without giving them full thoughts. This is a call to drop an outdated principle that doesn't work in today's development, the Open Close Principle, the result should be SLID.

SLID

The OCP or “The Open Closed Principle” is the popular “O” from the SOLID principles of Object-Oriented Design.

As an architect consultant working in London, I get a question about SOLID, every other interview. As usually the interview is about an hour, I answer the question by an out-of-the-textbook type of answer, otherwise, the interview will span for hours. I keep my opinions for myself and I am a happier person. :)

The Principle Origin

First of all, let me cite the OCP origin. This is first mentioned by Bertrand Meyer the creator of Eiffel, then surfaced in 2000 by Robert C. Martin (Uncle Bob) in Principles and Patterns.

Quoting form Martin’s paper:

"A module should be open for extension but closed for modification."

"Of all the principles of object oriented design, this is the most important. [...] . It means simply this: We should write our modules so that they can be extended, without requiring them to be modified. In other words, we want to be able to change what the modules do, without changing the source code of the modules."

What is a Module?

A module can be a class, a group of classes or a component, such as a library, that forms a feature. I will assume all of these in the discussion below.

Virtual Everywhere

The moment I release a class, I should not change it! I should think of all possibilities of how it can be extended, prepare all my methods to work well as virtual functions, drop “start with the simplest thing that works” Agile principle and think of the complete architecture, right now! I am going to lose my last chance of modifying it, I better act now, before I even check-in, this is the point of no return, the close to open moment, my last chance, mwahh ha ha haaaa!

Why, In the Hell, Are We Writing Unit Tests?

If every time I want to leave existing implementation, following OCP, without changing it and extending my module, then I don’t actually have to write future proof code as the code will not change, every time I will be extending existing code and as the norm in the software in change, I will keep extending and I will be reaching hundreds of inheriting class chains. I could maybe write much less unit tests as the logic of this module will not change!

But What Happened to “Favour Composition Over Inheritance”?

Doesn’t this contradict with the OCP? Now, every time I want to change, I will wrap my existing class with another class (sort of the Decorator GoF Design Pattern) and then the original implementation would be covered with 10s of classes and can’t be reached.

Seriously, Let's Drop It

Maybe this principle has worked in the Waterfall era, or it might, I used the term “might”, work in 3rd party components or frameworks, but it doesn’t work in the day-to-day business development.

Conclusion

We are used to propagating this term, no longer calling it a principle, without giving it much thought. The next time an interviewer asks me about the SOLID principles, I will ask him/her, did you mean SLID?

I was reviewing this before posting it and the tone looks like I am raging, but the fact is, I am laughing and enjoying it. I hope you share me a similar joy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)