Skip to main content

Pure Function vs Impure Function – Learn the Difference

An impure function is a function that contains one or more side effects, while a pure function is a function without any side effects.

Example 1: Impure Function

const myNames = ["Oluwatobi", "Sofela"];

function updateMyName(newName) {
myNames.push(newName);
return myNames;
}

In the snippet above, updateMyName() is an impure function because it contains code (myNames) that mutates an external state—which gives updateMyName() some side effects.

Example 2: Pure Function

We can eliminate updateMyName()'s side effects by turning it into a pure function like so:

function updateMyName(newName) {
const myNames = ["Oluwatobi", "Sofela"];
myNames[myNames.length] = newName;
return myNames;
}

In the snippet above, notice that updateMyName() does not depend on any external code to accomplish its duties. This makes it a pure function.

Wherever possible, you should use pure functions in your applications. Let's discuss some of the advantages you get by doing so.

Advantages of Pure Functions

The following are some advantages of pure functions.

1. Pure functions are independent

Pure functions do not affect any external state, and they are also not affected by external code.

In other words, all external data a pure function uses gets received as parameters—they are not explicitly used internally.

Therefore, what you see within is what you get—there are absolutely no strings attached.

As such, you don't need to look for external conditions (states) that might impact your pure function's effective operation as all activities happen within.

2. Pure functions are easier to read

Pure functions are easier to read and debug than their impure alternatives.

Pure functions are so readable because they are solely dependent on themselves—they neither affect nor are they impacted by external states.

Important Stuff to Know about Pure Functions

Keep these three essential pieces of info in mind whenever you choose to use pure functions.

Info 1: You can clone an external state into a pure function

Cloning an external state into a pure function does not make the function impure.

State duplication is simply a copy-and-paste operation that does not leave any strings attached between the source and its clone.

Here's an example:

const myBio = ["Oluwatobi", "Sofela"];

function updateMyBio(newBio, array) {
const clonedBio = [...array];
clonedBio[clonedBio.length] = newBio;
return clonedBio;
}

console.log(updateMyBio("codesweetly.com", myBio));

Try it on StackBlitz

In the snippet above, updateMyBio() used the spread operator to duplicate myBio's state into clonedBio. However, it is still a pure function because it is neither dependent on myBio nor does it modify any external code.

Instead, it is an exclusively deterministic function programmed to use the cloned version of its array parameter.

Info 2: Avoid code mutations in pure functions

Technically, you can mutate variables defined locally within a pure function's scope. However, it is best to avoid doing so.

For instance, consider the code below:

const compBio = ["code", "sweetly"];

function updateCompBio(newBio, array) {
const clonedBio = [...array];
clonedBio[clonedBio.length] = newBio;
return clonedBio;
}

console.log(updateCompBio(".com", compBio));

Try it on StackBlitz

In the snippet above, updateCompBio() is a pure function that uses clonedBio[clonedBio.length] = newBio to alter its local state.

Although such operation does not make updateCompBio() impure, it is not the best practice.

The recommended way to write a pure function is to make it receive all its values as parameters like so:

const compBio = ["code", "sweetly"];

function updateCompBio(newBio, array) {
return [...array, newBio];
}

console.log(updateCompBio(".com", compBio));

Try it on StackBlitz

Notice how clean and portable our code now looks. This is an advantage of making your pure function receive all its values as parameters. By so doing, you will also find it easier to debug your code.

Info 3: The same input will always return the same output

A vital trait about pure functions is that they will always return the same value with the same set of inputs—no matter how many times you invoke them.

Overview

Your function is pure if it does not contain any external code. Otherwise, it is impure if it includes one or more side effects.

Credit

This post is also published at educative.io.

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Join CodeSweetly Newsletter