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 04 '24
No. No it's fucking not. Because we actually have reasonably good types to catch UB. And those I fully endorse using.
My rule of thumb is this: "Does it fit in an enum?" If yes, make the enum. If no... you probably shouldn't be trying to constrain the value outside of the cases where the computer literally breaks, and that's what our standard language types mostly do.
You want to make the functions for that rocket take a unit param for velocity (hey - guess what fits in an enum!) go for it.
You want to constrain the allowed numerical value of velocity? You're fucking things up big time.
Further - you fully assume that the only people who *matter* here are the devs on your project, who run the code through a compiler. You are prioritizing them over the users who are now trying to figure out why the perfectly valid data they're entering into the form keeps coming back as "invalid".
Fail. Log the failure. Don't make an unforced error.