{"id":9503,"date":"2021-02-22T16:39:10","date_gmt":"2021-02-22T21:39:10","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=9503"},"modified":"2021-02-22T16:39:10","modified_gmt":"2021-02-22T21:39:10","slug":"designing-with-finite-state-machines","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=9503","title":{"rendered":"Designing with Finite State Machines"},"content":{"rendered":"<p>I\u2019ve written before (see <a href=\"https:\/\/irreal.org\/blog\/?p=3567\">here<\/a> and <a href=\"https:\/\/irreal.org\/blog\/?p=9119\">here<\/a> 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\u2019s because once you write your state transition diagram, almost everything else is mechanical<\/p>\n<p>Alfonso de la Rocha has an excellent post on <a href=\"https:\/\/adlrocha.substack.com\/p\/adlrocha-finite-state-machines\">designing and coding with FSMs<\/a>. 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.<\/p>\n<p>In his discussion de la Rocha mentions some advanced tools that can help with the design of FSMs. First is <a href=\"https:\/\/mermaid-js.github.io\/mermaid\/#\/README\">mermaid<\/a>, a tool that lets you specify your state diagram in a markup language and produces a nice state diagram from it. Often, it\u2019s possible to take that idea a bit further and produce both the diagram and the code from the same specification. I wrote about an <a href=\"https:\/\/irreal.org\/blog\/?p=7149\">example of this<\/a> in Eric Raymond\u2019s <a href=\"http:\/\/esr.ibiblio.org\/?tag=upside\">UPSide project<\/a>.<\/p>\n<p>Next he gives some examples of frameworks (in Go) to help produce an FSM implementation. They\u2019re similar to my template that I discussed above. Finally, he mentions <a href=\"https:\/\/www.learntla.com\/introduction\/\">TLA+<\/a>, a system to formally verify that your FSM is correct.<\/p>\n<p>It\u2019s a good post with lots of useful information. It\u2019s definitely worth your time to give it a read.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve 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 &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=9503\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[37],"class_list":["post-9503","post","type-post","status-publish","format-standard","hentry","category-programming","tag-tech"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/9503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9503"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/9503\/revisions"}],"predecessor-version":[{"id":9504,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/9503\/revisions\/9504"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}