What a state diagram is
Many things in software are always in exactly one situation at a time. An order is waiting, paid, shipped or cancelled. A traffic light is red, green or amber. Each situation is a state. An event that moves it to another state is a transition.
A state diagram (also called a state machine diagram or a state chart) draws those states as boxes and the transitions as arrows labelled with the event that causes them. It makes rules such as "you cannot ship an order that is not paid" impossible to miss.
The notation
| Element | Drawn as | Means |
|---|---|---|
| State | A rounded box | A situation the thing can be in |
| Start | A small filled dot | Where it begins |
| End | A dot inside a ring | Where it finishes |
| Transition | An arrow with a label | The event that moves it |
| Choice | A small diamond | The next state depends on a condition |
| Note | A sticky note | Extra rules or details |
Example 1: an online order
Notice what the diagram makes clear: there is no arrow from Shipped to Cancelled, so once a parcel leaves you cannot cancel. If the business wants that, someone has to add a path.
Example 2: a document review
A real review also has a path back: InReview --> Draft : changes requested. Loops like that are normal in state diagrams; a traffic light, for example, is nothing but a loop from red to green to amber and back to red.
Example 3: logging in
The full version also sends a wrong password back to LoggedOut, unlocks the account after 15 minutes and lets a user log out. Add those arrows one at a time and check each one is a rule you really want.
Writing a state diagram as text
The examples above use Mermaid's state diagram text, which is short enough to keep in a code comment or a README:
stateDiagram-v2
[*] --> Waiting
Waiting --> Paid : payment received
Paid --> Shipped : parcel sent
Shipped --> [*]
state "Waiting for payment" as Waiting
note right of Paid : Stock is reserved here
[*]is the start when it is on the left of the arrow and the end when it is on the right.A --> B : eventis a transition with its label.state "Long name" as Agives a short id a readable name.state Check <<choice>>draws a diamond for a decision.
How to draw a good state diagram
- List the states as nouns or adjectives: Waiting, Paid, Locked. If a name is a verb, it is probably a transition.
- Start with the happy path from start to end.
- Add the other events one state at a time: what can happen while it is here?
- Look for dead ends: states with no way out that should have one.
- Look for missing rules: arrows that should not exist, or ones the business forgot.
State diagram vs flowchart
A flowchart shows the steps of a process in order. A state diagram shows the situations one thing can be in and what changes them. If you are describing what a person or program does, use a flowchart. If you are describing what an order, a device or a screen *is* at any moment, use a state diagram.
Drawing state diagrams for free
In Chalkline, open Diagram as text → State, type the transitions and press Apply. Start and end dots, choice diamonds and notes are drawn for you. Switch between hand drawn and clean, export to SVG, PDF or PowerPoint, and keep the text next to your code. For the other diagrams developers use most, see sequence diagrams and UML class diagrams.