I have recently created some dead simple code to explain the concept to colleagues whom, as the article notes, were not using this very simple and effective construct at all to control state transitions in business apps.<p>As the article does not really explain how state machines work let me try to do it here so maybe you will go back to your code and put one in place :) In general, if anywhere in your code you have some property/field called status/state/etc... that you update depending on events then chances are that a state machine would improve the robustness of your code.<p>Ok, so you have some states. For business apps this usually comes from business. For our dummy example we will simulate a day (using Haskell here for conciseness, but no worry, it's trivial and you don't need to know any Haskell to understand it):<p><pre><code> data State = Awake | Dressed | Fed | Ready | Work | Cafe | Home | Asleep
</code></pre>
Now most apps stop here and thus they don't have a machinery to control how to move between these. If we were to draw a graph where vertices are the above states and edges are how we go from one to another then, without any restriction, we would implicitly get a complete graph where you can go from any state to any state. Eg. from "Ready" I could go straight to "Asleep" without "Work" :) Our aim is thus to restrict transitions from one state to another to transitions that actually make sense. Making sense obviously depends on the problem we are solving. For our example let's introduce the following events:<p><pre><code> data Event = Alarm | Dress | Eat | GoToWork | Think | Yawn | Coffee | GoHome | Read | WatchTV
</code></pre>
Now that we have states and corresponding events we can draw a graph where vertices are states and edges are events. The graph is now explicit in that we can only move between states (vertices) via events (edges). To put this graph into code, let's create a function that takes a pair of state and event, eg. (Asleep, Alarm) and returns a new state, eg. Awake:<p><pre><code> myDay :: (State, Event) -> State
</code></pre>
As said above, myDay is a function that takes a (state, event) pair, which we can also call a transition, and returns a state. This function is nothing more than a lookup table, aka. transition table, where we will write down how to transition between states:<p><pre><code> myDay (Asleep, Alarm) = Awake
myDay (Awake, Dress) = Dressed
myDay (Awake, Eat) = Fed
myDay (Dressed, Eat) = Ready
myDay (Fed, Dress) = Ready
myDay (Ready, GoToWork) = Work
myDay (Work, Think) = Work
myDay (Work, Yawn) = Cafe
myDay (Cafe, Coffee) = Work
myDay (Work, GoHome) = Home
myDay (Home, Read) = Asleep
myDay (Home, WatchTV) = Asleep
myDay (_, _) = error "invalid state transition"
</code></pre>
That's it, we have codified our graph. We explicitly stated the valid transitions and anything else is by definition invalid. We are pretty much done at this point. A state machine itself can be thought of as a generic function that takes a transition table, a transition and returns a state. This is only to decouple a specific transition table from the general machine itself, but it literally does nothing other than lookup whether the transition is in the table. If yes then it gives back the corresponding new state otherwise we get and error.<p>You can see a pretty picture of the above graph and some code here: <a href="https://github.com/pwm/fsm" rel="nofollow">https://github.com/pwm/fsm</a>