The two universes of programming! OOP and FP!
Tags: javascript, elixir, functional, oop
Published: 2020-07-01 05:40:10

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](https://media2.giphy.com/media/3jN3GziOKUEmI/giphy.gif?cid=ecf05e47370b178140be7bf56d9911337bb6a29b2141edd1&rid=giphy.gif)
<figcaption>OOP or FP? Which one should I get?</figcaption>

# 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!:
```java
class Dog {

}
```

Every class can have properties (states) and methods so it completes the definition of your entity:
```java
class Dog {
    public String name;
    public String breed;

    public void bark() {
        System.out.println("Woof Woof!");
    }
}
```

the code above can be rewritten in PHP:
```php
class Dog {
    public $name;
    public $breed;

    function bark() {
        print("Woof Woof!");
    }
}
```

Or in JavaScript:
```javascript
class Dog {
    name
    breed
    
    bark() {
        console.log("Woof Woof!")
    }
}
```

In most OOP languages variables are mutable, meaning that you can change the value later:
```javascript
let x = 5
console.log(x) // 5
x = 8
console.log(x) // 8
```

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:

{% link https://dev.to/adnanbabakan/you-should-learn-elixir-here-is-why-4i94 %}

# 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!