Pure Function
PronunciationPYOOR FUNK-shun
Definition
A pure function always returns the same result for the same inputs and changes nothing outside itself.
Where you hear it
In functional programming talks, in code reviews about testability, and when separating logic from input and output.
Examples
The payout calculation is a pure function, so testing it is easy.
Keep the logic pure and put printing and file access in a thin outer layer.
The tax function is pure: the same amount always gives the same tax, and it writes nothing.
Common mistake
Hiding a side effect inside a function that looks pure, for example reading the clock or a global variable.
Don't confuse with
A function with side effects, which may read or change things outside itself, such as files, the network or global state.
Say it at work
- In a meeting or chat
Make this a pure function and pass the date in as an argument.
- In an email or ticket
Pure functions are trivial to unit test.
Keep exploring
More in Programming Fundamentals