Frontend Scrapbook

Notes that make a difference

Referential transparency

By admin

on Sat Jun 13 2020

Function purity is determined by its referential transparency. ie, ability to replace the function call with its return. This improves readability of the code.

const sum = function(x, y){
return function() {
return x+y;
}
}

const x = sum(5,6);

x() // 11


x() // 11

As in above example, it is guaranteed that the call to function x will always produce 11. Then we call such a function, a pure function in functional programming.

Referential transparency says, we could take that function call and replace it with its return, it would have NO effect on other parts of the program.

Any thoughts ?