The pun baked into this website’s name begs for this question to be answered. What does that mean?

Wikipedia:

In computer programming, a pure function is a function that has the following properties:
  • the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and

  • the function has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams).

In other words, a pure function’s output only depends on what you put in as arguments.

  • It does not persist any long-lived state
  • It does not do any network requests, nor to read anything or manipulate anything.
  • Same thing when it comes to file access.
  • Nor does it access variables outside its local scope.

a pure function:

function sayGreeting(name) {
  return `Hello ${name}`;
}

not a pure function:

let greeting = "Hello";

function sayGreeting(name) {
  return `${greeting} ${name}`;
}

In the world of React, pure components is a related concept. Since both React and Flutter depends on re-rendering their layout often by calling their respective render() and build() functions every time any state changes, pure functions and components are what you strive for by default, to keep things clear and performant. And avoid recursive traps.

When you need side effects or state, you may give up some purity, but then extra thoughtfulness is desired.




sponsor

Need private photo sharing that's not tied up in the social media circus? Photoroll is a good way to share photos with your clients.
Or privately gather media from participants of weddings and other small to medium-sized events.