16 Jan · 6 min read
When it comes to programming, there are many different styles and paradigms. Now there are many programming languages and it is always hard to choose between them. We will take a look at two of the popular programming paradigms - functional programming and object oriented programming.
The functional programming paradigm has its roots in mathematics and is language independent. Functional programming might be described as a way of thinking about software development where you create pure mathematical functions - here where its name comes from. This is a programming paradigm where everything is built in pure mathematical functions. Its key principle is the execution of a series of mathematical functions.
Functional programming uses declarative programming style. This style is focused on the question “what to solve” unlike the imperative style that is focused on “how to solve”.
In functional programming expressions are used instead of statements. An expression is evaluated to produce a value whereas a statement is executed to assign variables. Based on the concept of mathematical functions, functional programming uses conditional expressions and recursions in order to perform calculations. Functional programming avoids the concepts of shared state and mutable data. The functions are meant for specific computation, not for the data structure as data is loosely coupled to them. Functions can be replaced with their values without changing the meaning of the main code.
Value output in functional programming depends only on the arguments that are passed to the function, unlike other types of programming where it depends on a local or a global state.
We can state that functional programming methods are focused on the end result, not the process itself. Functional programming uses immutable data and decomposes the whole problem into mathematical functions. One important note that needs to be marked is that functional programming does support iteration like loop statements and conditional statements like if-else.
Functional programming offers advantages such as bugs-free code, parallel programming, nested functions, etc. You will see them described below:
First, functional programming does not support state, which means that there are no side-effect results and software developers are able to create error-free codes.
Second, functional programming languages have NO Mutable state, so there are no state-change issues.
Third, functional programs consist of independent units that can run concurrently. This makes functional programming written software more efficient.
Forth, functional programming supports Nested Functions - these functions access and modify variables that are defined in their parent functions.
Fifth, functional programming offers hot code deployment and fault-tolerance, which prevents network, compute and other hosting issues from affecting application operation
Last, but not least - functional programming offers better modularity with a shorter code.
As a downside, functional programming requires a large memory space. As it does not have state, you need to create new variables every time to perform actions. It also needs lots of mocking and extensive environmental setup
Functional Programming could be used in situations where we have to perform lots of different operations on the same set of data.
Lisp is used for artificial intelligence applications like Machine learning, language processing, Modeling of speech and vision, etc.
Functional programming paradigm is not easy to understand, this is why not many developers are putting effort into getting into functional programming in the start of their careers.
Reusing code is not as easy as it is in object-oriented programming
Object-oriented programming organizes software creation around data and objects, rather than around functions as it is in functional programming. In object-oriented programming, the program is written as a collection of classes and objects which communicate with each other. The most basic entity is called an object and it performs all kinds of computations. The object also could be defined as a data field that has unique attributes and behavior.
Object-oriented programming is focused more on the objects that must be manipulated than on the logic that is required to manipulate them.
The organization of an object-oriented program also makes the method beneficial to collaborative development, where projects are divided into groups. Additional benefits of OOP include code reusability, scalability, and efficiency.
If you are wondering what type of programming paradigm to choose for your next product, here are the pros and cons of object-oriented programming:
First, objects are enabled to be self-contained. This makes troubleshooting and collaborative development easier. This is done thanks to the modularity of object-oriented programming.
Second, code could be reused through inheritance - this way your team does not need to write the same code multiple times.
Third, software developers can implement system functionalities independently in object-oriented programming,
Fourth, descriptions of external systems are simple due to the passing techniques used for communication between objects.
Fifth, by using encapsulation and abstraction the complex code is hidden. This protects the software maintenance and internet protocols and makes the written code more secure.
Last, but not least -object-oriented programming enables a single function to adapt to the class it is placed in. This means that different objects can pass through the same interface.
The object-oriented programming model has been criticized for multiple reasons. Here are some of them:
Object-oriented programming does not focus enough on computation or algorithms. It overemphasizes the data component of software development. Compared to functional programming, object-oriented programming involve more lines of code. Sometimes the software that is created via object programming might work slower as it will need more instructions to be executed. Overhead might rise if the software is left out of control - this could create a massive amount of bloated, unnecessary code - this also might increase the costs of maintaining the software. Object-oriented programming written software might use more CPU than alternative options. This is why it shall not be chosen when there are technical limitations in the future use of the software.
What is the difference between FP and OOP?
Now that we have taken a quick look at functional programming and object-oriented programming and their pros and cons, here is a simplified comparison between both of them.
|Functional programming||Object oriented programming|
|Immutable data||Mutable data|
|Functions do not have side-effects||Produces side-effects|
|Answers “What to solve?”||Answers “How to solve it?”|
|Supports parallel programming||Does not support parallel programming|
|Declarative programming model||Imperative programming model|
|Flow is controlled by functions calls and function calls with recursion||Flow is controlled by loops and conditional statements|
|Supports "Abstraction over Data" and "Abstraction over Behavior||Only supports “Abstraction over Behaviour”|
|Uses "Recursion" concept to iterate Collection Data.||Uses the "Loop" concept to iterate the Collection of Data|
This is where your expertise should shine. In order to choose some of the programming paradigms you need to know them in detail. Then you need to prepare your product/project/software roadmap. You need to know in depth the problem that your product will solve and what it will need to be maintained.
Knowing these main things will help you determine whether you need to choose functional programming or object-oriented programming for your project.
Both paradigms work well in their own way. There are fields where functional programming is preferred such as fintech as there you need everything to happen as fast as possible and to be able to maintain your system while it is up and running. Another field of business that would benefit from functional programming is bioinformatics. There you store and analyze massive amounts of data and you need real-time data processing functional languages such as Elixir and Erlang will come in handy.