Keep a calculation separate from its side effects

A pure function computes a result from its input without writing a file or changing another system.

A pure function computes a result from its input without writing a file or changing another system. Calling this function twice returns the same result. Side effects need separate tests.

Work through the example

For f(x) = x squared, f(4) = 16 on both calls. Two evaluations produce zero database writes.

Run the example as a separate Python file. The inputs are synthetic so you can change them and see exactly which assumption affects the result.

Run the example

def square(x):
    return x*x
print(square(4), square(4))

Expected output

16 16

Reference: https://docs.python.org/3/library/functions.html

The team guide · My original notes on testing coding agents · Working with Lightbulb Partners