State Modeling

What changes over time — order status, cart contents, inventory, payment status — and the state machines that fall out of asking.

What Information Changes Over Time?

Entities say what exists; state says what about them changes. Order status, cart contents, stock levels and payment status all change, each on a different trigger, and each one is a place where the system must know "the current value" — which is where most bugs live.

Q · You have the entities. Which of their attributes change over time, who changes them, and why does the answer matter more than the schema?
Overwrite or Append?

For every piece of state there is a choice: keep the current value, keep the history, or both. Stock is a number that is overwritten and a history that is appended; the choice depends on who will ask "why?", and getting it wrong is cheap to fix early and expensive later.

Q · For a piece of state that changes, should the system keep only the current value, the sequence of changes, or both — and what question decides it?
Finding the State Machine

When a piece of state has a small set of named values, the values are half the model; the other half is which transitions are allowed and who causes them. Order CREATED → PAID → SHIPPED → DELIVERED, with CANCELLED and REFUNDED as exits, is discovered by asking "from here, what can happen?" until nothing new appears.

Q · A status has a handful of values. How do you find the transitions between them, the ones that must not exist, and who is allowed to cause each — without a workflow engine?
The Order Lifecycle, Built
▶ lab

PENDING → PAID → FULFILLED, with CANCELLED and REFUNDED added later. Which transitions are legal, who triggers each, and in what order to build them so that the store works before the machine is complete — the state module applied end to end on the capstone's order.

Q · You know how to discover a state machine. In what order do you build the order's lifecycle so that every step leaves a working store, and which transitions can wait?
States That Must Be Unrepresentable

Paid twice, shipped before paid, refunded with no payment: some combinations of state must not merely be rejected at runtime but be impossible to write down. Finding them is a discovery move — ask which combinations of fields would be a lie — and where to enforce each is a decision, not a doctrine.

Q · Which combinations of state in the store must never exist, how do you find them, and where — type, constraint, transition — should each one be made impossible?