When not to write pure functions
So far, we have seen what pure functions are and what kind of advantages pure functions can provide. But we should at least spend a bit of time thinking about occasions where we might want to sacrifice function purity. Now, if you ask this question to “purists,” the answer to this question is probably along the lines of: “Never, nunca, jamais.” This is fine, and some languages make it pretty easy to write functional code without ever having to sacrifice function purity. But, let’s take a look at a few examples where it makes sense to sacrifice some function purity. Now, before we dive into these examples, let me be the first to acknowledge that all of these supposed problems are circumventable. And yes, a language such as Haskell handles this mostly gracefully.
But we are not programming in Haskell; we are programming in Go. And while Go allows us to write purely functional code if we wish to do so, some things...