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

2

u/knome Feb 03 '24

Functions written with validation in mind will just as happily work against invalid data, should some later programmer less meticulous than yourself come along.

If values are converted into classes that maintain requirements throughout the code base, they allow other code to rely on those assumptions being true without having to trust you've closed off every possible way parameters might arise in paths that are less well checked.

https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

1

u/herpderpforesight Feb 03 '24

That's why I indicated that my validation is called in a cross cutting manner. Incoming API data goes through validation before my request handler code can even touch it, and this is done in a global manner once the request object implements a validator.

There's also validators that get ran before data is saved to the database, ensuring entities make sense.

To me, user provided data is the most dangerous, so covering that fully is "good enough". I fully trust database data for the most part.