The goal of writing this article 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.
Before I proceed with this article though, I will like to clear some expectations: This article is not for a complete beginner. A basic understanding of any programming language is needed.
With that said, 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:
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.
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.
Both functional programming and object-oriented programming have the same goal, which are:
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 |
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:
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:
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.
That’s it guys, reach out if there’s anything you think I should work on.
Cheers and happy coding!