Pure function #
Side effect #
Definition. A method has a side effect if it modifies a resource (variable, object, array, etc.) that is defined 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(){
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(){
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).
Parallelization #
Example. If $f$ and $g$ are pure functions, then the two programs below (written in pseudocode) are equivalent. Therefore
f(a)andg(b)can also be executed in parallel.
x = f(a)
y = g(b)
result = x + y
y = g(b)
x = f(a)
result = x + y