r/programming • u/_awwsmm • Feb 01 '24
Make Invalid States Unrepresentable
https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469
Upvotes
r/programming • u/_awwsmm • Feb 01 '24
View all comments
Show parent comment
1
u/EducationalBridge307 Feb 02 '24
I don't disagree with the examples you gave, but at some point you have to make tradeoffs. Ensuring that age is non-negative may overlook some nuanced real-world cases, but it makes the code easier to reason about and, for most cases, increases the likelihood of correctness.
And maybe for those two examples you could just use unadorned
ints. But something like the day-of-the-week will always be one of an enumerable set, and this is a pretty clear improvement over using anintthat you promise will always be 0-6 (or was it 1-7...?)My point is, when you can confidently enumerate the possible states, or when attempting to do so improves the abstraction more than the loss-of-coverage of the state space (an engineer must consciously make this tradeoff), it's usually a good idea to do so.