Comparison
Pure functionvsSide effect
Pure function
you called it twice with the same arguments, got the same answer, and nothing anywhere else changed.
A function whose result depends only on its arguments and which changes nothing outside itself. Purity is what makes a function trivially testable, safely cacheable, freely reorderable and safe to run on several threads — every one of those properties follows from the same two conditions. The design move it suggests is not writing everything this way but knowing which of your functions are, and being deliberate about where the impure ones live.
Full entry →Side effect
the function returned a number and also wrote a row, and the second half is not in the signature anywhere.
Anything a function does beyond computing its result: writing to storage, mutating an argument, logging, reading the clock, sending a request. Side effects are the entire point of a program — a pure program with no effects does nothing observable — so the goal is never to eliminate them but to keep them out of the parts you want to reason about. Naming them is what makes the distinction discussable in a review.
Full entry →