OOP destroyed encapsulation

TL;DR – Object oriented design did not bring encapsulation principles to software development, it broke it.

Encapsulation

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:

    #include "SomeReferencedFunction.h"

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.

Code on,
Shonn Lyga.

About Shonn Lyga

Obsessed with anything and everything in Software Engineering, Technology and Science
This entry was posted in General Software, OOP, Software Concepts, Software Design and tagged , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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