mediumDesignCodingMidSeniorStaff
An order moves through a set of states with rules about which transitions are allowed. How would you design that so it does not become a nest of if statements?
5 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup
Answer
Short answer
Write the legal transitions down as a table first, then give the entity one entry point that consults it and no state setter at all. The design question is where the transition rules live and how side effects stay outside the decision that changed the state.
Was this answer helpful?
Likely follow-ups
- Two events for the same order arrive at the same moment. What stops the second applying to a state that has already moved?
- Where do the side effects live, and what happens when one fails after the new state was saved?
- How would you add a new state without editing every existing state class?
- The business says any state may go to Cancelled except Shipped. Where exactly does that rule live?
Related questions
- You may use an AI coding assistant in the machine-coding round. What do you still write yourself, and what do you ask it for?mediumAlso on invariants5 min
- A bundle can contain products or other bundles, and the pricing code should not care which it is holding. How would you model that?mediumAlso on invariants6 min
- Numbers arrive one at a time and after each one I want the running median. How would you keep it?mediumAlso on invariants5 min
- Build a shared-expense tracker. Three people split a bill of ten pounds evenly - write the classes, and tell me where the missing penny goes.mediumAlso on invariants6 min