DEV Community

Cover image for FP vs OOP ? I choose both !

FP vs OOP ? I choose both !

Meidi Airouche on October 05, 2023

Often, programmers consider Object Orientation and Functional Programming to be mutually exclusive forms of programming. Usually, two camps clash ...
Collapse
 
fabricehategekimana profile image
fabriceHategekimana

Hi, I love your post ! You have a really deep understanding of both paradigm and you changed my point of view. I have my two cents on this topic:

Of course people will say OO is closer from "real life objects" and "the way we think".

Yes, I was thinking the same until I discovered algebraic data types from FP which are a more expressive way of representing things. Nowadays I don't think in term of classes and objects (which are limiting) but in term of types

In most software systems when one function calls another, the runtime dependency and the source code dependency point in the same direction. The calling module depends on the called module.
However, when polymorphism is injected between the two there is an inversion of the source code dependency.

To be honest I didn't understood this part. To be more precise which kind of polymorphism (generics, ad-hoc, etc.) is used there ? Or is it about late binding that let some flexibilities in the run time creating a more dynamic program ?

Finally I am also using both paradigms: FP for modeling data and functions and OOP for protocols and communication between entities

Collapse
 
mairouche profile image
Meidi Airouche Onepoint

Hi,

Thanks a lot for your feedback. I'm glad you share the same vision.
I should probably add an exemple for the dependency part. I would add this example below, what do you think ?

Let's consider two classes: Service and Client.

In a system without polymorphism, Client directly calls a method on Service. Client directly depends on Service both in the source code and at runtime.

If we introduce an interface ServiceInterface, which Service implements, Client no longer depends directly on Service. Instead, Client depends on ServiceInterface. Now, the source code dependency is inverted: Service depends on ServiceInterface, and Client depends on ServiceInterface, but not directly on Service. However, at runtime, Client will still depend on an instance of Service, or another class that implements ServiceInterface.

Collapse
 
fabricehategekimana profile image
fabriceHategekimana

Thanks, that's a great example. I better understand