Skip to content

37 Pure Functions

Biswajit Sundara edited this page Aug 18, 2023 · 1 revision

Pure function is a function that returns the same output if the same arguments are passed and has no side effects.

1. Pure Function

Pure functions should meet the below criteria

  • It only depends on its input parameters and always returns the same output for a given set of input parameters
  • It does not have any side effects i.e modify any external state (modifying a global variable/DOM) or performing an I/O operation(database, logging to the console)
  • These rules are called Deterministic & No side effects respectively.
function add(x, y) {
  return x + y;
}

console.log(add(2, 3)); //Always 5
console.log(add(12, 13)); //Always 25
  • Pure functions are highly desirable in programming because they make code more predictable, easier to test, and less prone to bugs.
  • By minimizing side effects and external dependencies, pure functions contribute to code that is easier to understand, debug, and maintain.
  • They also enable benefits such as memoization, caching, and parallelization, which can lead to performance optimizations.

2. Impure Function

Impure function is a function that doesn't return the same output for the same inputs or has side effects.

  • Have Side effects - modifies data outside of their scope or involve in I/O operations such as database network calls.
  • Non Deterministic - produces different outputs for the same input, depending on the state of the program or external factors.
function addRandom(x, y) {
  return x + Math.random()*y;
}

console.log(addRandom(2, 3)); //Inconsistent output
console.log(addRandom(12, 13)); //Inconsistent output

3. Side Effects

Side effects are actions that go beyond the scope of the function itself and can affect the state of the program or interact with the environment.

A. Modifying external variables or data

  • Here calculatePay() is considered impure function
  • Because it's modifying global variable totalPay
let totalPay;

function calculatePay(basic, hra) {
  const gross = basic + hra;
  totalPay = gross;
  return gross;
}

console.log(calculatePay(200, 300));
const hobbies = ["Sports", "Cooking"];

function displayHobbies(hobbies) {
  hobbies.push("Reading");
  console.log(hobbies);
}

displayHobbies(hobbies);

B. I/O Operations

  • Making http call to save data is considered a side effect.
  • Or save data to file, printing in console etc.
function saveDataToServer(data) {
  console.log('Make http call');
}

saveDataToServer('hello');

Notes

  • It's generally recommended to use pure functions as much as possible and isolate impure operations to specific portions of the codebase,
  • such as in I/O handling or boundary interactions. This separation helps to maintain clarity, predictability, and testability in the code.
  • it's important to note that not all side effects are inherently bad, many cases, side effects are necessary to achieve certain functionality
  • such as interacting with external resources or producing output.
  • The key is to manage side effects intentionally, isolate them when possible, and clearly document their behavior to ensure code clarity and maintainability.

Clone this wiki locally