Pure Functions


Pure functions are a cornerstone of functional programming paradigms, offering predictability and testability. Understanding their characteristics is crucial for writing clean and maintainable code, especially in frontend frameworks like React.

A pure function adheres to two fundamental rules:

  1. Given the same input, it always returns the same output. This deterministic behavior eliminates side effects and ensures consistency.

  2. It produces no side effects. A side effect is any modification outside the function’s scope, such as changing a global variable, mutating an argument, or performing I/O operations.

Let’s illustrate with JavaScript examples:

// Pure function
function add(x, y) {
  return x + y;
}

// Impure function (modifies global state)
let globalSum = 0;
function addAndModifyGlobal(x, y) {
  globalSum = x + y;
  return globalSum;
}

// Impure function (mutates input)
function addToArray(arr, value) {
  arr.push(value); // Modifies the original array
  return arr;
}

// Pure function (creates a new array)
function addToArrayPure(arr, value) {
  return [...arr, value]; // Returns a new array without modifying the original
}

// Impure function (side effect: logging to console)
function addAndLog(x, y) {
  console.log("Adding:", x, y);
  return x + y;
}

In the examples above, add is a pure function because its output solely depends on its input and it doesn’t alter anything outside its scope. The other functions are impure due to their side effects: modifying a global variable, mutating an argument, or logging to the console.

Benefits of Pure Functions:

By striving to write more pure functions, you enhance the predictability, testability, and maintainability of your frontend code, leading to a more robust and efficient application.