Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an example of an impure function in JavaScript

Having seen a lot of pure functions and how they have no side effects, what would be an example of an impure function, which is always been antagonized as unstable and major source of error?

like image 737
user2167582 Avatar asked Mar 06 '16 09:03

user2167582


People also ask

What makes a function impure?

An impure function is a function that mutates variables/state/data outside of it's lexical scope, thus deeming it “impure” for this reason. There are many ways to write JavaScript, and thinking in terms of impure/pure functions we can write code that is much easier to reason with.

What is pure function in JavaScript example?

A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments.

What is not a pure function?

An impure function is kind of the opposite of a pure one - it doesn't predictably produce the same result given the same inputs when called multiple times, and may cause side-effects. Let's have a look at some examples.

Is map a pure function in JavaScript?

Pure functions are all about mapping. Functions map input arguments to return values, meaning that for each set of inputs, there exists an output. A function will take the inputs and return the corresponding output.


2 Answers

For example an impure function that has a side effect on a variable outside of its own scope:

var count = 0;

function increaseCount(val) {
    count += val;
}

Or a function that returns different values for the same input because it evaluates a variable that is not given as parameter:

var count = 0;

function getSomething() {
    return count > 0;
}
like image 130
crackmigg Avatar answered Oct 23 '22 22:10

crackmigg


A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope.

Concretely, that means a pure function always returns the same result given same parameters. Its execution doesn’t depend on the state of the system.

var values = { a: 1 };

function impureFunction ( items ) {
  var b = 1;

  items.a = items.a * b + 2;

  return items.a;
}

var c = impureFunction( values );
// Now `values.a` is 3, the impure function modifies it.

Here we modify the attributes of the given object. Hence we modify the object which lies outside of the scope of our function: the function is impure.

var values = { a: 1 };

function pureFunction ( a ) {
  var b = 1;

  a = a * b + 2;

  return a;
}

var c = pureFunction( values.a );

we simply modify the parameter which is in the scope of the function, nothing is modified outside!

var values = { a: 1 };
var b = 1;

function impureFunction ( a ) {
  a = a * b + 2;

  return a;
}

var c = impureFunction( values.a );
// Actually, the value of `c` will depend on the value of `b`.
// In a bigger codebase, you may forget about that, which may 
// surprise you because the result can vary implicitly.

Here, b is not in the scope of the function. The result will depend on the context: surprises expected!

var values = { a: 1 };
var b = 1;

function pureFunction ( a, c ) {
  a = a * c + 2;

  return a;
}

var c = pureFunction( values.a, b );
// Here it's made clear that the value of `c` will depend on
// the value of `b`.

Reference : For more details, click here

like image 22
abhiagNitk Avatar answered Oct 23 '22 21:10

abhiagNitk