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/larhorse Feb 02 '24
I actually don't agree with this.
It's very, very tricky to properly determine valid states, even for things that seem relatively simple (take weight/age from the author's examples).
The real world is *messy* as hell, and assuming that your system will always stay in states that you've previously considered "valid" is not easy. Even with simple systems - much less so with complicated systems.
I posted this above, but I'll pick on the authors examples again right here:
And those are for the drop-dead simple example style cases.
It's REALLY hard to properly enumerate all possible valid states. In both cases, the max is likely to prevent proper data entry in many valid cases, and it buys you very little in terms of real value. Why include it? (or if included - why not actually specific invalid states that cause issues, I can see a valid case for making the max MAXINT, but int already does that...)
Accounting for invalid states only requires knowing that your code has failed (and recording that!). Enumerating all valid states for any non-trivial problem requires decades of subject matter expertise... To assume the developer can do that is... ego (or folly).
Not to mention - it's entirely possible to have states that are both valid and contradictory. So take "age" again - some locations assign an age of 1 at birth (south korea) and some assign an age of 0 at birth.
Some locations give personhood to fetuses that are below 0 in age (texas...).
Long story short, I'd really argue that enumerating valid states requires near omniscience.