# Parse, Don’t Validate - lexi-lambda.github.io Synced: [[2023_11_30]] 6:03 AM Last Highlighted: [[2023_09_13]] Tags: [[software]] ![rw-book-cover](https://news.ycombinator.com/favicon.ico) ## Highlights [[2023_09_13]] [View Highlight](https://read.readwise.io/read/01ha6mdgx7w6exxd4t21qk1tch) > These two functions are nearly identical: they check if the provided list is empty, and if it is, they abort the program with an error message. The difference lies entirely in the return type: `validateNonEmpty` always returns `()`, the type that contains no information, but `parseNonEmpty` returns `NonEmpty a`, a refinement of the input type that preserves the knowledge gained in the type system. Both of these functions check the same thing, but `parseNonEmpty` gives the caller access to the information it learned, while `validateNonEmpty` just throws it away. [[2023_09_13]] [View Highlight](https://read.readwise.io/read/01ha6mgxxfp3g7daxsn7x0er0n) > **Use a data structure that makes illegal states unrepresentable.** Model your data using the most precise data structure you reasonably can. If ruling out a particular possibility is too hard using the encoding you are currently using, consider alternate encodings that can express the property you care about more easily. Don’t be afraid to refactor. [[2023_09_13]] [View Highlight](https://read.readwise.io/read/01ha6mnb4bmp7kp4yzba6ya62d) > However, what interests me is how the littlest things seem to get people the most upset. Few people have shouting matches over the best interpretation of quantum mechanics, but friendships will be tested when someone says they just aren’t that into *Star Wars*. One explanation for this phenomenon is simple accessibility: most people aren’t equipped to understand quantum mechanics well enough to argue about it, but almost anyone can have an opinion on which direction the toilet paper is supposed to go.[1](https://lexi-lambda.github.io/blog/2019/10/19/empathy-and-subjective-experience-in-programming-languages/)