Thinking in State: The Tao of Frontend Development

One of the side effects of working with redux is that it forces you to think about your application in terms of state instead of thinking in terms of user workflow. While state might seem like an implementation detail, with experience it becomes more a part of the design process, allowing us to accurately predict "sad path" situations that may arise when the app is in a particular state.

For example, I once worked on an app that allowed users to perform real-estate searches and then favourite properties. If the user was not logged in at the time they clicked the favourite button/heart, they would be prompted to log in (and then the property would be favourited).

The description of this feature, phrased as user stories, might look like this:

As a user, I need a favourite button, so that I can keep track of properties that are interesting to me.

As a visitor (user without an account or not logged in), I need to be prompted to create an account when trying to perform actions that require one, so that I can gain all the benefits of being a user.

This seems simple at first glance. Maybe it would only take a couple of hours to implement, at most. But up until we've neglected to account for the state of the app at the time the favourite is clicked. Obviously, if the property has already been favourited, it can't be favourited again, and (probably) clicking the same button should unfavourite it.

Given-when-thens get us little closer to describing state:

Given a user is logged in and a property is not favourited, when the user clicks the favourite button, the property should be favourited.

Given a user is logged in a property is favourited, when the user clicks the favourite button, the property should be unfavourited.

Given a user is not logged in, when the user clicks the favourite button, the user should be prompted to log in and the property favourited.

Put differently, there are two pieces of information we need to take action: whether the user is logged in, and whether the current property is favourited.

There's also one other cardinal rule of frontend development we should take into account:

TRUST NO ONE.

For example, a user might click the favourite button, be prompted to log in and then get annoyed and click away from the modal or whatever we've used to interrupt them. Worse, they might log in to an existing account, and then we might find that they've already favourited the property in question. You can't trust the user.

You also can't trust the network. Knowing whether the user is logged in, whether they've favourited any properties, the act of favouriting/unfavouriting the property, and the act of creating an account/logging in, all require network requests, which take time and can fail. What do we do if the user attempts to favourite a property and completes the registration form only to have the network fail at that point?

Procedurally it might be nightmare to describe all the different paths, but in terms of state it actually becomes manageable. For a task like this I would start by designing the state, which should contain the following items:

    userLoadState: 0,
    userId: null,
    loginModal: false,
    properties: [{
      id: '',
      favourite: false,
      favouriteLoadState: 0,
      // ... other data ...
    }]

Note I use loadState fairly ubiquitously with the following convention: 0 - not requested, 1 - loading, 2 - loaded, -1 - error. Representing these values numerically has the advantage that we test things with a simple comparison - ie if we need to know if already have a particular piece of information we test loadState === 2 if we want to know if we should try to get something we can do loadState < 1.

From the description of the state, our "sad-path" cases and their solutions become apparent:

Conclusion

By "thinking in state" we've anticipated a number of "sad path" cases and hopefully saved ourselves some headaches down the road. If we sketch out the state needed to handle a particular feature we can do a better job of estimating the time required to implement something - and sometimes even realized that we may need to send things back to design. This insight comes from realizing that all possible states need to be accounted for.

Like this post? Start a conversation, browse my other writing, or view my portfolio.