Functional vs. Object-Oriented Programming Paradigms. How are They Different?

Ashab Ahmed
5 min readOct 20, 2020

This article will briefly introduce and describe Functional Programming (FP) and Object-Oriented Programming (OOP). Moreover, this article will compare the two paradigms to see some differences between amongst one another. Hopefully, this article may help you recognize which programming approach you will want to lean towards.

Object-Oriented Programming (OOP)

For a quick definition let’s look to Techopedia’s definition on their site: “Object-oriented programming (OOP) is a software programming model constructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods).” (https://www.techopedia.com/definition/3235/object-oriented-programming-oop). In other words, OOP performs with the concept of “objects” containing data in the form of attributes. You can manipulate the data or its behavior by use of methods or functions.

To illustrate, let’s say we have a Car object that holds details about itself: color, speed, weight, model, type, etc. These details are the Car object’s attributes and describes what the Car object is. The Car object’s functions describe what the object can do: drive, brake, speeding, parking, etc. Referring to the specific methods the Car can attain based on the attributes provided. Thus, OOP makes easier to understand a program since all of the data and its relative behavior is stored in a single location, an “object”. A highlight of OOP is the notion that you can model your program to the real world, thus making it easier to think about how to write your code and work with the data.

OOP has four key principles (Scand, 2020):

  1. abstraction (focusing on the essentials and hiding the unnecessary details);
  2. inheritance (defining one class in terms of another);
  3. polymorphism (combining elements to create a new entity);
  4. encapsulation: (it allows for hiding unrelated data from users and prevents unauthorized access).

OOP is promoted due to the design of simplified programming. OOP makes it easy to refactor, maintain, and have efficient code.

Functional Programming (FP)

Wikipedia’s defines Functional Programming as: “In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements which change the state of the program.” (https://en.wikipedia.org/wiki/Functional_programming).

To elaborate, FP works under the design of relying only on composing functions to develop programs, more specifically, pure functions. Pure functions are functions that always provide the same output when given the same input (Elliott, 2016). All objects in FP are immutable objects, meaning once a thing is created then it can’t change, making it “pure”. The data passed in as parameters to pure functions don’t get altered, meaning it can’t change the external state. Furthermore, the functions don’t rely on variables outside the their scope. There is no shared state in FP, meaning objects don’t share scopes with each other. FP operates with the idea that data and behavior are two different entities and should be separated for more clear and concise code. Simply put, FP requires functions to execute all tasks of the application. For every little task, there will be a small function that does that part and only that small job. This allows code in FP to be very clean, bug-free, and reusable.

FP has six main concepts (Scand, 2020):

  1. higher-order functions (HOF);
  2. purity;
  3. recursion(function calls on itself until it meets condition);
  4. referential transparency (an expression may be replaced by its return value without changing the application).
  5. strict and lazy evaluation;
  6. type systems (rules of variables and expressions).

FP works well when state is not an issue and if there’s almost no involvement with variables and mutable data.

Main Differences

As you can see, these two programming paradigms are quite different and work well in different situations. Some key differences can be highlighted here:

Source: https://scand.com/company/blog/functional-programming-vs-oop/

Some of the differences between FP and OOP are in direct opposition with each other. For instance, FP is used when there are fewer things with more operations, whereas OOP is used when there are many things with few operations. Another contrast is OOP welcomes mutable data and utilizes it well. On the other hand, FP only desires immutable data. One more main difference is that in OOP the order of statements has to be executed particularly but FP the statements can be executed in any order.

Couple of disadvantages for each paradigm

Some of the disadvantages of OOP is that many times it’s not easily reusable. The functions and methods of one class don’t work for another class. Thus, it can be considered inefficient if you have many classes that have their methods that only work with one particular class. This can make situations more complex and often leads to writing more code than you have to. For instance, writing an entire method to modify one attribute only once.

With FP, it’s difficult to develop the mindset to think of the real-world from the lens of creating functions. It’s really not possible to approach real-world scenarios from a simply data manipulation standpoint unless you have been practicing for a while. Thus, making it hard for beginner developers to adhere to the methodology of FP.

Conclusion

Currently, there is a major debate going on in the tech scene about which is a better paradigm for coding, Functional or Object-oriented. In my opinion both programming designs have their merits and their weaknesses. It’s hard to directly point to one and say “that one is better”. It seems the norm is that OOP developers claim OOP is better and FP developers claim that FP is better. At the end of the day, both paradigms have the aim of creating bug-free and understandable applications, but with different approaches. Choose whatever style you feel you are better suited for. This article is only meant to provide a brief overview of the two paradigms. For more in-depth descriptions you can visit the resources listed below.

References:

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

--

--