DEV Community

Cover image for The two universes of programming! OOP and FP!
Adnan Babakan (he/him)
Adnan Babakan (he/him)

Posted on • Updated on

The two universes of programming! OOP and FP!

Hey there DEV.to community!

A lot of people getting into programming hear two words really often! OOP and FP! Which OOP has the advantage in their mind since it is heard more often.

Here I will explain what these two things are and which one you should learn!

Confused

OOP or FP? Which one should I get?

Programming paradigm

Each and every programming language is really unique. Although they might seem the same when looking at them on the surface, getting deeper in them reveals fascinating features which might not be available in others.

Being different in features is not the only difference in programming languages and the procedure a programming language follows to execute your code and the way it treats it is called a paradigm. A paradigm defines the structure of a programming language and how it gets executed.

There are some paradigms designed so far but two of them are the most popular ones, Object-Oriented Programming (OOP) and Functional Programming (FP).

Object-Oriented Programming

If you are designing websites or desktop applications most probably you are using an OOP language, like C++, Java, JavaScript, Ruby, PHP. There are some concepts these programming languages have in common which makes them appear in OOP category.

Simply put, object-oriented programming requires you to define everything as an entity using a syntax called class.

For instance here is how you define a dog in Java!:

class Dog {

}
Enter fullscreen mode Exit fullscreen mode

Every class can have properties (states) and methods so it completes the definition of your entity:

class Dog {
    public String name;
    public String breed;

    public void bark() {
        System.out.println("Woof Woof!");
    }
}
Enter fullscreen mode Exit fullscreen mode

the code above can be rewritten in PHP:

class Dog {
    public $name;
    public $breed;

    function bark() {
        print("Woof Woof!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Or in JavaScript:

class Dog {
    name
    breed

    bark() {
        console.log("Woof Woof!")
    }
}
Enter fullscreen mode Exit fullscreen mode

In most OOP languages variables are mutable, meaning that you can change the value later:

let x = 5
console.log(x) // 5
x = 8
console.log(x) // 8
Enter fullscreen mode Exit fullscreen mode

As you can see, these are highly similar since they are all classes in an OOP language. An entity can be instantiated to form an object and here is where OOP programming finds its meaning.

OOP paradigm defines these concepts all in all:

  • Abstraction: Reducing the complexity of a program.
  • Class: Defining the structure of an entity.
  • Encapsulation: Combining the data to form a new one.
  • Information hiding: Hiding the unnecessary data to reduce the complexity.
  • Inheritance: Defining the relationship between classes.
  • Interface: Using the hardware input and output and other programs.
  • Object: An entity derived from a class.
  • Polymorphism: The ability to perform multiple tasks and appear in multiple ways.

Although these words might seem strange, believe it or not, they are just harsh words to define simple things. Once you get into programming you'll realize how these words are simple and what they mean.

Functional Programming

Functional programming is different than OOP in most cases. Both paradigms have functions and variables but they treat them differently. So don't let the similarities get in the way of differences. There are many FP languages like Elixir, Erlang, Elm, Haskell, F# and etc.

Interestingly, some OOP language such as JavaScript, Python and PHP support FP concepts relatively which means you can implement an FP procedure in them.

A functional programming language is purely performing on functions and there are no classes and objects in it usually. A functional programming language tends to adopt a mathematical approach so the variables are immutable. Just like in mathematics when a variable is defined and its value cannot be changed.

If you think immutability is a restriction and it is impossible to work with such a programming language, well you are wrong! This way of programming helps you stabilize the mathematical way and that's why functional programming languages are the best if you are building something related to calculus. This doesn't mean you cannot use FP for anything else but rather was a suggestion.

Just like OOP where we defined the concepts, here are the concepts that FP consists of:

  • Immutability: The inability of changing the value of a variable.
  • Pure functions: Functions have no side effects.
  • Recursion: Recursion is a function that calls itself.

In case you are wondering how these work, check out my post below: about Elixir:

When to use OOP or FP?

The hardest question ever is which programming language to use and harder than that is which paradigm to use!

OOP and FP both have advantages and disadvantages according to the situation and how you want to solve your problem. OOP is the most used paradigm these days especially.

But here is a suggestion if you want to choose one: If you are looking for a programming language to design websites or desktop apps go with OOP. If you are looking for a concurrency and a more mathematical way of programming (especially if you are a data scientist) you'd choose FP. Again, I'm mentioning that these are my ideas for where OOP and FP fit best and it is up to you what to do.

I hope you enjoyed!

Top comments (13)

Collapse
 
_hs_ profile image
HS

More like class oriented vs FP. Take a look what Alan Kay wrote about OO after classes happened. He regrets calling it that way now and more focuses on some FP as actual OO with of course some side effects. Also creator of Erlang (known as FP) in 2006 (find youtube video) called it real OO and referenced Kay's statement.

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi
I will check these things you've mentioned out for sure, thanks. But all in all, I think this is how OOP and FP are considered right now.

Collapse
 
_hs_ profile image
HS

Sure, I think it doesn't matter what creator meant but how people ended up using it. Just fun thing about computers, it's usually different than expected. Like did Unix people intended for C to be this big? No, they intended Unix to be the main thing but they both got in history. The Sims game was started as tool for architects and look where it ended. Fun I guess

Thread Thread
 
adnanbabakan profile image
Adnan Babakan (he/him)

Well, I did know about all the computer stuff you mentioned more or less. But about Sims? No way. How come? LOL xD :))))

