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
- var y={a:1, b: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,
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
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.
- function product(a, b) {
- return a * b;
- }
- console.log(product(2, 3));
Another nice property of pure function is that it doesn’t modify the states of variables out of its scope.
Example
- var x = 10;
- function pureFunction ( a ) {
- return a + 2;
- }
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
- var count = 0;
-
- function Hits() {
- count += 1;
- }
- Hits ();
- Hits ();
- Hits ();
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.
- function impureFunction ( a ) {
- file('update.txt');
- }
Summary of differences
Pure function | Impure function |
No side effects like update or db calls | May have side effects |
Don’t modify arguments which are passed to them | May modify arguments passed to them, |
Always return the same value | Even if you call with same arguments, you may get different values. |
I hope you enjoyed reading the article. Please share your comments or feedback.