Ok, we get it - pure functions are good, side effects and mutable state are not. If we all only ever write pure code we’ll easily develop top quality code and live long and prosper.

Well, actually… there’s a catch. We actually need both side effects and the global state. You know, like, to get stuff done!

This is just a brief list of some impure things a typical software system needs to do:

  • Get the user’s inputs from the UI.
  • Show the outputs on the UI.
  • Store some data in the database, retrieve it and maybe update later.
  • Tell the current time and date.
  • Do something random, e.g. generate a session token.
  • Send an email, notification, SMS or another kind of message.
  • Talk to a different system, e.g. post a tweet via Twitter API.

If we stick to pure functions alone, we’ll never do any of this. So, while purity is good, we actually need impure functions as well.

In the last chapter, we will explore some ways we can reconcile the parts of our code that involve only pure functions with the parts that contain inevitable side effects.