Impure Functions
public void doSomething(int a, int b) { ... } // void methods must have side-effects
public boolean flipACoin() { // A no-argument pure function can be a constant?
double result = Math.random(); // Produces output based on randomness
}
public int countUnique(List<String> names) {
Set<String> nameSet = new HashSet<>(names);
this.numUniqueNames = nameSet.size(); // Uh oh. Side effects detected
return this.numUniqueNames;
}
public void filterANames(List<String> names) {
for(int i = names.size() - 1; i >= 0; i --) {
if(names.get(i).startsWith("A")) {
result.remove(i); // This looks dangerous
}
}
return result;
}
19 / 22