Context : ObjectOrientedProgramming / FunctionalProgramming

A comment I left on https://www.youtube.com/watch?v=wyABTfR9UTU :

If you really want to put it into a nutshell, then all programming is about trying to prevent complexity spiralling out of control. And complexity is largely made of dependencies between this bit of code and that bit of code. Some of those dependencies are unavoidable and should be explicit. Others are accidental, and you'd be better off without them.

OO is a bet that the way to reduce dependencies is "data-hiding". Hiding the details of how something is implemented from the code that relies on it. So the dependency can be simplified to the broad overall effect as defined in the API or interface. Rather than the full details of how the component achieves that effect.

FP is a bet that the way to reduce dependencies is "immutability". Restricting the number of points in the code at which state can be updated, so that fewer bits of the code are actually involved in any particular effect. The data in a data-structure could only only have been put there at the point that structure was created, which makes it much easier to pin down why the data is wrong, compared to if that same data could have been updated in a number of different places.

The argument in favour of FP over OO is that immutability is better than mere data-hiding for keeping down complexity. With immutability you can really know that what comes out of this function is only dependent on the arguments and what the function does. The bug must be somewhere in the function code and the code it calls on. OTOH, when you are looking at a class definition to find out why an object contains some incorrect value, Even when you know what happened in the constructor, and that the data can only have been updated through a specific interface, you STILL have to troll through the rest of your codebase to find any other code that might be sending an update message to that object.

Over the years I've used a range of languages from BASIC, Fortran, C, C++, Java, Smalltalk, Perl, Python, Ruby, Erlang, and now, by preference, Clojure. And I am pretty sold on that virtue of immutability. Polymorphism is a feature of both OO & FP.. It's not really the thing that distinguishes them. Agreed OO is perhaps a little more intuitive to beginners. But once you're used to FP, then maps and folds and filters are easy to read and write as for-loops are to a C programmer. The big difference between OO and FP is that OO is about enforcing data-hiding, and FP is about enforcing immutability. And I'm increasingly convinced that of the two, the immutability is the most worth having.

Backlinks (1 items)