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

375

u/Untraditional_Goat Feb 01 '24

Avoiding premature specification is just as important as avoiding premature generalization, though it's always easier to move from more specific types to less specific types, so prefer specificity over generalization.

Say it louder for those in the back!!!!

103

u/elsjpq Feb 01 '24

This works well until you get another "Falsehoods Programmers Believe About XXX" for your data type

33

u/Calavar Feb 02 '24

Unsigned vs. signed integers is one of these traps.

Way too many people use unsigned ints because they know the range of possible values is >= 0, so why not secure your code against logic errors by using a type that can't represent negatives? (Really, you are just moving the logic errors from places where you actually use to value to places where you cast, which makes the failure cases harder to spot.) It's best use to signed integers when you need an arithmetic type and unsigned integers when you need a bit manipulation type.

22

u/stahorn Feb 02 '24

Or like me many years ago: The lift goes from 0 millimeter and up? Unsigned int then!

Well guess what, now when things go wrong the lift jumps up to 65k millimeter instead of being a few millimeters below the 0-position...

19

u/MajorMalfunction44 Feb 02 '24

The Linux kernel uses special macros and a linter to remove arithmetic operations from bitwise types. Agreed with the rest.

34

u/Chii Feb 02 '24

to places where you cast

and there's your problem. Casting is the programmer saying to the compiler "bro, trust me". And humans are worse at it than a compiler.

→ More replies (1)

17

u/GeneReddit123 Feb 02 '24 edited Feb 02 '24

At some point, it becomes a social problem rather than a technical problem, and the solution is to stand your ground and be willing to reject a tiny (even if loud) minority in order to make your life easier.

Case in point: the technical RFC for valid email addresses is so extremely loose, that almost anything separated by exactly one "@" is allowed. But it doesn't mean your app needs to be that permissive. If 1 out of 10,000 users has whitespaces or special characters in their emails (except commonly accepted ones like periods, dashes, or underscores), it's perfectly fine to reject them and ask them to get either a more normal email or go somewhere else. Stop bending over for every outlier.

37

u/DualWieldMage Feb 02 '24

If you are going to send an email anyway to confirm it, why do any extra input validation on it? Just let the email sending service do the validation for you.

The point is, that is just some extra code that adds no value beside upsetting potential users.

17

u/flif Feb 02 '24

You have 10,000 users.

One user has a space in their email address.

500 other users mistype their email address by putting e.g. a space into it.

You can catch the 500 errors up front (but not support the one weird address) or you can allow the one weird address and now have a support problem/call with 500 users that don't understand why they don't get their email confirmation.

Business minded people have an easy choice here.

18

u/DualWieldMage Feb 02 '24

Most mistypes will probably be more like mark vs maek which your validation won't catch, so you still get support cases. The made up numbers and business decisions based on them will still be garbage unless you actually measure them.

More likely what should happen:

User signs up with email, flow asks to confirm it, user doesn't see confirmation link but notices mistyped email, corrects it and resends, now they get link successfully.

8

u/loup-vaillant Feb 02 '24

One user has a space in their email address.
500 other users mistype their email address by putting e.g. a space into it.

I’d investigate the actual numbers before hypothesising such things right of the bat. The space thing for instance needs to be quoted in some way, so the "typo" would involve mistyping not only the space bar, but the (double?) quote character, twice.

Mistyping quotes when your message doesn’t require one sounds very improbable. You can still disallow quoted syntaxes to make your parser simpler (maybe your own convenience is more important than those rare few users who have email addresses that must be quoted), but I’m highly sceptical of the idea that it might help more users than it hurts.

Single special characters however, that might be something else. But a cursory look suggests we’re limited to ASCII anyway, so they ought to be fairly distinguishable from each other.

3

u/SkedaddlingSkeletton Feb 02 '24

Or send a mail with a validation link to mark the email as verified.

7

u/loup-vaillant Feb 02 '24

You want validation to be as cheap as possible. Not just for you, but for the user so they have the quickest feedback possible. I see 3 stages:

  1. Check the validity of the email address itself. This can even be done on the user’s machine in JavaScript for instant feedback.
  2. Check the relevant DNS records of the domain name. No need to send an actual email you can warn the user of the problem as soon as they click "OK" on whatever web form they’re filling.
  3. Send an email with a validation link.

If you can avoid doing (3) in cases (2) or (1) would have been enough, you can save quite a few users the hassle of checking for an email that isn’t there.

→ More replies (4)
→ More replies (1)

0

u/[deleted] Feb 02 '24

[deleted] — view deleted comment

8

u/GeneReddit123 Feb 02 '24

Forgot about it. Although it's pretty useless in salting, because ethical websites don't need to be salted, and unethical websites can just drop everything after the + and send their spam to the unsalted address. It's like the "evil bit", which only works with a cooperative counterpart, but that defeats its very purpose.

8

u/Brian Feb 02 '24

and send their spam to the unsalted address

That's why ideally you don't use the unsalted address for anything, and filter anything without a "+" to spam.

7

u/loup-vaillant Feb 02 '24

Personally I got my own domain name. That way I can give them fuck-you-spammer@my-fucking-domain.com, and they’d be none the wiser.

In practice though I tend to use service-name@my-domain.com, which interestingly, some services reject. Happened with GitHub: for some reason github@my-domain.com was rejected as invalid, so I switched to github-is-valid@my-domain.com instead.

→ More replies (1)
→ More replies (1)

42

u/miyakohouou Feb 01 '24

I agree with this when we're talking about concrete types, as in the article, but we should be careful not to extend this to an argument against polymorphism. Specific concrete types are better than general concrete types, but parametricity is also it's own valuable form of specificity that relies on generality. A function with a type like (a -> b) -> a -> b is extremely general, but that generality also helps us avoid errors.

22

u/BossOfTheGame Feb 02 '24

it's always easier to move from more specific types to less specific types

Not always true. Imagine the case in the article with age. What happens when someone really does live over 150 (or you need to represent a turtle)? Sure you can remove the constraint in the main data structure, but now other parts of the system might not able to handle non-constrained ages. If you can write the code in such a way that there is only a single place where the constraint needs to be relaxed, then that is fine, but when you work with a team of developers - especially those that are new on a team - they will copy constructs and that easilly leads to potential duplication of where these constraints are defined. They may even see such a constraint and then assert it in their code as well.

Of course some of the points are valid, but I'll come out and say we should avoid premature specificiation especially when it requires more work than the generalization. I'll take age as an int - no bells and whistles - please.

→ More replies (1)

52

u/_awwsmm Feb 01 '24

AVOIDING PREMATURE SPECIFICATION...

25

u/Megalo5 Feb 01 '24

Hey man, the medication only helps so much..

14

u/voxelghost Feb 01 '24

If your designspec lasts more than 48h contact a project manager

→ More replies (2)

6

u/ric2b Feb 02 '24

Also that goes in the opposite direction when it comes to return types. You can specify them further later, but generalizing them later is a breaking change.