Pure functions

Objectives :

Understand what are Pure Functions and the benefits of using them

Connection - 10'

Categorize these code samples in Pure Functions vs Impure Functions :

import random

def f(x):
    if random.randint(1, 2) == 1:
        return x + 1
    return x + 2
public class MathUtility {
    public static int sum(int num1, int num2) {
        return num1 + num2;
    }
}
public class Line {
    private int value = 0;

    public int add(int nextValue) {
        this.value += nextValue;
        return this.value;
    }
}
public class MathUtility {
    private static int count = 1;
    public static int sum(int num1, int num2) {
        count++;
        multiply(num1,num2);
        return num1 + bnum2;
    }
}
const double = x => x * 2;

Explain your choices.

Concept - 5'

Pure functions don’t refer to any global state. Those functions do not produce any side effects (state changes).

They are easier to test because of these properties:

  1. You can see all the inputs in the argument list

  2. The execution is deterministic (the same inputs will always get the same outputs)

  3. You can see all the outputs in the return value

Code that is harder to test will lack some or all of these properties.

Concrete Practice - 30'

  • Clone the repository here

  • Identify the design problems related to the RentalCalculator

  • Refactor this code by using Pure Functions

A step by step solution is provided in the solution branch (Look at the commits)

Conclusion - 10'

  • How much of the time do you find yourself writing tests for functions that have all the three properties of pure functions ?

  • What do you need to write more Pure Functions ?

Last updated