Hall of FameAlexis King201918 min readintermediate
Parse, Don't Validate
Summary
The post argues for “parse, don’t validate”: instead of checking inputs and discarding the information, convert them into richer types that encode the validation result. Using Haskell’s NonEmpty type turns a partial head function into a total one, eliminating redundant runtime checks and catching bugs at compile time.
- Use richer input types (e.g., NonEmpty) to encode invariants rather than returning Maybe and handling impossible cases later.
- Turning a partial function into a total one by strengthening argument types removes redundant checks and improves compile‑time safety.
- Parsing functions return refined types that preserve proof of validation, while validate‑only functions lose that information.
- Compose parsing with existing functions (e.g., head' = fmap head . nonEmpty) to bridge between old and new APIs.
Static‑typed developers should care because encoding invariants in types prevents runtime errors and unnecessary boilerplate.
6/10



