Boolean flags multiply states
High-assurance frontends (clinical dosing calculators, radiation oncology planners, medical telemetry consoles) often track state with separate boolean flags such as isLoading, isError and isSubmitting. The combinations grow exponentially. Four independent flags allow 16 states, and only four or five of them are valid clinical operations.
Sooner or later the impossible states show up in production: a stale error dialog drawn over a spinning loader, or a submit button that re-enables while the request is still in flight. In a consumer web app, the user refreshes the page. In regulated medical software, an ambiguous screen can lead to a duplicate dispense or a dose calculation nobody verified.
Statecharts as the contract
A statechart is a hierarchical state machine. Define the UI as one and its transitions become deterministic and closed. Every valid state, every permitted event and the full transition matrix are written down as the contract:
type DosingWorkflowState =
| { status: "idle" }
| { status: "calculating"; inputHash: string }
| { status: "verified"; dosageMg: number; auditSig: string }
| { status: "flagged_contraindication"; code: string; rationale: string };
type DosingWorkflowEvent =
| { type: "INPUT_SUBMITTED"; patientWeightKg: number; serumCreatinine: number }
| { type: "CALCULATION_SUCCEEDED"; dosageMg: number; auditSig: string }
| { type: "CONTRAINDICATION_DETECTED"; code: string; rationale: string }
| { type: "RESET" };
Hierarchy
Statecharts add hierarchy, orthogonal regions and guarded transitions to plain finite state machines. A multi-step wizard doesn't have to flatten into dozens of loose states: nested compound states keep their own invariants without leaking them into the parent.
Checking the model
A statechart is a formal object, so it can be checked before any code runs in a browser. Automated state space exploration tests reachability across every possible sequence of user interactions:
- No deadlocks: no sequence of states can trap the user without a reset or an exit.
- Safety: the app can never enter
dispensedunless it passed throughverifiedwith a cryptographic audit signature that hadn't expired. - Event coverage: an unexpected event that arrives during network lag is either handled or dropped, never left to throw at runtime.
Race conditions removed by construction
Take the classic typeahead race. A user types query A, then query B. Response B arrives first, and then the stale response A overwrites the view. In a statechart, entering the searching state cancels any pending actor invocations from earlier queries, so the race can't happen and there is no cancellation token to bolt on.
What adoption takes
Statecharts ask you to think about discrete transitions before you write any event callbacks in React hooks, and that takes some getting used to. The payoff grows over the life of a system. When clinical requirements change, updating the transition matrix shows the missing edges and unreachable states at compile time.
To see formal verification applied to interactive directed acyclic graphs (DAGs), try the Proof Studio, or read about the compiler architecture in the CRF-XL Case Study. The NeuroRecon 3D Brain Studio checks its simulation invariants live.