I&#x27;ve coded some systems which use tons of force to move pieces of metal through the same space that moments earlier were occupied by humans or human limbs.<p>This was Scary.<p>I turned to state machines.<p>In ten years, neither of these two systems I coded has had a single software bug - not even in testing.<p>Imagine you have a emergency stop button (or two) on a machine. What needs to happen when that button is pressed? The machine needs to stop of course - but &quot;stop&quot; means radically different things depending on what the machine is doing at the time. If, say, a part is clamped in the machine, and a couple tons of cutting force is being applied to that part, an emergency stop had better keep that part held in place, or chunks metal will be exploding everywhere. On the other hand, if the clamping process has just started, the machine needs to stop applying pressure on the clamp.<p>When you figure that even a simple machine might have 30 outputs that need to be controlled, and 30 inputs coming in, trying to solve this kind of problem with if statements guarantees that you will miss something. And you will be unable to reason about it later.<p>With state machines however, you can make provably correct software. For every state in the system, I can see exactly what the machine should be doing in that state, and every possible transition out of that state. Alternately, I can take a given input or output, and see every possible effect it will have on the system.<p>For example, I can review every state to see what happens when the emergency stop is pressed, and make sure that it does the right thing.<p>Or I can work through the flow of states. In the &quot;Clamping&quot; state an emergency stop will cause a transition to the &quot;Estop Clamp Open State&quot; which release pressure to the clamp and turns off lots of spinning bits on the machine. Afterwords, when emergency stop is released, the machine transitions to the &quot;Reset, Clamp Open&quot; state which starts the process of moving things around to get back to normal. Once every sensor reports that everything has been reset, the machine transitions back to &quot;Ready&quot;, and we are ready for the next transition.<p>For web applications, I use state machines for business process logic. Usually this can be a lot less formal - just a state field in table, with methods that check that field to decide how to act, what is allowed, or to transition states.