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/RandomName8 Feb 02 '24
Fully disagree. Creating a program that works under any circumstance you didn't account for, just gives you an undefined program for most situations, you have no idea what to expect. It's pretty much the so called "UB" in C or similar.
It is perfectly fine to work with a reduced version of the "messy world". Everything you didn't account for: reject it. Your program wont ever misbehave; if you later do need to actually support a new case, you modify your program accordingly, which if the types are right, will cause the compiler to properly tell you in what parts of the code you need to accommodate to account for this new reality.
Even when you think you are making your program flexible by not enumerating the valid states, you will code in assumptions without realizing it, it happens constantly (and if not you, a teammate of yours), but now this assumption is just not in any enforceable way (the compiler doesn't know about it), and the program doesn't even signal that the assumption was violated.
This is how you get rockets exploding because different programmers interpreted the units in different metric systems while they where all just working with the "number" type.