I get a lot of requests about data modeling resources in Elm. I’ve written and spoken extensively on the topic as well as collected material from around the community. Over time, I’ve accumulated a list of links to various articles and talks that I share with people. Here is that list, made public, along with some brief commentary.
All about types
Most data modeling in Elm is done using its excellent type system. These articles explain various aspects of the type system and the tools that it gives you for modeling.
- ✍Elm Type Glossary (by Joël Quenneville)
- ✍Advanced Types in Elm (4-part series by Charlie Koster)
- ✍Types Without Values (by Joël Quenneville)
Eliminating impossible states
🎥Making Impossible States Impossible is one of the defining talks of the Elm community. Richard Feldman showcases some simple type design tricks that make it impossible to even represent invalid states. This approach is at the heart of a lot of domain modeling in Elm. The official Elm guide has an appendix on ✍Types as Sets that digs into the more theoretical ideas of why this works.
Other resources on this topic include:
- ✍Shaping Values with Types (by Josh Clayton)
- 🎥The Life of a File (by Evan Czaplicki)
- 🎥Immutable Relational Data (by Richard Feldman)
Elm comes with many primitive types such as
these have their place, it’s easy to overuse them - a code smell known as
primitive obsession. In many cases, it’s better to model domain concepts with
a richer value using a custom type.
- ✍Elm Slays a UI Antipattern (by Kris Jenkins)
- ✍Booleans and Enums (by Joël Quenneville)
- 🎥Solving the Boolean Identity Crisis (by Jeremy Fairbank)
- ✍Modeling with Union Types (by Joël Quenneville)
- 🎥A Number by Any Other Name (by Joël Quenneville)
Using the compiler to check for invariants
Business systems are full of invariants. Many of these can be encoded into a program’s type system such that the compiler prevents you from writing code that breaks certain rules. Nifty!
- ✍Using Elm Types to Prevent Logging SSNs (by Dillon Kearns)
- ✍Page State in Elm (by Alex Korban)
- ✍Modeling Currency in Elm Using Phantom Types (by Joël Quenneville)
- ✍Compile-Time Checks on Values in a List Using Phantom Types (by Alex Korban)
- ✍Fewer Operations on Custom Types is Valuable (by Joël Quenneville)
Often in a system, entities will start as permissive, loosely formed data and progress to a stricter more well-defined entity. Modeling this journey in the type system is often called type narrowing. The canonical resource on the topic is Alexis King’s ✍Parse, Don’t Validate.
This can take an incremental approach as explored in ✍A Broader Take on Parsing where an application is modeled as a series of layers and each time data moves up one of the layers, its type gets narrowed.
General domain modeling
Still looking for more domain modeling ideas? These resources explore the general concept of domain modeling. You might come away with some new perspectives!
- 🎥Domain Modeling Made Functional (by Scott Wlaschin, talk is in F# but concepts carry over to Elm very well)
- ✍Models that Match Reality (by Joël Quenneville)
- 🎥Make Data Structures (by Richard Feldman)