What are functions?
The function is a piece of code that will help if we are writing the same code repeatedly.
function iAmFunction() {
...some logic here
}
We will call the above function like this as shown below
iAmFunction();
What are pure and impure functions?
Impure functions are functions that contain one or more side effects and pure functions will not contain any side effects.
Ok, but what is a side effect? A side effect occurs in a program whenever you use external code in your function — which, as a result, impacts the function’s ability to perform its task.
var listOfAccessories = ['yoga mat', 'copper bottle'];
function addAccessory(newAccessory) {
listOfAccessories.push(newAccessory);
return listOfAccessories;
}
console.log(addAccessory('vibuthi'));
In the above code, the addAccessory
function is changing the state variable present in global. This is called a side effect. Here the addAccessory
function is called an impure function.
Example for pure function
function addAccessory(newAccessory) {
var listOfAccessories = ['yoga mat', 'copper bottle'];
listOfAccessories.push(newAccessory);
return listOfAccessories;
}
console.log(addAccessory('vibuthi'));
Advantages of pure functions:
1. Independency
The pure functions are independent because it is not dependent on any external data, it allows all data as parameters and do some logic, and returns an output. See the below code.
var listOfAccessories = ['yoga mat', 'copper bottle'];
function addAccessory(array, newAccessory) {
const newArray = [...array].push(newAccessory);
return newArray;
}
console.log(addAccessory(listOfAccessories,'vibuthi'));
The addAccessory
function is taking inputs as a parameter and clones an external variable to a newly created newArray
variable as a local state and some operation is done, finally, it returns newArray
as an output. Here the external state does not effect because of the function's logic.
So if we give the same inputs two or more times to the addAccessory
function, every time it (addAccessory) will give the same output.
2. Readability
The pure functions are easy to read and debug at the same time. Because it doesn't depend on the external code. It has its own inputs and returns an output.
Avoid code mutation:
The code mutation in a pure function will not make it an impure function but avoiding it will be good.
var listOfAccessories = ['yoga mat', 'copper bottle'];
function addAccessory(array, newAccessory) {
const newArray = [...array];
newArray.push(newAccessory);
return newArray;
}
console.log(addAccessory(listOfAccessories,'vibuthi'));
In the above code, the locally created state newArray
variable is mutated by using push()
method which appends a new entry into the newarray
variable. So, avoiding this is a better way of writing pure functions.
var listOfAccessories = ['yoga mat', 'copper bottle'];
function addAccessory(array, newAccessory) {
const newArray = [...array].push(newAccessory);
return newArray;
}
console.log(addAccessory(listOfAccessories,'vibuthi'));
In the above code, the locally created state newArray
variable is not mutated.