In functional programming, one principle reigns supreme: Don’t mess with state. This principle forms the bedrock of pure functions, which eschew side effects and serve as the atomic units of code in any functional program. Today, we’ll go over the power of pure functions, how they work, and why they’re so essential for writing clean, maintainable code.

Reminiscent of “In mathematics, a function is a relation between sets that associates to every element of a first set exactly one element of the second set”1, pure functions ensure predictable results leading to more maintainable software.

So, what exactly are pure functions? According to Professor Frisby’s Mostly Adequate Guide to Functional Programming, pure functions are functions that have two main properties2:

  1. Their return value is the same for the same arguments.
  2. Their evaluation has no side effects.

Have a look at the following piece of JavaScript code3:

function square(x) {
  return x * x;
}

In the world of JavaScript, this is a pure function. The function square will always produce the same output given the same input, and it won’t mess around with any other variables or state in your program.

Contrast that with:

let x = 10;

function square() {
  return x * x;
}

This version of our square function is not a pure function, because it’s mucking around with external state (our variable x), and its output can change if x changes.

Pure functions have the predictable behavior that makes code more maintainable, and easier to test. For instance, if you’re writing a unit test for a pure function, you don’t have to worry about setting up or tearing down state. You can simply pass in values, get a result, and assert that it’s what you expected4.

describe('square', () => {
  it('squares its input', () => {
    expect(square(5)).toBe(25);
    expect(square(-5)).toBe(25);
  });
});

Moreover, functional programming encourages combining smaller pure functions into larger, more powerful ones—a principle known as function composition5. In JavaScript (especially the modern ES6 flavor) and TypeScript, you can do this using higher-order functions such as map, reduce, and filter.

const numbers = [0, 1, 2, 3, 4];
const squaredNumbers = numbers.map(square);

In this example, .map is a higher-order function which receives square (a smaller pure function) and applies it to every element in the numbers array. The result, squaredNumbers, is a new array of squared numbers, and the original numbers array remains untouched6. This brings us to immutability, another cornerstone of functional programming7.

In the realm of Java-like languages, Scala has made significant adoption of functional programming concepts. The following Scala code provides a pure function example8:

def add(x: Int, y: Int): Int = x + y

As the pure function feminism implies: “The side-effect of any function is not feminism, it is cancer. The side-effect of any function is not transparency, it’s obscurity. It’s not adding information, it’s hiding it."9 Therefore, watch for code smells that signal reliance on mutable state or side effects may be stunting your code’s scalability and maintainability10.

In summary, the beauty of functional programming, through the power of pure functions, lies in its simplicity and expressiveness. Writing code in a functional style makes it cleaner, easier to understand, and less prone to bugs. So, the next time you find yourself reaching for mutable state or writing an impure function, remember the benefits of keeping things pure.


  1. https://en.wikipedia.org/wiki/Function_(mathematics) ↩︎

  2. https://github.com/MostlyAdequate/mostly-adequate-guide ↩︎

  3. https://github.com/getify/Functional-Light-JS ↩︎

  4. https://jestjs.io/docs/en/getting-started ↩︎

  5. https://en.wikipedia.org/wiki/Function_composition_(computer_science) ↩︎

  6. https://www.typescriptlang.org/docs/handbook/map.html ↩︎

  7. https://en.wikipedia.org/wiki/Immutable_object ↩︎

  8. https://github.com/scala/scala ↩︎

  9. https://www.youtube.com/watch?v=JMP6gI5mLHc ↩︎

  10. https://martinfowler.com/bliki/CodeSmell.html ↩︎