Object-oriented programming vs Functional programming

Claudynn Lee
3 min readSep 5, 2021
Photo by Mohammad Rahmani on Unsplash

Functional programming has been around since the late 1950s but recently became popular in development culture with the introduction of NodeJS. Object-oriented also started around the same time and has been adopted in many modern-day frameworks.

Each of these is a programming paradigm with its own unique advantage over the other. We will go over a bit of history, key concepts, and the types of problems they solve with examples.

We start with object-oriented programming. It was developed by Alan Kay circa 1966 or 1967 while at grad school. Kay wanted to create a way to share data without getting it from the source so he suggested sharing by message passing. A few key concepts include inheritance, polymorphism, and encapsulation. Using these concepts we are able to create classes that contain state. Inheritance allows us to derive a class from another class. For example, if you had a base car class you can create a new subclass that inherits from the base car class called Truck class. This prevents duplication of code but can get complicated very quickly. Polymorphism is similar to inheritance in reducing code duplication by reusing classes and functions. Finally, encapsulation is like the glue to both of these concepts. It refers to the bundling of data and related functions into a single unit or class.

Moving on, we will now be looking at functional programming concepts and how it works in comparison. Unlike object-oriented programming, functional programming was derived in academia, specifically, lambda calculus which is a formal system of computation based purely on functions. Currying sounds spicy and in fact, it is. It is a powerful tool that allows developers to break up multiple argument functions into a series of single-argument functions. It is particularly helpful in situations where all the parameters of a function are not yet available, allowing the program to run through other operations. Closures are another popular concept and use the scope of which variables have access to within code blocks. For example, a closure gives you access to an outer function's scope from an inner function.

Now that we understand a bit of the basics behind object-oriented and functional programming, we can look at a simple example of how they work in comparison with each other.

Object-oriented Programming

We will be creating a class to create a car object with a few getters and setters.

car.js

After we have created our class, we can now create a new car object in our main program by passing in parameters to define its properties. The result of running this program is a new silver sedan. To access these properties we have to reference the object itself where the data is stored.

main.js

We can easily change the properties of the car by using our setters.

newCar.setColour("blue"); 

The result of running the program now is a new blue sedan.

Functional Programming

Creating the car ‘object’ is a bit different in functional programming. Using currying we can simply pass in the new car’s properties one by one until we are happy with the type of car we have. There is no need for a separate class or storing state for the properties of the car and therefore the data is deemed immutable.

car.js

To create a blue sedan we would need to create an entirely new car object with a new colour since we are not able to access the properties of the object.

I’m not trying to sell one over the other but I do believe that in order to use each of these programming paradigms to their highest potential you must understand how they solve problems and if that is what you need for the problem you are trying to solve. Like a mentor once told me, fit for purpose and if there’s anything I want you to take away from this article, it’s this. State vs stateless, mutability vs immutability, and objects vs functions. Thank <you/>.

--

--

Claudynn Lee

Entrepreneur, amatuer writer, fitness enthusiast and software developer. Fresh perspectives coming at you every month. Watch this space 🔥