Do you know Functional Programming?

   



Functional programming is a paradigm that focuses on using functions to solve problems, treating data as immutable and trying to avoid side effects.

🔗 Do you like Techelopment? Check out the website for all the details!

Functional Programming

The main feature of functional programming is that functions are treated as first-class citizens. This means that functions can be passed as arguments to other functions, returned by other functions, and assigned to variables, just like any other data type.

In the context of functional programming, inline functions play a fundamental role. An inline function is a function defined directly within the code, which can be passed as a parameter to other functions or used immediately. This makes the code more concise, modular and easily readable, as it reduces the need to define separate functions, promoting more expressive and purpose-oriented writing.


Traditional (Imperative) Programming vs Functional Programming

To better understand what functional programming means, let's see a practical comparison between a traditional (imperative) and a functional approach in a language like JavaScript.

Example with Traditional Programming (Imperative)

Imagine you want to add all the numbers in an array that are greater than 10.

In an imperative approach, the programmer must explicitly manage the control flow, modify state variables, and iterate through the array manually. An example might be the following:


const numbers = [5, 12, 8, 130, 44];
let sum = 0;

// Explicit iteration with for loop
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] > 10) {
    sum += numbers[i];  // add to sum
  }
}

console.log(sum);  //Output: 186

In this case:

  • The execution flow is controlled manually via the loop for.

  • The numbers array is iterated, and the sum is changed explicitly inside the loop.

  • The code is verbose and not particularly declarative. It also uses a state variable (sum) that is modified during execution.

Example with Functional Programming

In a functional approach, on the other hand, the programmer tends to focus on operations that transform data declaratively, often using inline functions and methods like .filter() and .reduce() that abstract the imperative logic.

The same example of adding numbers greater than 10 using a functional approach could be written like this:


const numbers = [5, 12, 8, 130, 44];

// Using filter and reduce
const sum = numbers
  .filter(num => num > 10)  // filter numbers greater than 10
  .reduce((acc, num) => acc + num, 0);  // Sum of filtered numbers

console.log(sum); //Output: 186

In this case:

  • The execution flow is abstracted via higher-order functions (filter and reduce), without having to write explicit loops.

  • Inline functions: The filter function num => num > 10 is defined directly inside the filter method, and the sum function (acc, num) => acc + num is passed inline to the reduce method. This makes the code more compact and readable.

  • There is no need to manually manage state variables such as the sum; everything is handled immutably.


Wait...why did we write inline functions with that weird syntax?

Actually, it is not a strange syntax but simply the way in which from the ES6 version of JavaScript it is possible to write anonymous functions using the arrow function notation. For more information you can read the article The famous lambda functions (aka arrow functions).


Main features of functional programming

  1. Immutability: Data is not directly modified. Each operation on a data structure creates a new structure without altering the original. For example, filter and reduce return new arrays or values ​​without modifying the original array.

  2. Higher-order functions: Functions can be passed as parameters to other functions, returned by other functions, or used inline. This is a fundamental concept of functional programming, which allows for more expressive and modular code.

  3. Pure Functions: Functions should be pure, meaning they should have no side effects (they should not modify global variables or the state of the application) and should return the same result given the same input. This makes the code more predictable and easier to test.

  4. Function Composition: Functions can be combined together in elegant ways. For example, in the code above, filter and reduce are used sequentially, resulting in one becoming the input for the other. This is an example of function composition.


Benefits of Functional Programming

  1. More concise and readable code: Using inline functions and higher-order methods makes the code more compact and clear. There is no need to manually manage loops or state variables, which reduces the risk of errors and makes the code easier to understand.

  2. Better modularity: Pure functions and function composition allow you to create components that can be easily reused. Each function is self-contained and independent, making the code more maintainable.

  3. Better error handling: Since pure functions have no side effects, it is easier to trace errors and predict how the system will behave. Functions do not change the global state, so the likelihood of introducing bugs related to unwanted state changes is reduced.

  4. Ease of testing: Pure functions are easier to test because, given an input, they always produce the same output, without depending on the external state.


Conclusion

Functional programming offers a different perspective than traditional programming. While imperative programming focuses on how to do things, functional programming focuses on what to do, abstracting the execution flow and making the code more declarative.

By using inline functions, code becomes more concise, readable, and maintainable, with greater separation of concerns.


Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment