3 ms·
I'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. Thi
by danielvf 11y ago
I'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.
This was Scary.
I turned to state machines.
In ten years, neither of these two systems I coded has had a single software bug - not even in testing.
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 "stop" 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.
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.
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.
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.
Or I can work through the flow of states. In the "Clamping" state an emergency stop will cause a transition to the "Estop Clamp Open State" 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 "Reset, Clamp Open" 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 "Ready", and we are ready for the next transition.
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.