Photo by James Harrison on Unsplash

The Elusive Enum Trap

Enums are both powerful and ubiquitous in typed programming languages.

Do not use “Type” in an enum’s name

An enum’s name should make it immediately clear what we would expect its values to be, and to not be.

Made with carbon.now.sh
Made with carbon.now.sh

Take the time to name well

  1. Identify clearly what the domain of your values is.
  2. Think about what the domain is not.

Footnotes

* Enums and Maps to me are like peas in a pod. I believe most switch statements should be replaced with a private map and public accessor. For example:

Made with carbon.now.sh
  • + the lookup is O(1) at runtime**
  • + fewer lines of code
  • + Record<V, N> is statically type-checked to ensure it holds an entry for every V
Made with carbon.now.sh

dubious dev, fintech friend, sloppy skier | he/him

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store