parse-dont-validate

Installation
SKILL.md

Parse, don't validate

Two functions can check the same condition and differ enormously in value:

def validate_non_empty(xs: list[T]) -> None:              # validation
    if not xs: raise ValueError

def parse_non_empty(xs: list[T]) -> NonEmpty[T] | None:   # parsing
    return NonEmpty(xs[0], xs[1:]) if xs else None

The validator checks and discards the information. Every function downstream must re-check or hope. The parser checks and returns a value carrying the proof, so downstream code holding a NonEmpty[T] cannot be wrong about it and head() becomes total.

Validation returns a boolean. Parsing returns a narrower type.

The distinction is Alexis King's, from 2019.

The smell

Installs
1
First Seen
10 days ago
parse-dont-validate — auralshin/coding-skills