r/functionalprogramming • u/raulalexo99 • Feb 14 '23
Question Is FP in conflict with OOP? (Example provided)
So, I was reading this article. It is a summary of one of the many Martin Fowler's Refactoring techniques. This article was not written by him, but the autor just extracted this information from Fowler's book.
As you can see, Fowler's idea is to make parameter lists shorter, by introducing internal state in the object, and querying that internal state from other methods. This is in order to make code easier to understand and reusable inside the object.
So, does this mean OOP and FP are kinda opposite to some extent? Because as far as I know, FP relies heavily on pure functions.
What is your opinion?
14
u/pilotInPyjamas Feb 15 '23
In OOP, ignoring virtual dispatch,
this.foo(arg)is essentially the same asfoo(this, arg). The second form is possible in functional languages, so there is no significant difference between the OOP and functional styles in this case.16
u/OpsikionThemed Feb 15 '23 edited Feb 15 '23
There's no conflict there. Nothing says that getSeasonalDiscount() or getFees() are stateful, or even that the
thisobject in that example is stateful. It's a perfectly reasonable refactoring. A functional version might be->
(I think you could argue that there's a tension between, at the extremes, the pure functional style where composability is built up from referential transparency, and the pure OO style where composability is built up from state-encapsulation. But that's not this example.)
8
u/redchomper Feb 15 '23
I fear the example's been mischaracterized. It's not about introducing state; it's nothing to do with state. Shorter signatures are nicer than long ones, so if you have a function that operates on a collection of fields from the same structure, then consider passing the original structure instead. It just so happens that in the example notation, the original structure is called "this".
5
u/josephjnk Feb 15 '23
There are multiple equally-valid definitions of both FP and OOP. Some of these definitions are in conflict, and some are not. So yes, there is conflict, but not irreconcilable conflict.
I don’t believe the article you posted demonstrates a conflict, because nothing in it is particularly functional or object oriented. They’re taking multiple computations from outside a method and putting them into a method, without involving the notions of private state, function composition, encapsulation, etc. For all intents and purposes this could be done in C.
4
u/[deleted] Feb 15 '23
They are different tacks for dealing with state, yes, but not incompatible.
It's not uncommon for me to use FP and OOP together. In general, I relegate FP principles to my domain model. The data representing things inside the domain of the app are represented immutably and transformed with pure functions whereas the program's plumbing (its imperative shell) can be handled with OOP.
If you haven't learned Clojure it's worth learning just to see how atoms are used. Then look at Elm's architecture. If you store your domain state inside an atom you can effectively model Elm's architecture. Then freely use all your OOP stuffs for whatever things belong to the machinery of the program.
3
u/jhuni Feb 15 '23
FP is not in conflict with OOP. The success of modern functional programming languages like Clojure, F# and Scala is a consequence of their ability to coexist with object oriented platforms like the JVM and the CLR. Its fully possible to do functional programming in an object oriented platform like the JVM.
Take Clojure for an example. Every Clojure function is actually just its own class on the JVM, which overloads the invoke method of clojure.lang.IFn in its own way. The same is true for any other JVM language, which necessarily has to model functions by methods within classes. Yet there is no limit of the ability of the JVM to support functional programming. In the other direction, it is fully possible for a functional language on the JVM to make use of all object-oriented features.
So the compatibility between functional and object oriented paradigms has been a thing for a while now. That is good thing too, because otherwise functional programming would hardly be able to achieve the success that it has. Both paradigms have their place in any modern software system.
To determine how OOP and FP might be incompatible we have to first determine what OOP actually means. If it refers to runtime polymorphism, then that is a universally good thing and there is certainly no conflict. Even a purely functional language should have runtime polymorphism supported by multiple dispatch.
On the other hand, if OOP refers to the idea of mixing data types with state, then there is a certain incompatibility and difference in philosophies. Quite often classes are used to encapsulate stateful parts of a structure. If that is what OOP refers to there is a certain incompatibility. FP languages instead prefer to make data structures immutable. So there would seem to be an incompatibility between OOP time and FP time, but there is no conflict beyond that.
4
u/[deleted] Feb 15 '23
OOP and FP have different ways to do things. You won't find many patterns for FP because it solves those problems differently.
I compiled a list of FP refactorings from Fowlers list and it is half as long as the OOP version. All the cleaning up state and mutation ones are missing because FP aims to isolate state and mutable data.
2
u/[deleted] Feb 16 '23
I have not read your example. But generally yes, a conflict between OOP and FP exists.
One part of OOP is being imperative. This is debated somewhat, but i think is rather true than false. Even the word "object" is not meaningful in functional programming.
2
u/przemo_li Feb 17 '23
OOP must have inheritance (otherwise it's just imperative programming), or else expression problem is unsolvable (or you solve it by switching paradigm but then it's X + small bits of OOP).
Inheritance make type inference co l impossible.
So OOP is in deed in conflict with typed functional programming
2
u/gusdavis84 Feb 15 '23
This is just my personal view and absolutely no one else's
IMHO yes true OOP cannot work with a truly functional language or I would at least say the current way OOP is used cannot work truly with functional programming.
My reasons would be not just one feature or another but rather the mind set of an OPP solution vs a functional solution. Whenever you use OOP you're thinking of the problem in a very class first way of solving a problem. Everything has to be in a class , then you use methods on that class, then you use encapsulation to protect your data, and then you can use inheritance in some ways all an attempt to solve a problem.
Now in a truly functional solution you wouldn't think of first starting with a class you would start with data. What are you trying to get, append, store, or send? You would start with data and how to structure it, then use functions to either retrieve, append, store, or send data someplace else. You don't have to worry about classes since youre not thinking about things from a class blank fashion. You stop first and say I need a Data structure for say a person. You wouldn't say class person and then proceed. You would say something like I need a hashmap or map and then store your data and use a function to operate on this structure.
When you stop and think about it, why would I need to put something in a class for encapsulation to protect it's data when the more functional you go all data is immutable? Encapsulation is really there to guard against random changes. But if everything is immutable then what is the need for encapsulation? Also why use inheritance when in a functional language you can use higher order functions and get all of the abilities of the function without overriding or changing the behavior of another function?
True someone may say well I may want to use OOP and functional features just cause which is a person's choice. However if you really wanted a true I would carefully say OOP or currently accepted "OOP" solution than you're really not structuring your code in a way that would serve a purpose to a functional programmer or vice versa. Even though languages like Scala allows one to do this to mix and match features as much as you want, there is a reason why for example on the JVM Java and Clojure are more popular or used the Scala. Because one solution really requires a very different mindset, features, and values than the other in order to problem solve.
Lol in my case I think more functionally so I would choose something like Clojure than Java and I wouldn't try and use something like Scala to mix and match.