Impure functions
If you decided to entirely forgo all kinds of side effects, then your programs would only be able to work with hardcoded inputs and wouldn’t be able to show you the calculated results! Similarly, most web pages would be useless: you wouldn’t be able to make web service calls or update the DOM; you’d only have static pages. And your Node code would be really useless for server-side work, as it wouldn’t be able to perform any I/O.
Reducing side effects is a good goal in FP, but we shouldn’t go overboard! So, let’s think of how to avoid using impure functions, if possible, and how to deal with them if not, looking for the best way to contain or limit their scope.
Avoiding impure functions
Earlier in this chapter, we saw the more common reasons for using impure functions. Let’s now consider how we can reduce the number of impure functions, even if doing away with all of them isn’t really feasible. Basically...