Cover image

Programming Paradigms: Procedural vs. Object Oriented vs. Functional

September 1, 2021

4 min read

Introduction

Copy heading link

Programming paradigms refer to the style of programming you choose to implement in your codebase. The paradigms are a way to logically write and structure your code. It is not a strict set of rules set by your browser, IDE or compiler, however, certain languages or frameworks may be opinionated about which paradigm is preferred. We will be exploring three paradigms with working examples and comparisons. Firstly, a general overview of each paradigm:

Procedural

Copy heading link

Procedural programming involves running a set of instructions in sequential order. This type of programming is the simplest form of programming and can be often referred to as scripting. When I first learnt JavaScript to manipulate the DOM, this was the quick and easy solution to make changes to an HTML page.

Object-oriented

Copy heading link

Object Oriented Programming or OOP is the concept of reasoning your code with the real world. Entities are represented as objects that have properties (its attributes) and methods (its actions). This is the first paradigm I learnt going to university and it is usually the first concept that you need to wrap your head around when starting with programming. This style of programming is prominent in Java and C#.

Functional

Copy heading link

Functional programming is kind of the new kid on the block. It is the idea of organising your code into pure functions that take input parameters and return a result. The idea of this approach is to keep your code as predictable as possible by not omitting side effects in your functions. This style of programming I picked up mostly while learning the React framework. It is the most prominent with React and the idea of reusable functional components. Javascript's array methods such as .map(), .reduce(), and .filter() are prime examples of taking in an argument and producing an output.

Comparison

Copy heading link

To quickly demonstrate how all three paradigms are used, I will show a very basic use case for creating a new user in JavaScript.

Procedural

Copy heading link
JavaScript

Object-oriented

Copy heading link
JavaScript

Functional

Copy heading link
JavaScript

Final thoughts

Copy heading link

As you can see, all three paradigms offer vastly different ways of structuring your code. In the end, they all yield the same result. There is no hard and fast rule about which style to use and the usage is very much circumstantial. In many cases a codebase can have a mix of these paradigms, however, the only hard and fast rule is to keep the codebase consistent.

Some things to consider when choosing a paradigm:

  • How readable is your code?
  • How easy is it for someone else to work on your codebase?
  • Is there repeated code that can be consolidated into a reusable function or method?
  • How does the paradigm affect your folder and file structure?
  • How much boilerplate code is required to operate the paradigm?
  • How much technical overhead does the paradigm introduce?
  • Does your code scale well?

Newsletter