Thread Thread
 
_hs_ profile image
HS

Yup, Will Write was working on a tool for 3D modeling because of architects or such, you can research more but I remember something about it written when first Sims was out

Collapse
 
erikpischel profile image
Erik Pischel

I I think that you explained some concepts incorrectly. encapsulation for example, wikipedia defines "encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components."

polymorphism isn't about performing multiple tasks. it's about variables referencing objects that do not have the declared type but another (often derived) type.

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi
Thanks for your comment
I think we are both on the same page

Collapse
 
610yesnolovely profile image
Harvey Thompson • Edited

Great summary of the two different paradigms.

Personally I'd recommend starting with OOP (or even imperative/declarative), because we often think about "Nouns" (class) and "Verbs" (method), and our minds are good at breaking down problems into categories and hierarchies.

However, one should also learn functional program - even in a non-functional language. The reason is that computer power is still increasing exponentially, but now that power comes from having multiple CPUs and threads.

Working with immutable data (see also persistent data structures) in functional languages with multiple threads/CPU is much easier to reason about, compared to working with mutable data, threads/locks (though knowing about such things is very important).

Modern programming languages are typically multi-paradigm, and we're seeing more and more functional ideas being applied in historically OOP languages.

So, learn both :-D

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi
Thanks for your comment
Yes OOP is good to start with but learning FP opens a new universe in a programmer's mind due to immutables and so.

And yes I love how OOP langauges are adopting FP features.

Collapse
 
scilari profile image
Ilari Vallivaara • Edited

Where do you get that FP could not use objects? For example, even Haskell has objects; an (immutable) class is one (good) way to define a data structure or a mathematical object, such as a complex number. These are perfectly usable by pure functions and FP in general.

I think this post kind of misses the point of these two paradigms by treating them as opposite and non-overlapping sides of programming. This is mainly due to not recognizing the key aspects of OOP and FP. Why isn't FP compared to imperative programming and OOP to something that does not bundle the functionality and data, instead?

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him) • Edited

Hi
Thanks for your comment
I know that OOP and FP can have many things in common. I just wanted to give a general idea about these two and the general thing about FP is that they don't use classes and objects. I will try to write an article more detailed about imperative and declarative programming later.

Collapse
 
sjf3 profile image
Sam Fleeman

You should look at golang. It's a different kind of OOP. No classes. You get class like behavior from having struct or struct pointer function receivers. No inheritance either. You have struct embedding and implicit interface fulfilment to accomplish similar things. Go also has first class functions so while not being optimized for tail recursion you can do some functional things. Look up Dave Cheney's functional options blog post/talk for an example of first class functions providing OOP like data encapsulation. My point is java style OOP is not the only way to OOP and not forcing you into making everything an object is actually less verbose. Sometimes you just need a utility function it's not an object it's not even related to an object. GetTerraformPluginDir( ) for example. It's different on windows or mac/Linux. The function has to determine the OS and build a string I don't need a class for this and I might want to reuse this logic across multiple "classes"

Collapse
 
adnanbabakan profile image
Adnan Babakan (he/him)

Hi
Thanks for your comment
Yes, I know there are other approaches to OOP. And I've also written an article about Go which you can check it out here: