Click here to Skip to main content
15,890,579 members
Articles / General Programming / Architecture

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

Rate me:
Please Sign up or sign in to vote.
4.06/5 (8 votes)
16 Jun 2013CPOL3 min read 30.2K   28
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)


Written By
Architect
United Kingdom United Kingdom
Passionate about refining software practices, promoting self-motivated teams and orchestrating agile projects.
Lives in London, UK and works as a .NET architect consultant in the City.

Blog AdamTibi.net.

Comments and Discussions

 
GeneralMy vote of 2 Pin
abdu30-Oct-13 6:47
abdu30-Oct-13 6:47 
GeneralRe: My vote of 2 Pin
Adam Tibi30-Oct-13 8:27
professionalAdam Tibi30-Oct-13 8:27 
GeneralRe: My vote of 2 Pin
masbaaz28-Nov-13 0:57
masbaaz28-Nov-13 0:57 
GeneralMy vote of 5 Pin
Fabio Franco24-Jun-13 2:28
professionalFabio Franco24-Jun-13 2:28 
GeneralMy vote of 5 Pin
Rob Philpott20-Jun-13 23:13
Rob Philpott20-Jun-13 23:13 
GeneralRe: My vote of 5 Pin
Adam Tibi21-Jun-13 6:57
professionalAdam Tibi21-Jun-13 6:57 
QuestionYou aren't the first person to make this mistake. Pin
Robert Martin18-Jun-13 2:19
Robert Martin18-Jun-13 2:19 
AnswerRe: You aren't the first person to make this mistake. Pin
Adam Tibi18-Jun-13 4:52
professionalAdam Tibi18-Jun-13 4:52 
GeneralRe: You aren't the first person to make this mistake. Pin
jphamilton19-Jun-13 5:48
jphamilton19-Jun-13 5:48 
General[My vote of 1] Call not accepted Pin
jphamilton17-Jun-13 5:48
jphamilton17-Jun-13 5:48 
GeneralRe: [My vote of 1] Call not accepted Pin
Adam Tibi17-Jun-13 7:11
professionalAdam Tibi17-Jun-13 7:11 
The principle doesn't say you can't ever modify classes again.
Did you read the definition? You are interpreting it with what suits you, which is a good practice, rather than with what it really says.
If we need to add something new we do that by adding classes
You are correct in your interpretation this time but now I am saying that I disagree. Not changing code that is already working will mean you will start accumulating technical dept. Unfortunately, you will have to change working code to add new features and you need to make sure that this won't take a rippling effect by writing the proper unit tests to protect you (TDD and Continuous Integration revolve around this). Your statement above means that you are throwing the agile principle "write the simplest thing that works then refactor" in favour of sticking to a "principle".
Can we always do this? No
If you can't always do this, then how can it be a principle? You are actually agreeing with my conclusion of calling it a "term" or maybe calling it a "good practice".
Make it simple, as simple as possible, but not simpler.

GeneralRe: [My vote of 1] Call not accepted Pin
jphamilton17-Jun-13 8:04
jphamilton17-Jun-13 8:04 
GeneralRe: [My vote of 1] Call not accepted Pin
Adam Tibi17-Jun-13 9:03
professionalAdam Tibi17-Jun-13 9:03 
GeneralRe: [My vote of 1] Call not accepted Pin
jphamilton17-Jun-13 9:21
jphamilton17-Jun-13 9:21 
GeneralRe: [My vote of 1] Call not accepted Pin
Adam Tibi17-Jun-13 9:49
professionalAdam Tibi17-Jun-13 9:49 
GeneralRe: [My vote of 1] Call not accepted Pin
jphamilton17-Jun-13 10:01
jphamilton17-Jun-13 10:01 
GeneralRe: [My vote of 1] Call not accepted Pin
Adam Tibi17-Jun-13 10:14
professionalAdam Tibi17-Jun-13 10:14 
QuestionNice argument, false premise Pin
Darek Danielewski17-Jun-13 2:24
Darek Danielewski17-Jun-13 2:24 
AnswerRe: Nice argument, false premise Pin
Adam Tibi17-Jun-13 2:46
professionalAdam Tibi17-Jun-13 2:46 
GeneralRe: Nice argument, false premise Pin
Darek Danielewski17-Jun-13 5:07
Darek Danielewski17-Jun-13 5:07 
GeneralRe: Nice argument, false premise Pin
Adam Tibi17-Jun-13 7:28
professionalAdam Tibi17-Jun-13 7:28 
GeneralRe: Nice argument, false premise Pin
jphamilton17-Jun-13 8:25
jphamilton17-Jun-13 8:25 
GeneralRe: Nice argument, false premise Pin
Adam Tibi17-Jun-13 9:11
professionalAdam Tibi17-Jun-13 9:11 
GeneralRe: Nice argument, false premise Pin
jphamilton17-Jun-13 9:31
jphamilton17-Jun-13 9:31 
GeneralMy vote of 3 Pin
Paul Tait17-Jun-13 0:26
Paul Tait17-Jun-13 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.