Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Last active January 5, 2018 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelQ/1bb54e8f39374c92c41b47c7d6c4f459 to your computer and use it in GitHub Desktop.
Save JoelQ/1bb54e8f39374c92c41b47c7d6c4f459 to your computer and use it in GitHub Desktop.

Working with Maybe

One of the selling points of Elm is that you cannot get null errors. Maybe is what allows Elm to avoid this billion-dollar problem entirely. It's a beautiful solution.

But it doesn't take too long before your code is one long ugly nested case statement checking Maybes at every level. Surely this can't be right. There must be a better way.

We have an array of tools at our disposal to solve this problem, from simple convenience functions all the way to eliminating Maybe altogether in favor of other constructs. Join me on a whirlwind tour of these solutions and take back control of your codebase.

Details

This talk is aimed more at the beginner/intermediate crowd. I've often been asked to help clean up deeply nested Maybe code. There are three broad approaches to dealing with code that's been taken over by Maybe:

  1. Use utility functions like map and andThen to eliminate case statements
  2. Decouple the parts of your app that process values from those that handle optionality.
  3. Choose to model the situation using some other constructs. Data that has multiple shapes if usually better modeled with union types than Maybes.

The goal of this talk is to help people confidently use Maybe while preventing it from taking over the whole codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment