Notes

Beyond boolean hell: why isLoading && !hasError fails

12 October 2026 2 min read

Nested booleans create impossible states nobody designed for. A small state machine keeps the interface out of them.

  • State machines
  • Resilience
  • TypeScript

Every interface starts with a boolean. isLoading. Then someone needs an error message, so hasError joins it. Then a success toast arrives, and with it isSuccess. Three flags later the interface has eight possible states. The design covers four of them.

Before

Three flags

The interface renders

Nothing, idle

After

One status

The interface renders

Nothing, idle

Always exactly one of four states.

Live demo. Everything runs in your browser. Now try to break it.
BooleanHell.svelte, 121 lines

Tick isLoading and hasError together on the left. The spinner and the error banner now render at the same time. Nobody designed that screen and no test covers it, yet it ships, because nothing in the code makes it impossible.

The arithmetic of flags

n booleans give you 2ⁿ combinations. Three flags are eight states, five flags are thirty-two. You wrote conditions for the handful you imagined, and every other combination is an "impossible state" in exactly one sense: impossible in your head, perfectly possible at runtime.

What this looks like in production

  • The stale error. The user retries, the request succeeds, and the old error banner stays because one setHasError(false) was forgotten.
  • The double submit. isLoading is set after an await, so a fast second tap slips through and the order is placed twice.
  • The flash. Success appears for one frame, then the spinner, because two setters ran in the wrong order.

These are all the same bug: a flag that someone forgot to reset. The more flags, the more resets, and the more places to forget one.

One variable instead of many

A finite state machine replaces the flags with a single status that can only hold one value at a time. The impossible states are not handled, they are unrepresentable. Transitions are written down once:

type Status = 'idle' | 'loading' | 'success' | 'error';
type Event = 'submit' | 'resolve' | 'reject' | 'reset';

const transitions: Record<Status, Partial<Record<Event, Status>>> = {
  idle: { submit: 'loading' },
  loading: { resolve: 'success', reject: 'error' },
  success: { reset: 'idle' },
  error: { submit: 'loading', reset: 'idle' }
};

function next(status: Status, event: Event): Status {
  return transitions[status][event] ?? status;
}

Look at loading. It has no submit transition, so a second tap during loading does nothing. The double submit is gone without a single if. Use the buttons on the right in the demo and try to reach a spinner and an error together. You can't.

Every state gets one voice

A status that is always exactly one thing is also easier to make accessible. Each state maps to one announcement and one set of attributes: aria-busy while loading, a role="status" region that says "Order placed" on success, an alert for the error. With flags you have to decide what a screen reader should say when two of them are true. With a status the question never comes up.

What it costs

More typing up front. For a single on/off toggle, a boolean is the right tool. A useful rule: when two flags cannot both be true, you do not have two flags, you have one state with two values. Reach for a library like XState when you need nested states, timers or parallel regions. For most buttons and forms, a ten-line reducer is enough.

The design question hiding underneath

The real work is deciding which states exist. Write them down once, in one place, and everyone reads from it: designers drawing the screens, engineers writing the transitions, QA writing the cases, and the person relying on a screen reader. That list is the design.

Related

How bright was it?

Five lamps and an optional line. Feedback, a bug, an idea, anything. I read every one and sum them up once a month.

Pick a lamp

Stored without name, address or cookie, and deleted after a year. Details in the privacy notice.