I’ve written before (see here and here for example) of my enthusiasm for using finite state machines (FSMs) in my design and coding. I had a C-based template that I used over and over. To implement a new state machine I just filled in a table that mapped the current state and input to a transition routine and, of course, wrote the transition routines. It was fast and easy. More importantly, I found that the software was always more reliable than if I used some other method. That’s because once you write your state transition diagram, almost everything else is mechanical
Alfonso de la Rocha has an excellent post on designing and coding with FSMs. He echoes my opinion that developing with FSMs results in simpler more reliable programs. He also shows how drawing the state diagram helps to understand the problem better, which also improves the implementation.
In his discussion de la Rocha mentions some advanced tools that can help with the design of FSMs. First is mermaid, a tool that lets you specify your state diagram in a markup language and produces a nice state diagram from it. Often, it’s possible to take that idea a bit further and produce both the diagram and the code from the same specification. I wrote about an example of this in Eric Raymond’s UPSide project.
Next he gives some examples of frameworks (in Go) to help produce an FSM implementation. They’re similar to my template that I discussed above. Finally, he mentions TLA+, a system to formally verify that your FSM is correct.
It’s a good post with lots of useful information. It’s definitely worth your time to give it a read.