Choose Wisely
Sometimes—especially for the newly arrived Clojure programmer—it can be difficult to know which container to reach for in any given situation. After all, along with vars we now have atoms and refs and agents. It is an embarrassment of programming riches. Fortunately there are some straightforward guidelines that you can follow.
The first rule: If your value is mostly stable over the life of the program, perhaps with some thread-local variations, then put it in a var.
The second rule: If you have a number of mutable values that need to be updated together—and those updates don’t involve side effects—then use refs. Remember, it’s refs that give you that databaselike ability to build transactions.
The third rule: If there are side ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access