A while ago I released a draft blog post and got a lot of good feedback on it (big thanks to everyone who helped out!)
My big takeaway was that trying to explain a concrete programming technique (keeping models accessible to view functions but opaque to update functions) while dancing around the broader philosophy behind why one would want to do this was a big mistake. I now think it’s a better idea to start with a post introducing this philosophy, then follow up with concrete techniques for making it work.
The tl;dr of such a post would be this: state machines are a powerful mental model for Elm applications. Once you start thinking about (and drawing out!) the state machine underneath your app, lots of good things happen:
- Since you’re explicitly specifying application states, impossible states are impossible by definition
- It becomes easy to see why “components” (eg. init/update/view triplets) are are bad abstractions:
- their state machines know too little about the context of the app they’re in, so they need some complicated message passing mechanism to bubble information up (the so-called OutMsg pattern)
- they needlessly couple state machines to particular chunks of DOM
- Better alternative abstractions become apparent, namely small, app-specific state machines for business logic, and reusable “config-only” views
- Armed with the above abstractions, the scaling advice from @evancz and @rtfeldman becomes even more powerful
This perspective has made me personally much more productive with Elm, but because it implies a substantial shift from how we currently talk about Elm apps, I want to get everyone’s input before making too many noises about it in public.
There are movements in the broader frontend world indicating that folks are open to the idea of state machines. In particular, DavidKPiano’s ReactRally talk and this blog post from Adam Solove seem to have been well-received. Take a look at those resources and let me know what you think!
Some open questions:
- How should we approach explaining state machines to beginners?
- Do we need the word “state machine” at all?
- Are there any potentially big risks I’m not seeing?