Elm Radio

011: Parse, Don't Validate


Listen Later

  • Alexis King's article Parse, Don't Validate

the difference between validation and parsing lies almost entirely in how information is preserved

  • Shotgun parsing (original academic paper)
  • Mixing processing and validating data

Shotgun parsing is a programming antipattern whereby parsing and input-validating code is mixed with and spread across processing code—throwing a cloud of checks at the input, and hoping, without any systematic justification, that one or another would catch all the “bad” cases.

Why the term "parse"?

a parser is just a function that consumes less-structured input and produces more-structured output

[...]
some values in the domain do not correspond to any value in the range—so all parsers must have some notion of failure

  • Conditionally return types
  • Don't have to repeatedly check condition
  • Look out for "lowest common denominator" built-in values being passed around (like empty String)
  • Maybe.withDefault might indicate an opportunity to parse

Two ways to use this technique:

  • Weaken return type

  • Strengthen input type

  • Design by contract

  • Elm Radio JSON decoders episode

...more
View all episodesView all episodes
Download on the App Store

Elm RadioBy Dillon Kearns, Jeroen Engels

  • 5
  • 5
  • 5
  • 5
  • 5

5

16 ratings