This is not going to be a tutorial on how to write a state machine in code. There are lots of great libraries that handle the heavy lifting for you. We’re going to talk about how to identify the state machine that’s already present in your business (even if you don’t realize it), and how to model it in a way that you could plug it into code.
How this process will benefit your business
Properly identify and prove where there are inefficiencies in your business so you can take action. Fixing these issues will save you time, money, and even improve the customer experience.
Discover where there are gaps in your business process so you can start to develop solutions to close those gaps.
Clearly define process flows to make training new employees easier.
What is a state machine?
“a device that can be in one of a set number of stable conditions depending on its previous condition and on the present values of its inputs.”
What does that mean in real life? A state machine can be used to model any entity in your business that changes states over the course of its life. Here’s a simple example: There’s a telephone sitting on the desk. Its state is “hung up”. The telephone company can transition that phone to “ringing”. The person in the room can “pick up the phone” in either of these cases, but in the first case, it transitions to “dial tone”, and in the second, it transitions to “answered”.
State machine diagrams and examples
Here’s a visual example that we’re more likely to see implemented in code:
What value does a state machine bring? First of all, it allows you to get a proper understanding of the business process being defined. The examples given are both fairly simple. Here’s a diagram of the state machine from a recent project.
This may seem overly confusing, but the reason for that is it’s not a straight progression from one state to the next. There’s a variety of events that can happen at multiple points in the process. If we re-look at the eCommerce diagram, you might notice some things that are missing.
How are returns handled?
What happens if somebody calls to cancel after it has been marked shipped but before it’s picked up? In the above case, a “Decline” can happen at any point in the process, which is why there are so many lines.
The benefits of using state machines for your business.
Why do you want this complicated state machine diagram above? Your business might be working fine the way they are. The goals of a state machine are to define process flow, and enforce constraints. There are certain actions which can’t be taken at certain points without disrupting flows. Having clearly defined states and process flows allows objective decisions to be made, taking the thought process out of the everyday operation of your business.
Having the state machine diagramed out also allows you to improve your process one step at a time. You can take one state or one transition and optimize your processes around it without disrupting the rest of your system.
How do I model this in real life?
You simply start to map it out. This may seem hard at first, but the goal isn’t a perfect first draft. Once you’ve gotten started, you’ll start to identify what you’re missing as you progress through the process.
Lay out the states that you know your process goes through. These should be nouns, adjectives, or short phrases.
Some of these are self-explanatory:
Placed: An order has been placed but not touched by anybody at the company
Ready to Pick
Picking: An order is being picked and packed
Pending Shipment: An order is sitting on the loading dock waiting to be shipped
In Transit: An order is in transit
Delivered: An order has been delivered
Pending Return: An order is pending return
Received: An order has been returned but not inspected
Returned: An order has been returned and inspected, pending credit
Credited: An order has been credited
Some may be less obvious:
Backordered: An item on an order is backordered
Pending Cancellation: A user calls to cancel the order while it’s in one of the other states
Pending Payment: A user’s payment gets declined after the order is accepted
Fraud Alert. A transaction gets a fraud alert
Map how it gets from state to state. These will typically be verbs.
Some state transitions may be fully automated:
Print Slip: a packing slip automatically prints in the warehouse when an order comes in and the state transitions from Placed to Ready to Pick
Deliver: delivery confirmation was received from the shipper’s API
Some state transitions happen when a person does something:
Barcode Scan: scanning a barcode on a packing slip
Think about exceptions to the process, and how they are handled. You may not have any of these, or you may not realize them until they come up.
What happens when a customer calls customer service at each state in the process?
What happens if something goes wrong with the shipment?
Think about the rules for transitioning into each state
Some are common sense as they apply to the process flow
You can’t mark an order as shipped unless it has a tracking number (or a shipping method which doesn’t provide tracking)
You can’t return an order before it has been shipped
Some might require more thought as they’re custom business rules
You can’t return an order that was placed more than 60 days ago unless you’re a manager
What can I do with a state machine diagram?
Your next steps will depend somewhat on your business. If you’ve modeled out a physical process, I love the ball of string validation.
Ball of string (process walk/Gemba walk)
Get a ball of string or yarn
Get your team together wherever the process starts
Tie down the string
Start to walk through the process.
Tie down the string at each step along the way.
Once you’ve completed the process, walk through each deviation/exception
Tie a new string on where the deviation happens, and walk through the process until the end (or until you rejoin the main process flow)
Takeaways with ball of string:
Anywhere the string crosses (or backtracks) is a place where your process might be inefficient
Over the course of this process, your team has probably noticed things which are missing
State machines and software
State machines are well defined constructs in most programming languages. Beyond defining your states and transitions, you can add code which triggers each time the state transitions to handle any side effects that happen with that transaction (notification emails, etc). Every company and industry has the ability to improve a process with software so you can scale more effectively.
If you’ve got a complex business process like this, Headway can help. We can work with you to define your processes and see how a if a digital product can be built to help your company save time, reduce your costs, and keep growing.