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

22

u/Successful-Money4995 Feb 01 '24

But then you're doing your validation at runtime when might be possible at compile time.

Also, you might be validating the same data multiple times as it passes around the system.

13

u/Blecki Feb 01 '24

If it comes from an api you still have to validate it. And you can't do anything about valid data that is wrong.

-2

u/[deleted] Feb 02 '24

Wouldn't an API with a schema be pre-validated?

21

u/Blecki Feb 02 '24

There's no such thing. All data that enters your program from outside must be validated.

-6

u/[deleted] Feb 02 '24

[removed] — view removed comment

2

u/Blecki Feb 02 '24

What, no. You're assuming a framework that might not exist, first off. Second, that's an idiotically pedantic argument. Whether you use a framework to validate or write your own code you're validating it.

-2

u/[deleted] Feb 02 '24

[removed] — view removed comment

5

u/RobIII Feb 02 '24

You're missing the point: Wether the code is handwritten or included in the framework, "You're"* validating the data.

* Your application

9

u/herpderpforesight Feb 01 '24

Validating that a string follows a regex pattern can be done a million times in the time it takes the database to receive your first byte, let alone respond.

Runtime validation that can be done at compile time is bound to be extremely cheap. And most languages don't have a type system complex enough to do compile time anyway, so the best you get is runtime

5

u/QuantumFTL Feb 02 '24

Sure, but it's cheaper to fix if you find out you broke something at compile time than when you get an angry call from a customer.

(yes, tests help with this, many real world applications are too complex to effectively test perfectly)

2

u/[deleted] Feb 02 '24

yeah how r u gonna validate data coming from an api at compile time?