A Quick Comparision Between Imperative and Functional Programming (Part 1)

Vy Le
3 min readFeb 19, 2022

--

These days, we often hear about the phrase “functional programming”, especially the frontend guys. I used to wonder what it is and why I need to follow it. In this short article, I will perform a quick comparision between imperative programming and functional programming. About the definition and detailed explanation, please search them on Mr. Google :).

Note 1: Examples are written in Python but can be applied to any code base in any language.

Note 2: This article is inspired a lot and has many references from https://codewords.recurse.com/issues/one/an-introduction-to-functional-programming. Thanks Mary Rose Cook for your short but condensed explanation about function programming.

Don’t iterate over lists. Use map and reduce:

  1. Using map:

2. Using reduce:

Here are some benefits of using map or reduce over imperative loop:

  • they are just one-liners
  • the collections (array), the operation and the return value are always in the same places
  • they don’t affect external variables
  • code reader can instantly understand and abstract in their mind what they are doing (e.g map means we are transforming each item in this collection; reduce means we are combining all items into an output)
  • we have other utility functional functions: find, filter, all, any, etc.

Write declaratively, not imperatively

Imagine we have a list of users including boys and girls, who are looking for their future partners. We will match each girl to a random boy. Here is imperative way of doing that:

The code is written in imperative way. Code readers have to read each line in the while loop carefully to understand what it is doing. A functional version of this will be more declarative as below.

Use functions

By bundling pieces of the code into functions, we will make the program more declarative:

Code readers just need to read the main loop and get the idea: While there’re still unmatched girls, we will continue match a girl to a boy. If the readers wants to understand more about what exact match_a_couple() do, they will read the code inside it.

However, this way is not really functional. It still has high risks of feeding bugs because it affects users directly. Besides, when code readers read match_a_couple() and find external variable users, they have to find its origin. After that, they must see what match_a_couple() change that variable. This is really annoying and easy to be confused, especially if this program is longer.

Remove state

Here is a more exact version of functional programming:

The code is still split into functions, but the functions are really functional. By removing state, there is not any shared variables. All data changes are done with return values. match() recurses with the result of match_a_couple(). Each time match_a_couple() matches a girl to a boy, a new state of boy and girl list will be returned to next match() function.

Part 2

--

--