pure function

A function whose result depends only on its arguments and which has no side effects, so the same inputs always give the same output.

In more detail

Pure functions are the easiest code to test: call them with inputs and check the result, with no database or clock to prepare. Side effects such as reading the clock or writing to storage are pushed out to injected collaborators instead.

Examples

The course keeps business decisions pure and injects time, storage and network access.
React requires components and reducers to be pure during rendering: changing an outside variable, sorting a prop array in place or starting a request during render makes results unpredictable.
In this course: Pure functions are the easiest code to test, because there is nothing to set up and nothing to clean up. Real code also reads clocks, files and networks; much of testing is about pushing those inputs to the edges so the core stays pure.

Related terms

Taught in

References