![]() |
Functional programming is a programming paradigm that is based on the concept of pure functions and immutability of data. In recent years, it has gained popularity due to its many advantages over imperative and object-oriented paradigms, you can learn more about it in the article Do You Know Functional Programming?.
In this article, we will see how we should think when we start writing code with this programming paradigm.
for loop in traditional way
Imagine you have an array of JSON objects representing persons. Here's an example of how you can iterate over it using a for loop:
In this example, the for loop manually loops through the array and prints each person's name and age. This is the traditional method and requires you to explicitly manage the index.
By using forEach (Functional Programming)
Now let's see how we can achieve the same result, but with a more functional approach, using the forEach method. This is a typical example of functional programming in JavaScript, which makes the code more readable and concise.
In this case, forEach is an array method that automatically iterates over the array elements. There is no need to worry about indices or explicit looping. The code is cleaner and more concise, and better follows the principles of functional programming.
Comparison
With
forloop: You have to manually manage the index and iterate over each element, which can make the code more verbose and error-prone.With
forEach: The callback function is more concise (also thanks to the arrow functions) and the iteration is handled automatically. The code is more readable and functional.
These examples demonstrate how functional programming, using methods like forEach, can simplify code and make it more readable.
Evolution
Now imagine that, following a software update, the persons array needs to be updated to include a new property gender. As a result, our program needs to print only female persons and exclude all male persons.
Let's see how our software should evolve.
Non-functional approach (immediate, but not optimal)
A programmer who is not used to functional programming could simply add a condition inside the forEach to exclude persons of the "male" gender. Here's how he might do it:
In this example, the if operator inside the forEach manually excludes persons of the "male" gender. While this approach works, it is not ideal because it mixes filtering logic with iteration logic. The code may become less clear, especially as the number of conditions increases or the code grows.
Furthermore, in more complex cases, mixing functional programming (forEach) with imperative programming (if inside the forEach) might not even give the expected results due to side-effects or execution scope (remember that forEach is a wrapper that takes functions as input) that might overwrite variables and values.
Functional approach (more elegant, with separation of concepts and safe)
A more functional programming approach is to separate the filtering logic from the iteration logic. In this case, you can use the .filter() method before calling forEach to directly exclude persons of the "male" gender.
This approach makes the code more readable, modular and adherent to functional principles. Furthermore, in this way we avoid possible side-effects due to different programming approaches (e.g. functional programming vs imperative programming).
Functional code explanation
.filter(): First, we use the filter() method to create a new array that contains only persons whose gender is not "male". The filter is clear and separate from the iteration logic..forEach(): Then, after applying the filter, we use forEach() to iterate only over the persons who match the filter. This makes the code more readable, easier to maintain, and more declarative.
Advantages of the functional approach:
Separation of duties: Separate the filtering logic from the iteration logic. The filter() function is clearly responsible for filtering, while forEach() only takes care of iteration. This makes the code more modular and understandable.
Conciseness and Readability: The code is more concise and clear, without having to insert conditions inside the loop. The intent is immediately understandable, and the natural functional flow (filtering, then iteration) is followed.
More flexibility: If in the future you wanted to change the filter condition (for example, exclude another gender or add more conditions), you would just need to change the filter() part, without touching the iteration logic.
No side-effects: Using functional programming offers a higher level of reliability in reducing the risk of side-effects in the code due to potential variable overrides or scope issues.
In general, the functional approach is more scalable and easily maintainable.
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment
