TL;DR – Object oriented design did not bring encapsulation principles to software development, it broke it.
Many of my friends go to job interviews for software engineering positions, and the question they are asked a lot is “what are the advantages of object oriented programming?”.
The question in itself is not that bad, actually, but the answers they are expected to give are horrible mistakes. One of the “musts” in the eyes of the interviewer is for the candidate to mention the term “Encapsulation of functionality”. OOP gave us encapsulation? No, OOP broke the encapsulation principle in favor of other advantages.
Let’s talk about it for a second, should we?
Do you remember the old days before the modern OO languages like Java or C#?
I’m not talking about C++ glory days, rather about C days, when people actually wrote C code for a living. When most commercial software was written in C.
Well, if you don’t then let us refresh the memory. How did we use to import a library? We included header files like this:
What were those header files? Those were declarations. You couldn't see the local variables, you couldn't change the functions and you didn't know anything about the implementation! You could only see declarations. That was the only relevant information provided to your code as an API.
That was true encapsulation. Perfect. No access modifiers, no difficult file organizations, just true encapsulation of functionality and data.
Object oriented design brought a lot of goodness to the table, but encapsulation was not one of them. It destroyed it. How did the OO language designers "solve" this problem? Well, they invented hacks in the form of access modifiers. Keywords like "private", "public" or "internal" are nothing but hacks invented to solve the issue of breaking encapsulation.
Here is a wonderful lecture from Robert Martin where he touches on the subject.
In conclusion, the next time you will be asked to talk about the goodness of encapsulation in object oriented languages - remember, it is not a philosophical question, OO design destroyed it. It's a fact.
If you are angry, mad or just happy about this post and want to share it with me – leave a comment.