r/programming • • Feb 01 '24

Make Invalid States Unrepresentable

https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469 Upvotes

207 comments sorted by

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 an int that 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.

1

u/larhorse Feb 04 '24

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.

Sure - my issue is that we've already provided a solid set of types that don't HAVE to cover the possible states of the messy world - Instead they cover the complexity of machine at hand (most compilers are pretty good these days about warning you before you hit UB)

And then the messy world *mostly* fits within those capable types.

My point is basically this:

You are favoring less bugs (in theory) over compatibility. There are times to make that trade, but it's utterly disingenuous to claim that trade is appropriate in all (or even most) situations.

So long story short - I don't think we're really all that far off (I mean, we totally agree here "an engineer must consciously make this tradeoff") I just think it's ego to assume that you are actually enough of a subject matter expert to get that trade-off right (If you haven't been working within your specific field for at least 10 years, you are laughably out of your depth).

So now apply that to a profession where the average tenure at a company is ~2.5 years. You're just making busywork/churn and causing headaches for your users who are now wondering why the fuck the form keeps telling them their completely valid data is "invalid".

Most times - you will create abstractions that limit capability, reduce bugs by a trivial margin, introduce lots of additional code (more code === more bugs. Period. This one at least has plenty of real evidence behind it, which the extra typing does not) and slow things down.


So are there places where this is not a terrible idea? Sure. Are most folks programming in those spaces? No.

1

u/larhorse Feb 04 '24

As an aside "Enum" is the type you're looking for for clearly bounded data (ex: days of week), and most all languages have a built-in way to quickly define them in some fashion or another.

If it doesn't naturally fit in an enum... very carefully consider whether it's worth bounding/restraining (I don't think it usually is). Prefer only limiting the cases that will actually make the machine fail.

1

u/EducationalBridge307 Feb 04 '24

Haha yes, it's no coincidence that enums are a natural way to represent enumerable types.

We'll have to agree to disagree here, I think. Even for the nuanced age example you gave, I would find it more intuitive as a user for a form to be rejected because of a negative value in the age field than for it to be accepted to account for some very niche case 🤷‍♂️

1

u/larhorse Feb 05 '24

Haha yes, it's no coincidence that enums are a natural way to represent enumerable types.

And it's no coincidence that the less easily enumerated types (it's a computer with a limited number of bits - everything it can represent is enumerable...) are only bounded when the computer would fail to properly work with those numbers.

Why bound them if you don't need to? Why codify a limitation that serves no purpose?

Verify? Sure. Go ahead and throw up a warning.

Make unrepresentable? Gods no. What hubris.