Functional purity is introduced as a practical way to think about functions in code, not just in mathematics. A pure function depends only on its inputs and produces no side effects, while an impure function may alter state, depend on outside context, throw exceptions, or perform I/O. The chapter stresses that many real applications inevitably contain impurity, but understanding where it appears helps you control it rather than letting it spread through the whole program.
The chapter then shows why purity matters most in concurrent and parallel scenarios. Pure functions are deterministic, so they can be evaluated in any order, cached, deferred, or run in parallel without changing results. By contrast, impure functions can behave unpredictably when parallelized, especially if they mutate shared state. The text demonstrates how avoiding argument mutation and shared mutable state makes code easier to reason about and can unlock safer parallel execution.
Finally, the chapter connects purity to testability. Pure functions are simple to test because their inputs and outputs are explicit, while impure functions require extra setup for hidden dependencies such as the clock, files, or services. To manage this, the chapter recommends isolating I/O at the boundaries of the program and keeping the core logic pure. It explores both interface-based dependency injection and simpler function- or value-based approaches, showing how to make code deterministic, easier to test, and less burdened by unnecessary boilerplate.
RecomputeTotal mutates its input, and the caller depends on this side effect.
RecomputeTotal refactored to explicitly return all the information it computes
Parallel processing of data in a list
When processing the list in parallel, multiple threads access the counter concurrently.
A program where I/O can easily be isolated. The core logic performing format conversion can be kept pure.
Business scenario: validating a transfer request
Testing a pure function is easy: you simply provide inputs and verify that the outputs are as expected.
Testing an impure function. You need to set up and assert against more than just the function inputs and output.
Summary
- Compared to mathematical functions, programming functions are more difficult to reason about because their output may depend on variables other than their input arguments.
- Side effects include state mutation, throwing exceptions, and I/O.
- Functions without side effects are called pure. These functions do nothing other than return a value that depends solely on their input arguments.
- Pure functions can be more readily optimized and tested than impure ones, and they can be used more reliably in concurrent scenarios. You should prefer pure functions whenever possible.
- Unlike other side effects, I/O can’t be avoided, but you can still isolate the parts of your application that perform I/O in order to reduce the footprint of impure code.
Functional Programming in C#, Second Edition ebook for free