Overview

3 Why function purity matters

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.

FAQ

What is a pure function in the context of this chapter?A pure function is one whose output depends only on its input arguments and that causes no side effects.
What counts as a side effect?Side effects include mutating global state, mutating input arguments, throwing exceptions, and performing I/O such as reading from or writing to the console, filesystem, database, or external services.
Can a function with no side effects still be impure?Yes. If its result depends on something other than its inputs, such as mutable global state, it is still impure.
Can a function whose result depends only on its inputs still be impure?Yes. A function can still be impure if it also has side effects, such as updating global mutable state.
Why are pure functions easier to test?Because they are deterministic and isolated: you can test them by providing inputs and checking that the outputs match expectations, without setting up external state.
Why do pure functions help in concurrent or parallel code?Pure functions are safe to evaluate in any order, including in parallel, because they do not depend on shared mutable state or produce side effects that can interfere with other work.
What can go wrong if you parallelize an impure function?Concurrent execution can lead to race conditions, lost updates, and nondeterministic bugs, especially when multiple threads read and write shared mutable state.
How can you avoid mutating input arguments?Return the computed information to the caller instead of modifying the arguments. If a function computes multiple results, return them explicitly, for example by using a tuple.
Why is I/O difficult to unit test directly?I/O depends on the external world, which can change over time or be unavailable, so tests may not be repeatable and can fail for reasons unrelated to the code logic.
What are some ways to make impure code more testable?You can isolate I/O from pure logic, inject dependencies such as a clock or a function, or replace impure behavior with a predictable stub in tests.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free