Pure function #
Side effects #
Definition. A method has side effects if it modifies resources (variable, object, array, etc.) that are accessible outside of its scope.
Definition #
A pure function is a method that largely behaves like a mathematical function. More precisely:
Definition. A pure function is a method that:
- returns a value,
- returns the same value if called twice with the same arguments, and
- has no side-effect.
Example #
The Java method createUsers below is not a pure function.
Can you see why?
How would you rewrite it into a pure function?
public class MyClass {
String[] names;
User[] users;
public MyClass(){
String[] names = parseNames();
users = new User[names.length];
createUsers();
}
...
void createUsers() {
for(int i = 0; i < names.length; i++){
users[i] = new User(i, names[i]);
}
}
}
public class MyClass {
String[] names;
User[] users;
public MyClass(){
String[] names = parseNames();
users = createUsers(names);
}
...
Users[] createUsers(String[] names) {
User[] users = new User[ids.length];
for(int i = 0; i < ids.length; i++){
users[i] = new User(i, names[i]);
}
return users;
}
}
Benefits #
Easier debugging #
Because it has no side effect, a pure function may cause a bug only if it returns an incorrect value.
Readability #
The behavior of a pure function can be fully documented by describing its signature (arguments and return value).
Easier parallelization #
Consider two pure functions $f$ and $g$. If the input of $f$ (resp. $g$) does not depend on the output of $g$ (resp. $f$), then they can be executed in any order (therefore also in parallel).
Example. If $f$ and $g$ are pure functions, the two programs below (written in pseudocode) are equivalent:
x = f(a)
y = g(b)
result = x + y
y = g(b)
x = f(a)
result = x + y