17

One of the important concepts in Object Oriented Programming is Encapsulation. However, lately the software world seems to be tilting in favour of other paradigms like functional programming.

It makes me think, what about encapsulation and other OOP tenets? Are they wrong?

Is it that OOP is applied wrong? For instance Alan Kay is noted for saying in the OOPSLA'97 Keynote: "I invented the term object-oriented, and I can tell you I did not have C++ in mind."

Joe Armstrong - "Objects bind functions and data structures together in indivisible units. I think this is a fundamental error since functions and data structures belong in totally different worlds."

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
JeffV
  • 415
  • 4
  • 10
  • Possible duplicate of [Can "higher order function" feature allow/maintain abstraction and encapsulation?](http://programmers.stackexchange.com/questions/273405/can-higher-order-function-feature-allow-maintain-abstraction-and-encapsulation) – gnat Aug 25 '16 at 12:50
  • see also: [Is functional programming a superset of object oriented?](http://programmers.stackexchange.com/q/163432/31260) and [Equivalent of SOLID principles for functional programming](http://programmers.stackexchange.com/q/165356/31260) – gnat Aug 25 '16 at 12:51
  • @gnat I don't think there is any good answer under that question. And it seem to be too Python-specific. But I guess anything broader would be either Too Broad or Opinion Based. – Euphoric Aug 25 '16 at 13:26
  • 2
    I would say the question first needs to clarify what is meant by Encapsulation and what it means for language to support it. – Euphoric Aug 25 '16 at 13:27
  • @Euphoric none of the answers in that question looks Python specific, did I miss something? And frankly the question itself doesn't look really language specific, that snippet of Python code in it seems to be provided mostly as a concrete demonstration of a general concept – gnat Aug 25 '16 at 13:30
  • I think this question is answerable in the abstract sense that the OP is looking for. –  Aug 25 '16 at 13:32
  • @gnat Well. Half of the top answer is about encapsulation in Python and one answer is about "example problem" show in OP not existing in Haskell. Either the question is different from it's title or the answers are wrong. – Euphoric Aug 25 '16 at 13:35
  • @Euphoric I see what you mean, thanks for pointing that out. Need to chew it a bit, your explanation made me doubt if the duplicate vote is right here – gnat Aug 25 '16 at 13:46
  • 14
    I'm having trouble figuring out what this question is asking. Is it asking whether encapsulation can exist outside of OO (subject line)? Is it asking whether encapsulation is wrong (second paragraph)? Is it asking whether OO is applied wrong (third paragraph)? And what does the OP want to say with that Joe Armstrong quote? How does the OP define "encapsulation"? How does the OP define "OO"? What are those "other tenets"? – Jörg W Mittag Aug 25 '16 at 14:10
  • This question needs a bit of fixing up to be clearer and to the point. Hopefully that will happen and it'll be reopened. – Ben Aaronson Aug 25 '16 at 15:07
  • "It makes me think, what about encapsulation and other OOP tenets? Are they wrong?" Well, many argue that inheritence is a very bad idea, but both encapsulation and polymorphism occur in many languages/paradigms, not just OO. – David Arno Aug 25 '16 at 18:19
  • Has this been cleaned up? Based on the answers, it seems like a straight-forward question. – JeffO Aug 25 '16 at 18:36
  • Encapsulation appears in Abstract Data Types too, which was before OOP. – Adrian Iftode Aug 25 '16 at 20:11
  • Rust certainly supports encapsulation, but depending on your definition of OOP, it isn't object oriented. – CodesInChaos Aug 26 '16 at 07:46
  • 1
    Robert Martin once said that OOP took encapsulation away and then patched it back in with horrible hacks. "We had perfect encapsulation before oo". Of course he was being hyperbolic, but now everytime I see someone say that OO is about encapsulation I remember Uncle Bob. – GnP Aug 26 '16 at 22:23
  • @GnP: OO is about Messaging. Encapsulation is merely a consequence of that: if you send someone a message and all you can observe is the reply, then you have no idea, and no way of finding out, what the receiver did to come up with that reply. – Jörg W Mittag Aug 26 '16 at 23:47
  • If anyone knows how to reach into `(function() {var foo = 9;})();` and get the foo out, let me know; it's driving me nuts. With OOP you can at least use reflection; this seems impossible without direct memory manipulation/opening up the runtime. – Dmytro Nov 28 '16 at 19:04

4 Answers4

14

I think the trap you fell into here is thinking that there is a rigid definition of any programming paradigm (structured, object-oriented, functional, et. al.)

If you ask two different developers what OOP means, you will get two different answers. Yes, as a profession we agree that there are a few common themes such as encapsulation, data hiding, etc. covered by any OOP software engineering class in college.

However, in the real world, things are not quite so cut and dry, which is why two developers will give two different answers. Perhaps they are experts in different languages that express OOP concepts differently. Language paradigms tend to overlap. The latest rage as of 2013 or so is incorporating functional programming in object-oriented languages through closures or lambdas. Is Java 8 object-oriented or functional? I would call it object-oriented with a dash of functional. Someone else might describe it differently.

Is it that OOP is applied wrong?

No, the issue is that various languages express programming concepts differently. Perhaps one language leaves out an OOP concept, and another language includes it but leaves out a different OOP concept. Languages are typically designed for a purpose: to make a certain type of task easy, at the expense of making other tasks more difficult. That is neither right nor wrong, it is simply a real-world tradeoff that is impossible to avoid.

The real world is not the classroom. We either need to discuss conceptual programming paradigms at the abstract level, or we need to discuss real programming languages that are forced to make tradeoffs in order to be useful. As long as a programming language is mostly defined by an abstract definition of OOP, we can include it in that bucket.

10

However, lately the software world seems to be tilting in favour of other paradigms like functional programming.

That is debatable. First, I don't see any other paradigms besides OOP and functional programming which are discussed that broadly, so I guess we can forget the "other paradigms like" phrase, let us talk about FP, nothing else.

The reasons why functional programming became so popular over the last years were discussed in other questions here in depth, I am not going to repeat this (see here or here, for example). But, in my opinion this is not because "OOP was a big mistake", or "Functional vs. OOP are mutually exclusive", it is more like people extending their toolbox and try to get the best of both worlds. Ok, there are surely experts which are hardliners favoring one over the other, but you will find those guys on both sides.

It makes me think, what about encapsulation and other OOP tenets? Are they wrong?

Encapsulation has many different flavors. Functional programming languages and language constructs provide certain forms of encapsulation, object oriented others. If you are looking for examples of encapsulation with functional means, start with closures.

Concerning "other tenets": no, they are not wrong, but for certain scenarios like high scale parallelization, functional approaches probably scale better. For other scenarios, like creating well designed UI frameworks, OOP approaches scale probably better (YMMV, I don't just have a better example at hand). Moreover, I am sure for most real world scenarios it depends on the knowledge and experience of the team with its favorite programming paradigm how well a certain system will scale.

Is it that OOP is applied wrong? For instance Alan Kay is noted for saying in the OOPSLA'97 Keynote: "I invented the term object-oriented, and I can tell you I did not have C++ in mind."

Surely OOP is often applied wrong by many people, but I am sure the same is true for FP. And I would be astonished if John Mc Carthy (designer of Lisp) had something like Javascript in mind when he was thinking about functional programming (mercy on me, don't flame me too much for that comparison ;-)

Joe Armstrong - "Objects bind functions and data structures together in indivisible units. I think this is a fundamental error since functions and data structures belong in totally different worlds."

I guess the inventor of Erlang has some good arguments, but he has also his very own point of view, so let him his opinion and build your own. There are lots of other experts which have a different idea of this.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • 1
    Im not sure OO is particualry better for GUI, more that OO and GUIs emerged about the same time. +1 for the McCarthy/javascript though ;) – jk. Aug 25 '16 at 20:13
  • 1
    To be fair, [some people do suggest the existing approaches are flawed and *could* be replaced by something else](http://shaffner.us/cs/papers/tarpit.pdf). I don't know if I would call that "expanding" or rather "improving" the toolbox :) – Andres F. Aug 26 '16 at 02:38
  • 1
    @AndresF. That's a fantastic read. I plan on going through that paper in detail when I have some time. – Robert Harvey Aug 26 '16 at 15:48
5

Of course:

struct Foo
{
    string bar;
    int bux;
}

I know what you're going to say: "But that doesn't also encapsulate behavior!" Well, I'm with Joe Armstrong on this one: you can write entire programs or operating systems without ever requiring first-class objects. Linux proves that.

Javascript programmers routinely encapsulate state and behavior in functions and closures, not classes.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 3
    You can also shovel hill of dirt with just teaspoon. But anyone who claims that is optimal way to do it should be viewed with suspicion. – Euphoric Aug 25 '16 at 14:14
  • 7
    I never said it was optimal, and that's not what the OP is asking anyway. In other news, I find it amusing that Linux qualifies as shoveling dirt with a teaspoon. – Robert Harvey Aug 25 '16 at 14:33
  • fiven that FP languages have first class functions this does allo behaviour to be encapsulated as well no? – jk. Aug 25 '16 at 16:25
  • 2
    @jk. Of course. So does C, of a sort. – Robert Harvey Aug 25 '16 at 16:26
  • 1
    indeed it sort of does – jk. Aug 25 '16 at 16:31
  • if you squint and tilt your head just so... – Michael Brown Aug 25 '16 at 16:49
  • 4
    I wouldn't call C structures 'encapsulation'. They neither protect the data nor necessarily provide any standardized methods for accessing it. They just save you from doing manual pointer arithmetic. For example it's pretty common to find networking code that casts serialized packets into structures and vice versa. Get the network byte order wrong and fun ensues. – david25272 Aug 26 '16 at 02:59
  • @david25272: Then refer to the last paragraph in my answer. DTO's are used all the time in practice, even in typical OOP. – Robert Harvey Aug 26 '16 at 13:57
  • you could simulate behaviour encapsulation with function pointers (thinking C here for example) – Newtopian Aug 26 '16 at 15:15
  • I'm not disagreeing that you could do encapsulation in a non-OO language by adhering to certain rules - such as always using an API to access a structure. But then you're essentially doing OO programming anyway. – david25272 Aug 28 '16 at 23:26
  • Most Javascript programmers use closures in a way that lets them pretend that Javascript has objects..... (I can't decide if this is good or bad given we often have no option but to use Javascript.) – Ian Mar 20 '17 at 11:54
4

The main issue here is that Encapsulation is not rigidly defined concept nor why is it useful. Doing some research shows that how people see encapsulation is highly opinionated and that lots of people confuse it with Abstraction.

First definition you are going to find is

Encapsulation is a concept that binds together the data and functions that manipulate the data ...

If this is your definition, then majority of languages have way to group data and functions operating on that data into classes, modules, libraries, namespaces, etc..

But I would argue that is not the main purpose of encapsulation, as that definition continues :

... , and that keeps both safe from outside interference and misuse.

Wikipedia also agrees on that :

A language mechanism for restricting direct access to some of the object's components.

But now, we need to ask what is meant by "interference and misuse" and why should direct access to the data be restricted. I believe there are two reasons.

First is that limiting scope in which data can be mutated is in developer's best interest. Way too often there is need to have logic before/after value is set. And having only limited number of places where value can be set is extremely valuable. In OOP languages this can be done using classes. In "mutable" functional languages closures serve same purpose. And because we know classes = closures, it is even arguable than mutable functional languages are different "paradigm" to OOP.

But what about immutable languages? It doesn't have problem of mutating variables. This is where second issue comes in : binding functions and data allows to keep that data in state that is valid. Imagine you have immutable structure for Email. This structure has single string field. We have requirements that if you have value of type Email then that field contains valid address. In OOP's encapsulation, this is easily done by declaring that field private, providing only Get method and having constructor method that only succeeds if passed in string is valid address. Similar thing with closures. Now, for immutable language, it would need way say that structure can only be initialized through specific function and such function can fail. And I'm not aware of any language that would fit that criteria (maybe someone in comments can enlighten me).

Last issue is what is meant by language "supporting" encapsulation. On one side there are languages that allow enforcing of encapsulation, so that code won't compile if encapsulation is broken. On the other side, the language might provide way to do encapsulation, but it doesn't enforce it, leaving developer to enforce it themselves. For the second case, any language that has structures and functions can work. Dynamic languages and Haskell come to mind. And I would say there aren't many languages that fall on the other side of the spectrum. Even C#, which is really good at enforcing encapsulation for it's objects can be bypassed using reflection. But seeing that in C# would be considered massive code smell and no sane C# developer would do that willingly.

Euphoric
  • 36,735
  • 6
  • 78
  • 110