Psst! Watch Phototropism, my new experiment.

Replace booleans with two-cased enumerations


When programming, we often describe state of objects in terms of boolean properties, on/off switches. Using a true/false value is an easy solution, the one at hand, but it might cause problems when our applications evolve. Next time you are about to use a boolean value to describe state, consider creating a two-cased enumeration for that purpose. Especially, if your programming language has decent support for enums. (In Swift, for example, enums are a pleasure to work with. In Python, not so much.)

I’m writing this post because, admittedly, I don’t use enumerations often enough. They just aren’t as easy to use as booleans. They have friction: you have to define enums before you can use them. You have to stop for a minute, think about what and how you want to describe, where to put the enum definition, etc. It’s an entry fee you have to pay, but it buys you a tool that I think is both more powerful and simpler than booleans in a few important ways:

As you can see, enumerations are a useful tool to have in your tool box. Use them to your advantage.