a shot of dev knowledge

RELATED TAGS

Pure function vs. impure function

An impure function is a function that contains one or more side effects. 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 a code (myNames) that mutates an external state — thereby making updateMyName() have 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;
}

Now, updateMyName() does not depend on any external code to accomplish its duties, therefore, making it a pure function.

Advantages of pure functions

The following are some advantages of pure functions.

Advantage 1: Independency

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

In other words, all of the external data that a pure function uses gets received as parameters as 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 conditionsstates that might impact your pure function’s effective operation as all activities happen within.

Advantage 2: Readability

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

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

Important information on 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.

Example

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

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

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

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 in 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));

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

Although such an 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));

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

Info 3: 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.

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring