Pure and Impure Method

One of the most rare terms used in java to explain the function or method in the programming, even though this terms are commonly based on theory and not used in regular programming by the professionals but still it is worth to take a look at this specific terms which are ‘pure’ and ‘impure’ functions.

This specific words are  not specific to java but in general remains same for all the programming languages. Let’s consider pure method first.

Method is said to be pure if and only if it follows all statements stated below.

  • Method must return same result for the same argument values
  • Result of method does not depend on any other external state which may change during the execution
  • It should not depend on external I/O devices
  • Result evaluation should not create any side effects

So, in case if any function which does not follow any of the above statements is said to be impure.

Now as we have already listed out the quality every pure method must have to follow but let’s discuss it briefly. Consider an example to bifurcate between the pure and impure functions.

public int square(int number) {
    return number * number;
}

In above method it will return the square of the argument passed in it, notice that in any case it will return the same result for the same argument you won’t have 5 for square(2) and this method will never going to be affected by any external state nor it will create any kind of side effect.

So ultimately we reached to the goal to prove that any method or function which is impure if it change behavior according to the global or external state and does not return same result for the same argument etc. So, consider following method,

public int randomNumber() {
    Random random = new Random();
    return random.nextInt();
} 

In above method you will have different number on every call and it depends on external class or state which is changing the result of the method. So we can firmly say that result of pure method only depends on it’s arguments and not on any other external element moreover it does not generate any side effect.

Side effect means method modify some external state in addition to the actual evaluation of result.

Side effect here can be considered as a change of some other external state during the execution of the method.For example println method which eventually write the data on the standard output or file as an side effect.

Some examples of pure and impure methods in java,

PURE     :   finds square of any number, length of String etc.

IMPURE :   generate random number, return current date and time etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s