Pure Functions
A pure function is a function that is completely predictable and self-contained. It always follows two rules:
1. No side effects
A pure function does not change anything outside of itself.
It doesn’t modify external variables, update global state, write to disk, or alter its inputs.
2. Deterministic results
Given the same inputs, a pure function will always return the same output.
There is no randomness, no time-based behavior, and no dependency on external state.
Why Pure Functions Matter
Pure functions are easier to understand because everything they do is visible in the code.
They’re also easier to test, debug, and reuse, since they don’t rely on hidden state or external conditions.
They play a key role in systems that depend on:
- predictable behavior
- undo/redo or history tracking
- reliable state transitions
- clear data flow
Any time you need code that behaves consistently and safely, pure functions are a strong fit.