Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 1, 2020 05:40 am GMT

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 {}

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!");    }}

the code above can be rewritten in PHP:

class Dog {    public $name;    public $breed;    function bark() {        print("Woof Woof!");    }}

Or in 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:

let x = 5console.log(x) // 5x = 8console.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. A functional programming language tends to adopt a mathematical approach so the variables are immutable. Just like in mathematics when a variable is defined 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!


Original Link: https://dev.to/adnanbabakan/the-two-universes-of-programming-oop-and-fp-5504

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To