# The Boolean Explosion

> Understand why independent boolean flags lead to unmaintainable 'Impossible States'.

Subject: State Management | Lesson 16 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/impossible-states-boolean-explosion
Previous: [Redux Toolkit: Listener Middleware](https://codewithanimation.in/state-management/rtk-createlistenermiddleware.md) | Next: [Finite State Machines & Actors](https://codewithanimation.in/state-management/xstate-fsm-and-actors.md)

## What is the Boolean Explosion?

When developers use multiple independent boolean flags to track application state (like `isIdle`, `isLoading`, `isSuccess`, `isError`), the total number of possible state permutations grows exponentially.

Just 4 independent booleans create $2^4 = 16$ possible state combinations! This exponential growth in complexity is known as the Boolean Explosion or 'Boolean Salad'.

## The Impossible State Problem

Out of those 16 possible combinations, only 4 are actually logically valid (an app cannot be 'Idle' and 'Loading' at the same time).

This means 75% of your state space consists of 'Impossible States': combinations of variables that are syntactically allowed by the code, but logically contradictory to the business rules.

## How do Impossible States Happen?

Impossible states occur due to uncoordinated state updates. When using independent `useState` hooks, setting one flag requires you to manually remember to reset all the others.

If an API request fails and you call `setIsError(true)` but forget to also call `setIsLoading(false)`, your application is now permanently stuck in a contradictory state where it is both loading and erroring simultaneously.

## Common mistake: The Rise of Fragile Guard Clauses

When your state model permits impossible combinations, your UI components are forced to defend themselves. You will find your codebase littered with fragile guard clauses like `if (!isLoading && !isError && isSuccess && data) { ... }`.

If you ever need to add a 5th flag (like `isRefetching`), you have to manually audit and update every single conditional check across the entire application.

## Tip: Make Impossible States Impossible

Instead of independent booleans, use Discriminated Unions or Finite State Machines. A single string literal like `status: 'idle' | 'loading' | 'success' | 'error'` inherently compresses the state space down from 16 to exactly 4 valid states, preventing bugs at the structural level.

## Try it: Auto Demo: A Buggy Fetch Sequence

Tap the play button below to watch what happens when a developer forgets to clear the `isLoading` flag when setting `isError` to true. Watch the UI glitch out!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/impossible-states-boolean-explosion

## Check yourself

### Question 1

If a developer uses 5 independent boolean flags to track the status of a form submission, how many possible state combinations exist?

- A. 5
- B. 10
- C. 32

Correct answer: C

Each boolean has 2 states (true or false). The total combinatorial state space is $2^n$. Therefore, $2^5 = 32$ possible states.

- Why not A: That is the number of valid states if modeled correctly, but independent booleans grow exponentially.

## Remember this

- Independent booleans cause the state space to explode exponentially ($2^n$).
- Impossible states are valid syntactically but logically contradictory (e.g. `isLoading: true` and `isError: true`).
- Uncoordinated state updates in `useState` are the primary cause of transient UI bugs.
- Solve the boolean explosion by using explicit string literal statuses or Finite State Machines.
