Hacker News new | ask | show | jobs
OOP vs. FP (medium.com)
33 points by horrido 3338 days ago
5 comments

I don’t wholly disagree with the article, but I think it overstates its case. In particular:

> Is there really so much difference between f(o), o.f(), and (f o)?

Yes, there is!

In the case of o.f(), o needs to know about f.

In the case of f(o), f needs to know about o.

Maybe I'm mistaken... but at least in C++ the implementation of Foo::Bar() is similar to Bar(Foo):

    Bar(Foo * foo); // how Foo::Bar() is implemented
So while Bar(Foo) could be passed as several ways:

    Bar(Foo foo);
    Bar(Foo &foo);
    Bar(Foo *foo);
    ....
there isn't really a huge distinction between the two except for syntax and locality within the source file. (That is, class methods are all defined in the class and can't be spread across multiple header files).
I don't see that as much difference.
I think the difference becomes easier to see when you need to add new data types vs new functions

http://wiki.c2.com/?ExpressionProblem

Take a class C.

Alice extends class C with class A, which implements function a.

Bob extends class C with class B, which implements function b.

I have both Alice’s and Bob’s code, and I want to use both a and b. How can I do it?

Of course, there are ways to do it – notably multiple inheritance, if your language supports it – but the point is, in a functional language, this question never even arises. You just import the function definitions and use them, without ever having to think about the mechanics of it.

By the way, I’m not saying that this necessarily makes functional languages better (although I do tend to prefer them). I’m just pointing out that the difference is real.

Inheritance is not a strict requirement of OOP. In OOP, you can always use composition, in which case the question also never arises.

Inheritance is there if you need it. It's an available option.

Yes, but in common discussion, noone when talking about OOP is referring to inheritence-less OOP. No common programming language uses it, few to no programmers practice it. OOP/w inheritance is the default item being discussed.
And why is "common discussion" technically relevant? This is not a fault of OOP but of education.
> in which case the question also never arises.

I'm not sure I see that.

If class A and B wrap a C by composition and expose, respectively, `a` and `b`, then I still very much have the choice of which I'm building. Perhaps less so if they wrap a C by reference, so the C can be reused (it's too late at night to be sure I've thought it all the way through).

Maybe it's best to think about this in a capabilities-security aspect.

'o' gives you capabilities to perform 'f'. 'o' can hide its data and only expose functions that it wishes.

With f(o), 'f' needs to be given permission to access 'o' internals.

In Nim, with its unified function call syntax, there is no difference between them, which is pretty neat.
Valid points, but not _the_ point. There is a big difference between allowing and supporting something.

OO languages _support_ hidden inputs and outputs as well as programming by mutation. They _allow_ programming in a functional style, but you will have to be inventive for it.

FP languages _support_ immutable values, referential transparency and all that. They _allow_ programming by mutation and hidden inputs and outputs, but you will have to be (sometimes very) inventive for it.

Exactly. Starting points matter. And for me, a starting point of "enforces immutable data" is a good one.
Starting points matter, but this depends on the programming situation. That's why you use OOP or FP where it is most appropriate or suitable. Neither OOP nor FP is a panacea. Neither OOP nor FP is the universal programming tool.
Nothing prevents an object's methods from being referentially transparent. It just depends on how you choose to write them.
hmm, not so sure I agree to that extent.

[To me,]

FP is more about nobody owning the data. Everything operating on all the data. (Mostly) All data exposed and accessible.

OOP is about certain classes owning data and limiting exposure to that data.

It has nothing to do with ownership. In FP, a function can only operate on data of a particular type. In OOP, a particular type (or class) gathers all the applicable functions (or methods) under one roof. Same difference.
Well, not really.. In effect, your class owns the relevant piece of data that it operates on and only allows pre-defined operations on that data.

Maybe my point is better articulated by saying [to me,] functional programming is more data oriented than OOP.

The word "own" is pretty meaningless. What does "own" even mean? Classes and objects (thought of as "types") are an organizing principle for organizing your functions. If you didn't organize them in this manner, then you'd have the same functions that operate on the same data types, only they're scattered everywhere, just like in functional programming.
> the same functions that operate on the same data types, only they're scattered everywhere

???

OO is different because you have things like private variables. Meaning that only methods of an object's class can access the variable. So you now have a special group of functions that can access the variable. FP is not like that.

A closure is a poor man's object, and an object is a poor man's closure: http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m...
Is this guy talking about OO as described by smalltalk? Because he says that objects are bags of functions, not data, but in languages like java, c# and c++ objects are bags of both.
Yes, he is probably talking about Smalltalk-style OO:

https://medium.com/@richardeng/domo-arigato-mr-smalltalk-aa8...

He's also the subject of this article: https://thenewstack.io/can-man-spark-renaissance-smalltalk-p..., which made the front page of HN a while back (https://news.ycombinator.com/item?id=13642947)

It's Uncle Bob who says objects are bags of functions. And this is not specific to Smalltalk nor dynamic languages. In Java, C#, and C++, objects are still just bags of functions.
Java and C# allow you to expose mutable fields, so objects are not strictly (or even ordinarily) just bags of functions.
Yeah but objects in those languages are also bags of data, which he explicitly said they are not.
The C++ family of languages isn't really all that object-oriented; it's practically a different (static, class-oriented) paradigm that takes some inspiration from OOP (mostly from Smalltalk) but more from C's particular implementation of statically-typed procedural programming, as a result they are based around structs-enhanced-with-methods rather than proper objects.