State diagrams are one of those things that seem intimidating at first, but once you start using them, you’ll wonder how you ever did without. But before we get into the diagramming part, first we need to understand what a state machine is.
A state machine is a device that stores the status of an object at any given time. It can simulate sequential logic, as well as model problems in various fields, including AI, maths, linguistics, and game development. A state machine can also change the object’s status or cause other actions based on its input.
If you want to understand all the possible states and show how an object gets to and from each state, you’ll want to visualize it. Hence, the state diagram.
What is a state diagram?
A state diagram is a graphic representation of a state machine. It shows a behavioral model consisting of states, transitions, and actions, as well as the events that affect these. It’s also one of the 14 Unified Modeling Languages (UML) used for specifying, visualizing, constructing, and documenting software systems.
What’s the difference between a state diagram and a flowchart? Well, despite having a clearly defined start and end point, state diagrams aren’t useful for showing a progression of events because they depict transitions. So they’re better for showing shifts in behavior.
A flowchart shows the processes that change the state of an object. A state diagram shows the actual changes in state, not the processes or commands that created those change.
How can a state diagram help you?
It’s useful for showing the progress of event-driven objects in a reactive system, such as an ATM. First, the machine is either on or out of order. When the card’s inserted, the user enters their pin. If it’s correct, they’re taken to another set of options. If incorrect, the process is ended.
State diagrams are also useful for describing how an object moves through various states within its lifetime. For example, the movement of an academic through their career: they begin as a freshman, then move to sophomore, junior, and then senior.
How to draw a state diagram
Each diagram usually begins with a dark circle that represents the initial state and ends with a bordered circle that represents the final state. States are denoted with rectangles with rounded corners. Each rectangle is labeled with the name of the state. And transitions are marked with arrows that link one state to another, showing how the states change. You’ll find a run through of the most popular symbols below.
Composite state (also known as a ‘nested state’)
This refers to a state that encompasses various sub-states, which are nested into it. Sub-states are used to simplify complex flat state diagrams by showing that some states are only possible within a particular context. In the air toaster oven example below, the ‘heating’ status of the machine represents the composite/nested state.
This is represented by a diamond shape on the diagram. It indicates a dynamic condition that has several potential results.
This is a message that moves an object from state to state. In the keyboard example below, pressing CapsLock is the triggering event. If the keyboard is in the “default” state, pressing CapsLock will cause the keyboard to enter the “caps_locked” state. However, if the keyboard is in the “caps_locked” state, pressing CapsLock will cause the keyboard to enter the “default” state.
Event or Transition
This is when a moment triggers a shift. These are represented by labeled transition arrows. In the example above, pressing CapsLock is the event that triggers the ‘default’ and ‘caps_locked’ states.
This represents the first state in the process. It appears as a dark circle with an arrow leading from it.
Final State (or terminator state)
This represents the object’s final state. It appears as an arrow pointing to a filled circle nested inside another circle.
A circle with an X through it represents the point at which an object escapes the state machine. The exit point is used when the process is interrupted or left incomplete due to an error or issue.
This is a boolean condition that must be true for a state transition to occur. If the condition is false, the transition does not occur. It’s written above the transition arrow.
States represent the current status of an object and appear as a rounded rectangle. In the example below, ‘Closed’, ‘Open’, and ‘Deleted’ are all states.
State transition diagrams
This refers to when the object transitions from one state to another, the events that must have taken place before the transition can occur, and the activities during the life of the object.
This refers to a state contained within a composite state’s region. In the toaster oven example above, ‘toasting’ and ‘baking’ are sub-states in the larger ‘heating’ composite state.
State diagram examples
State diagrams are an essential tool for visualizing system behavior. Setting this information out in a diagram means you can see an object’s behavior throughout its entire lifespan, as well as the different events that cause various transitions, at a glance.
Pick out your dedicated diagramming software — ideally one that has a robust library of UML templates and shapes — and begin making your first state diagram today. If you’re working with a team, make sure you invest in software that lets you work collaboratively with your team in real-time, so you can edit diagrams together, easily share with others, and get feedback right on your canvas.