Advanced Elm

Impossible States

Richard Feldman

Richard Feldman

Vendr, Inc.
Advanced Elm

Check out a free preview of the full Advanced Elm course

The "Impossible States" Lesson is part of the full, Advanced Elm course featured in this preview video. Here's what you'd learn in this lesson:

When we have multiple sources of truth for the same information, our application can end up in states that ought to be impossible. Richard gives an example of how this can affect the tabs in the sample application.


Transcript from the "Impossible States" Lesson

>> Richard Feldman: Sources of truth, we're gonna talk about a few different topics here. One is impossible states and the making impossible there of. Derived data, authentication and JavaScript. Okay, lets start with some impossible states. So when we're talking about sources of truth, generally speaking these break down into a few different categories.

One of which is essentially do I have agreement on where a particular piece of information lives. So let's say I have a model that has selectedTag which is a string to represent which particular tag I have selected. And then let say for some reason. I have another field, I call theSelectedTag which is also a string.

Okay, which one's right? They're both claiming to represent the currently selected tag. We have sort of multiple alleged sources of truth, here. It's even worse if I'm actually using them in different places, like in some cases I'm setting the one but not the other. In another case as I'm reading the one but not the other.

That's going to lead to all sorts of nasty blogs if I'm doing that. So hopefully, we can avoid something like this when we're designing our models. But usually, it's trickier than that. Usually, it's less obvious. It's not that you have two things that are the same name but rather that you have some piece of data that is nested somewhere in some record or nested in some custom type that represents the same piece of information as some other piece of data that's nested somewhere else in some other thing.

A classic example of this is the current user or the current username. Sometimes that might appear in multiple different places and as long as they all agree, maybe there's no problem. But if they can get out of sync, and then different parts of the UI think that we have a different current username, that can cause a problem.

>> Richard Feldman: So when I first built out that tab box that we saw a moment ago, this is what I had as a design. I'm sorry not that part of the tab box, but rather this part. This is the The global feed, your feed, and then the tag if you have one of those tags selected.

So each of these is a tab. I could represent these in this way which I have. A name which is a string, so Your Feed, Global Feed, or perhaps the name of the tag that we've selected, so dragons or happiness or elm, or whatever those things may be.

And I can store a Boolean for whether or not it's the active tab. The problem with this is that it allows an impossible state to be represented. It is entirely possible that we end up with multiple tabs that have active set to true as is evidence in the bottom here.

And this is not a contrived example because actually when I was browsing their react reducts example for this project, the real world app, I encountered this. In fact, this just, I don't remember how I got it, but I was just clicking around and somehow I ended up with two different tabs selected.

This is I would guess probably a symptom of the fact that in React it's encouraged to use components that have local state. They manage their own state. In this case, that's a source of potential bugs in the form of multiple sources of truth. Each of them thinks that they are the active tab but in fact there can only be one active tab.

That is one of our rules for these tabs. And so if each of them thinks that they can potentially be it without having any innate coordination with the others, that means they can potentially end up in a situation where we have multiple competing sources of truth and bugs as a consequence like this.

Where we ended up with this is a different structure. So we have type tab and this represents which tab we're on. It sort of inverts that and says you know what? We're only gonna have one source of truth for what tab we're on. We're going to enumerate the three possibilities which is to say your feed is the active tab.

The global feed is the active tab. Or we have a particular tag feed and if so, we also have the tag that is associated with that, so dragons or whatever else. So this is a custom type that enumerates all of the possible states completely, and it's one source of truth for all of them.

So we don't have the possibility of competition just stores, this is the tab that we're on like this. So this becomes impossible. This is an impossible state that we have made impossible by virtue of modeling our data differently. As a consequence we've now reduced our multiple sources of truth down to just one.

I did a whole talk on this concept a couple of years ago. It's called "Making Impossible States Impossible". It talks about techniques like this in a variety of different scenarios so we won't go into too much detail on that right now except to note that this is one form of potential multiple sources of truth causing problems is impossible states.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now