Multi-Paradigm is the new OOP

simon rebiere
24stechblog
Published in
5 min readFeb 22, 2021
Photo de Christina Morillo provenant de Pexels

Intro:

Nowadays one of the most used programming paradigms is the Object Oriented one. It’s considered to be one of the simplest to learn and understand for beginners as its syntax is a very descriptive one.

Functional programming on the other hand, is more of a niche way of programming, as it is considered to be harder to write and understand, although it has been around for more than 60 years and the theory behind it is even older than programming itself.

We’ll see here that functional programming is not that complicated. In fact it is so little complicated that most of the recent programming languages are almost all multi paradigm, and that the features they add can make your life, as a developer, much easier, by reducing the amount of side effects and improving readability.

1.1/ What is Object Oriented Programming

First we need to talk about imperative programming which is a way of writing code where you describe how the code should operate. This means that code is written as a sequence of instructions to be executed in a specific order. The primary advantage of writing code that way is that you decide when to change the state of your program.

Object Oriented takes this way of writing code and adds Objects, which are real-world objects represented by state variables and methods. Object Oriented Programming has many advantages and many uses. Nearly all the front-end software that we use are conceived according to this paradigm, because it tends to be modular and easily scalable, provided you are using the right architecture for your project.

To write these programs we create objects which we will use to monitor the state of the program. But as the program grows in size, scope and number of features, more and more objects will start to interact with these state variables. This makes the complexity of the project grow exponentially, thus the dream of modularity and scalability starts to take a hit.

1.2/ The limits of OOP (Object Oriented Programming)

The limits of OOP should be getting clearer. The more code, features and developers we add to a project, the more the interaction between objects and state variables will get complex. This complexity often leads to the multiplication of side effects.

Side effects are an essential part of OOP, as they allow functions and objects to change another state variable. Every time a function or any entity interacts with a variable outside its local scope, it is a side effect.

This makes side effects an essential feature in OOP. Still they are also the most common source of bugs in every program: as programs get bigger and more complex, handling and remembering every side effect in the code becomes an impossible task, leading to state variables being misused.

Another essential part of OOP is multi-threading. It’s a great way to use all the computation power of your device, as it allows the machine to run multiple actions simultaneously! But also, a great way to create concurrency on data treatment, deadlocks and other problems.

Add to that the inevitability of interdependence between parts of your program that will appear as your project grows and evolves, and it will result in a code that is not that modular nor scalable.

In other words as Joe Armstrong, creator of Erlang, said: “Because the problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle. “

2.1/ Functional programming

As we started by explaining what imperative programming is, we will now follow up by introducing declarative Programming which is a way of writing code in which you describe what the code should do instead of how it should do it. A simple example of this would-be HTML in which you describe what the page is (title, text, etc..) and not how it should be handled.

(Note that HTML is closer to descriptive programming, but it conveys the idea in a manner understandable by everyone)

We then need to add a few principles to this:

  • Function should be considered a result of mathematical expressions
  • No state variable, no mutability of data structure
  • Usage of pure functions, meaning functions that only interact with what is inside their scope, and which for a given parameter will always return the same result
  • Usage of a lot of high-order functions, these are functions that takes parameters and lambdas in order to be executed, such as map, filter, etc…

2.2/ Functional programming benefits

All these principles applied correctly will bring lots of advantages codewise. Your program should be faster to compile or execute since there is no more state variable to slow down everything. It will make your program less likely to have side effects since we do not change values of state variables and this will remove the threat of concurrency in data treatment.

It also makes your code way easier to read since not having to search if there are interactions between models, classes and structures avoid losing a lot of time. The same applies to pure functions as they only interact with their scope, you have nothing to read, and it makes unit testing way easier.

Then, why do we even OOP ?

No, functional programming is not the ultimate answer to all your programming debates. As with anything programming related, it serves a purpose: its goal was and still is data treatment. Please do not try to handle your UI life cycle with functional programming.

But please do try to keep these principles in mind so you can improve your code little by little.

3/ Stop doing ! Start doing !

Here are some very basic examples of small modifications you can probably perform in your code.

First thing you need to stop doing is using loops to transform, parse, or modify an array. There are plenty of high order functions that are made for that purpose, such as map, filter, reduce, zip, contains, etc…

Now to avoid having too much side effect and improve readability you can start implementing pure functions.

To go further:

https://www.freecodecamp.org/news/an-introduction-to-the-basic-principles-of-functional-programming-a2c2a15c84/

https://matteomanferdini.com/swift-functional-programming/

--

--