make impossible states unrepresentable

Planted Oct 2021 Tended 2 years ago

Contents

Software not working when it’s in a bad state is the reason why “turning it off and back on again” (which resets the state to a known-good value) is the solution to so many technical issues. So how can we prevent our software from getting in a bad state in the first place? By making invalid states impossible.

  • In application code, one approach to this is using types
  • Prefer enums to represent state instead of multiple, possibly-conflicting columns
  • In a relational database, use unique, not-null, and foreign-key constraints to prevent invalid data from even being inserted

Further Reading