State

Escrito por Darjus Kazakevic · revisado el

Make the allowed transitions something you can read.

An order status starts as a string. Soon cancel(), ship() and refund() each check it in their own way, and nobody can say for sure whether a shipped order can be cancelled. The state machine exists, but only spread across if statements.

The State pattern gives each status its own type and puts the transitions on it. In TypeScript the lightest form is a discriminated union (a union of object types with a shared kind field) plus one function per transition. The cost is more types, which is wasted on two or three simple statuses.

En esencia — Give each status its own type that holds only the actions allowed from it, so the state machine is visible and illegal moves are hard to write.

Cuándo recurrir a él

Recurre a él cuando

  • A status field is checked in many methods, and not in the same way
  • Nobody can say for sure whether a shipped order can be cancelled
  • Each status allows different actions, not only different data

Reconsidéralo cuando

  • Two or three states whose rules fit in one small switch
  • States that differ only in data, not in what is allowed next
  • A workflow engine or the database already owns the transitions

Conceptos clave

State
One status as its own type, holding only what is allowed from there. A placed order can be shipped or cancelled; a shipped order can only be delivered.
Transition
A move from one state to the next, written as a function that returns the new state. Its parameter type says which states it accepts.
Context
The object or record that holds the current state, such as an Order row. It swaps its state in one place, after a transition returns.
In modern TypeScript
A discriminated union and plain functions often replace the GoF class-per-state form. A switch on state.kind, checked for exhaustiveness, gives the same safety with plain data.

Reglas de diseño

Make illegal transitions absent, not guarded
Accept only the states a transition is legal from: cancel() takes a Placed order, not any Order. A wrong call then fails at compile time, not at runtime in the one path nobody checked.
Return the next state; do not mutate into it
A transition that returns a new state value keeps every move explicit and typed. The context stores it in one place, so the whole machine is readable from the return types.
Keep the whole machine in one file
Write the states and transitions next to each other. A reader should answer "can a shipped order be cancelled?" from one file, not by searching the codebase.

Errores habituales

  • Guarding transitions with ifs inside each state, so illegal moves still compile and fail at runtime.
  • Transitions spread across many files, so the machine is as hidden as before.
  • A switch with a default branch, so a newly added state is silently ignored instead of failing to compile.
  • Two requests changing the status at once. The symptom is an order both shipped and cancelled; guard the row with optimistic locking.