Object-oriented Programming vs. Functional Programming Which Is Better?
The goal of this tutorial isn’t to deliberate or talk about which is better between Object-oriented Programming and Functional Programming but it is about figuring which choice is better for your project.
let’s dive in and learn about object-oriented programming and functional programming.
For this topic to be more intuitive, we look at the various concepts listed below:
- What is object-oriented programming (OOP)
- What is functional programming (FP)
- What do functional programming and object-oriented programming have in common?
- What is the difference between functional programming and object-oriented programming?
- What Makes a Function “Good”?
What is Object-oriented Programming (OOP)?
According to Wikipedia, Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).
It simply is a way of bringing data together in a single location called an object, which makes it easier for understanding how our program works.
What is Functional Programming (FP)?
Functional programming is all about separations of concerns which object-oriented programming does as well. It’s just a way of making sure code does one thing it’s good at. They care about data and its behavior.
What Do Functional Programming and Object-oriented Programming Have In Common?
Both functional programming and object-oriented programming have the same goal, which are:
- They both help us with clarity in our code
- They are easy to extend
- They help us to maintain our code
- They provide good memory efficient
- DRY(Don’t repeat yourself)
The Difference Between Functional Programming and Object-Oriented Programming
|Functional Programming||Object-Oriented Programming|
|They perform many operations of fixed data||They perform operations on common data|
|Their states are immutable||They are stateful|
|Functions are pure and have no sides effect||They cause side effects|
|They are declarative about what they do||They are more imperative about how we want things to be done|
What Makes a Function “Good”?
Functions play a very vital role in our programs; without it, we won’t be able to write better programs.
What exactly are the qualities of a good function? Well, there are a few of them that are most significant if you want to write better programs:
- Task: they perform one task at a time
- Predictability: a quality function must be predictable, we always expect the same input no matter the output
- Composability a quality function must be composable
- Return statement a quality function must have a return statement
- Shared state a quality function does not share states.
Which Should I Use: Functional Programming or Object-oriented Programming?
Well, firstly it’s important to know that both of them have the same goal, which is to help us write better programs.
But of course, there will always be a time when you’d prefer one over the other.
My recommendation is to learn both functional programming and object-oriented programming.
How can you make a choice on which you should use?
I’m going to be going through the main concepts behind them - composition and inheritance.
| Inheritance: they are a superclass that is extended to smaller pieces that adds or overwrite thing. This concept is used by object-oriented programming, classes are structured based on how things are, and lastly, they are tightly coupled together.
Composition: they are smaller pieces used to create something bigger e.g compose used by functional programming. |
It doesn’t mean though that object-oriented programming is inheritance and Functional programming is composition, NO! You can do both things in both paradigms, however, functional programming tends to focus more on composition and object-oriented programming on inheritance.
There are some issues with object-oriented programming though:
- They are tightly coupled
- They can break our code
- There is a hierarchy problem - this problem arises when we start sharing methods
- They inherit functions that are not really necessary for use e.g imagine your request for a banana and then you get a gorilla holding a banana
Inheritance is not always bad, but because things might change in the future we want to stick to the composition.
In functional programming, codes are essentially a combination of functions and data is immutable which leads to writing good programs with no side effects and pure function. In functional programming, you can’t change the outside world and the output value. Functions simply depend on the given arguments. This allows functional programming to really have control of program flow; the last functions are first-class citizens.
|“ At the end of the day, both are good.”|
The advantage of each paradigm is simply in the modelling of your algorithm data structure the choice of which you use is simply what makes more sense for your project and the language that you are using.