Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen

JavaScript is a language of Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.

In the last article I covered anonymous function. In this article I will cover pure and impure function in detail. Before I cover this, we should understand some aspects of functional programming so we know what is mutable and immutable objects.

Before moving further let us look at the previous articles of the series:

Functional programming

It is a different style of programming language that uses declarative approach rather than imperative. In declarative programming, you are using a more descriptive way to define what you want to do and not how you want to do some action. Example, LINQ in C#

bottles.Where(price<20).OrderBy(brand).Take(10);

It will find bottles where price is less than 20, order by brand and take top 10.

Another aspect of declarative programming is that it avoids changing state and mutable data. In simple terms, once a variable is bound to a value, it cannot be changed.

Two more concepts

Mutable

Mutable means it can be changed. Different programming languages have different approaches towards implementation of mutability or immutability.

In Javascript, Objects are mutable as they are addressed by reference and not by value.

Example

  1. var y={a:1, b:2};  
  2. var x =y;  
The above will not duplicate y in x, but it’ll point x to y. Please refer objects (part 2) for detail.

Immutable

Immutable means it cannot be changed. In Javascript strings are immutable. Example,

code

You cannot change string value. If you want to modify a string it’ll always return a new string. Remember using trim, slice, toUpper functions

code

Pure function

These are the functions which always return the same value when given the same arguments. They take some parameters, return a value based on these, but don’t change parameter values. Example product is a function which will always give same output depending upon the input.
  1. function product(a, b) {  
  2. return a * b;  
  3. }  
  4. console.log(product(2, 3)); // always give 6 whenever you pass 2, 3  
Another nice property of pure function is that it doesn’t modify the states of variables out of its scope.

Example
  1. var x = 10;  
  2. function pureFunction ( a ) {  
  3. return a + 2;  
  4. }  
code

pureFunction doesn’t modify the variable outside of its scope , i.e., “x”. It a nutshell pure function doesn’t have side effects.

Impure function

Impure function may have side effects and may modify arguments which are passed to them. The return value will depend upon the arguments. There may be a case where for same arguments you’ll get different values:

Example
  1. var count = 0;  
  2.   
  3. function Hits() {  
  4. count += 1;  
  5. }  
  6. Hits ();// will make count 1  
  7. Hits ();// will make count 2  
  8. Hits ();// will make count 3  
Here it's using an external variable count and also modifying it. If a function has side effects whether it updates any file or database it’ll also fall under the category of impure function.
  1. function impureFunction ( a ) {  
  2. file('update.txt');  
  3. }  
Summary of differences

Pure functionImpure function
No side effects like update or db callsMay have side effects
Don’t modify arguments which are passed to themMay modify arguments passed to them,
Always return the same valueEven if you call with same arguments, you may get different values.

I hope you enjoyed reading the article. Please share your comments or feedback.