Pure Functions

In computer programming, a pure function is a function that has the following properties:[1][2]

  1. the function return values are identical for identical arguments (no variation with local static variablesnon-local variables, mutable reference arguments or input streams), and
  2. the function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams).

No side Effects

Learn more: https://en.wikipedia.org/wiki/Pure_function

Side effects

const array = [1,2,3];

function mutateArray(arr){
    arr.push(5);
}

mutateArray(array);
console.log(array);
[ 1, 2, 3, 5 ]

The function has what we call side effects and the side effects are does the function modify anything outside itself.

const array = [1,2,3];

function mutateArray(arr){
    arr.forEach(i => {
        arr.push(i);
    });
}

mutateArray(array);
console.log(array);
[ 1, 2, 3, 1, 2, 3 ]

This is confusing because we should figure out how the function is modifying the array data.

This is one the problems with having side effects is that reusing shared state something like a global variable that can interact with anything and the order of function calls matter and that can cause a lot of bugs.

No side effects

const array = [1,2,3];

function addItem(arr){
    const newArray = [].concat(arr);
    newArray.push("***");
    return newArray;
}

console.log(addItem(array));
console.log(array);
[ 1, 2, 3, '***' ]
[ 1, 2, 3 ]

Although we created a new state or new data inside of the function it is local variable and the function is not modifying anything outside of our scope world.

Also, there is we call referential transparency and referential transparency simply means the same input and always the same output.

The rule no matter what the input if they are the same it is always going to give me the same output and as a matter of fact the functions also have no side effects.

function plus(n1, n2) {
    return n1 + n2;
}
function muliply(n) {
    return n * n;
}
console.log(muliply(plus(2, 6)));
64
  1. Easy to predict.
  2. Easy to test.
  3. Avoid a lot of bugs.

Can everything be pure?

  • console.log() which is a side effect.
  • As a matter of fact, input and output are a side effect that is communicating with the outside world.

Philosophically, without side effects, it does not do anything because a program can not exist without side effects. You can not run a piece of code without having a side effect of interacting with the browser. You can not have a Web site with just pure functions. So the goal of functional programming is not to make everything pure functions. The goal is to minimize side effects. The idea is to organize your code with there is a specific part that has side effects but when you have a bug you know right away to go to that spot because that is where the side effects are happening.

To build programs that are built with a bunch of very small reusable predictable pure functions.

  1. Pure
  2. No shared state
  3. Immutable state (Never modify global state)
  4. Composable
  5. Predictable
  6. Return statement

Leave a Reply

Your email address will not be published.

ANOTE.DEV