Created June 24, 2021
Updated June 28, 2021

Wrap Early, Unwrap Late

Model data with its ideal form. The sooner you can get it into its ideal type, the better. The later you can turn it into a non-ideal type, the better.

Examples

Wrap Early#

  • Run a JSON decoder into an Elm value immediately
  • Abstract a decoder with an [Opaque Type]
  • Use elm-graphql Custom Scalar Codecs to directly map your custom scalars into their ideal types that represent the constraints of the custom scalar

Unwrap Late#

  • Pass a Posix.Time to a function - only turn it into an ISO 8601 String at the last second

Why It Matters#

  • It models the constraints better
  • Harder to pass the wrong value somewhere
  • More semantic information for reading through your code paths
  • When you have the ideal data, you can do more with it - like sorting Posix times easily with timezones, compared to doing so with Strings which could easily be sorted incorrectly without you realizing it (just by doing an alphabetical sort with List.sort)

Sign up to get my latest Elm posts and course notifications in your inbox.

Incremental Elm



Pure Elm content. Unsubscribe any time.