Tech Terms Explained: Programming Paradigms

David Deren
Hackages Blog
Published in
5 min readMay 10, 2019

--

Programming is about solving problems and problems can usually be solved in a variety of ways. A programming paradigm is a way to approach a problem. While some programming languages allow you to use only one approach, most popular languages today allow developers to use a combination of them.

On the highest level, we can differentiate between 2 types of programming paradigms: imperative and declarative.

When we program in an imperative way, we tell the computer how to solve a problem.

When we program in a declarative way, we tell the computer what we want as a result.

In this article, we’ll focus on 3 of the most popular approaches: procedural programming and object-oriented programming, which are both examples of imperative paradigms; and functional programming, which is an example of a declarative paradigm.

Don’t worry if this all sounds a little bit abstract right now. I will illustrate their differences using small snippets of Javascript code. Don’t panic! You don’t need any programming experience. I will walk you through each example and explain exactly what happens step by step. The main goal here is to give you a general idea of what the differences are between the 3 approaches.

The Problem

Things are not going well in the hip new startup MyYummeeFoods. The company cannot launch their IPO until they add one last feature: they need to be able to calculate the total amount their users have to pay at checkout. The 3 lead developers, Athos, Porthos and Aramis; are debating on the best way to solve the issue.

First for some setup: The following snippets of code will operate on the same collection of items:

cartItems is just a collection of items that the customer has put in his cart. Each item has a name and corresponding price. The problem we then have to solve is: calculate the total amount to be paid based on the items in the shopping cart.

The Procedural Approach

Our first developer, Athos, proposes the following solution:

When you solve a problem in a procedural way, you tell the computer exactly which steps to go through in order to solve it. In this example, we tell the computer to go through the following steps:

  • Create a new variable total, and set its value to 0.
  • Check how much items are in cartItems
  • If the cart is not empty, get the first item in cartItems and add its price to the running total
  • If the cart contains more than 1 item, get the second item and add its price to the running total, continue doing this until you’ve gone through all the items
  • Finally, we assign the value of total to result

You can imagine how it might sometimes get tedious to solve problems in a procedural way, with you having to create temporary variables, keep track of indexes (= the position of an item in a collection),…

The Object-Oriented Approach

This isn’t Porthos’ first gig. He knows how complex applications become over time and doesn’t think Athos’ procedural solution will be maintainable in the long run. He comes up with the following solution:

Porthos’ program solves the issue following these steps:

  • First it declares a class Cart, which acts as a kind of blueprint describing which things a cart can contain and what it can do
  • It then creates a new cart called myCart, by combining the blueprint that was created earlier with the actual items we want our cart to contain. If you ever heard a developer talk about an instance, this is what she was talking about.
  • This newly created myCart has a property total, whose value we assign to the final result.

While still being an imperative approach, the added value of Object-Oriented programming is that it groups related data and functionality. You create objects that reflect real things, making some problems easier to reason about.

The Functional Approach

Aramis likes his programs to be as descriptive as possible. He doesn’t care too much about a telling a computer how to solve an issue. He prefers to tell the computer exactly what he wants.

Aramis remembers he already wrote a sum function when developing the counters in the shopping cart. As he doesn’t want to reinvent the wheel, he decides to reuse the same generic sum function here. In functional programming, functions are to be as reusable and generic as possible.

The only value Aramis cares about is that final result. He describes it as the sum (using reduce) of all the cartItems’ prices (using map). Even without knowing exactly what map and reduce do, this piece of code should give you a good idea of what is happening here.

The Solution

After extended discussions, our 3 developers decide there are good elements in each of their proposed solutions (except for Athos’ procedural solution, poor guy!). They want to keep using elements from Orthos’ object-oriented approach, as that would make the front-end code easier to understand for the Java team and will make the code easier to maintain in the long run; but they also like how the functional approach makes their code more descriptive and easier to reason about.

As they are writing their application in Javascript, they can mix and match and use the best of both worlds. They come up with this solution:

MyYummyFood’s founder is ecstatic. The IPO is saved and the shares’ prices go through the roof 🎉

I hope you now have a better understanding of what a programming paradigm is and of the differences between the 3 main ones. If you’d like to have other tech terms explained to you in a friendly way, check out the following articles:

You can find more information about Hackages on our learning platform. We offer high-level training on the latest frameworks and technologies and Tech Jargon training for non-tech profiles working in the IT sector to understand the terms used in the field and improve their daily tasks.

--

--