Chapter 2. Why function purity matters
This chapter covers
- What makes a function pure or impure
- Why purity matters in concurrent scenarios
- How purity relates to testability
- Reducing the impure footprint of your code
The initial name for this chapter was “The irresistible appeal of purity.” But if it was so irresistible, we’d have more functional programmers, right? Functional programmers, you see, are suckers for pure functions—functions with no side effects. As you’ll see in this chapter, pure functions have some very desirable properties.
Unfortunately, the fascination with pure functions and what you can do with them is partly why FP as a discipline has become disconnected from the industry. As you’ll soon realize, there’s very little purity in most real-world applications. And yet, purity is still relevant in the real world, as I hope to show in this chapter.
We’ll start by looking at what makes a function pure (or impure), and then you’ll see how purity affects a program’s testability and even correctness, especially in concurrent scenarios. I hope that by the end of the chapter you’ll find purity, if not “irresistible,” at least “definitely worth keeping in mind.”