Functional vs Object-Oriented Programming. Pros and Cons.

Farida Aliyeva
4 min readNov 27, 2020

--

The battle between Functional and Object-Oriented programming is similar to Black or White? Coke or Pepsi? Samsung or iPhone? disputes. This kind of dispute is tough to resolve as it is indeed a matter of preference. However, it is worth preparing a pros and cons list of both programming styles. Therefore, in the following blog post I and going to shed a light on both of the programming styles and how they are different from each other.

Functional Programming

FP is a programming technique that is well known for its programs to be constructed by deploying functions. The main concept of FP is that it attempts to avoid changing state and mutable data. In a functional program, the output of a function should always be the same, given the same exact inputs to the function. Why is it so? Well, it is because the output of a function relies on its arguments. Let’s make it clear if you call a function subtract() it will return the difference between the values and in case you give the same input a and b, you will always get the same result.

Functional programming supports various programming languages such as Lisp, Wolfram, Erlang, Haskell, F#, and others. It is also well suited to work in the data related scientific sector.

Here are some of the well- known advantages FP has: efficiency, lazy evaluation, nested functions, bug-free code, parallel programming.

Object-Oriented Programming

Object-oriented programming technique is essentially based on the concept of object which contains data in the form of attributes and code in the form of procedures (methods). The attributes in the objects are manipulated through methods that are given to that particular object. Example:

Example code.

The key aspect of OOP is the ability to encapsulate data from outsiders. Encapsulation is the ability to hide variables within the class from outside access — which makes it great for security reasons, along with leaky, unwanted, accidental usage. OOP uses several techniques from previously established paradigms, such as modularity, polymorphism or encapsulation. Many popular programming languages such as Java, C++, Python and PHP support object-oriented programming. In OOP each object resembles the independent small machine with a separate role. Object programming is intended to promote greater flexibility and ease of development in programming and is very popular in large scale software development.

Differences

Comparison between FP and OOP.

Referring to the picture above we get familiar with the key differences between FP and OOP. Shortly, both of the programs have two basic components, data — what the program knows — and behavior — what the program can do with that data. So object-oriented programming states that combining data and related behaviors in one place, is called “object”, which makes it easier to understand how a particular program works. On the other hand, functional programming argues that data and behavior are different things and should be separated to ensure their clarity.

Pros and Cons

Everything in this world has both advantages and disadvantages, so it is worth knowing them before actually using something. Once we defined both FP and OOP we can discuss them.

Object-oriented languages work well when you have a fixed set of operations on things, and as your code develops, you mainly add new things to it. This can be achieved by adding new classes that implement existing methods while leaving the existing classes to themselves.

Functional languages work well when you have a fixed set of things, and as your code develops, you mainly add new operations on existing things. This can be achieved by adding new functions that perform calculations using existing data types, and existing functions are left to themselves.

While working across different boundaries, OOP is an excellent choice to keep everything packaged up and secure from unwanted external usage. Whereas, Functional programming works well when complexity is contained.

FP Pros and Cons

+ Using clean and transparent functions leads to reliable results without side effects that deliver and return exactly what you expect.

+ It uses a more declarative style that focuses more on what needs to be done and less on how to do it, with an emphasis on efficiency and optimization.

– It is a relatively new paradigm and sometimes it is not so easy to find documentation or information compared to Object-oriented Programming.

– Sometimes it may not become illegible due to a very large number of functions compared to Object-oriented Programming.

OOP Pros and Cons

+ Objects and methods are very clear and understandable.

+ Use an imperative style, in which the code is read like a simple set of instructions, just like a computer would read it.

– May lead to unspecified and unexpected results in the sense that a parallel code that would have access to a common resource could do so in a way that produces unspecified results.

– Its methods can have side effects and may put an impact on processors.

All in all, it is always up to the programmers which concept to choose to make the development process as productive and easy as possible. But from my personal view, it mostly depends on the requirements of the project.

Thank you for your time :)

References:

https://sdacademy.dev/difference-between-functional-programming-and-object-oriented-programming/

--

--

Farida Aliyeva

Data Scientist at SDH | MS Graduate in Computer Science and Data Analytics