Finite State machine - when to use and when not to?

You’ll get significantly better responses, IMO, if you ask about your specific use-case versus sifting through mountains of speculation. That said, here’s a small pile of speculation…

Must not use:

  • things where the space of states is infinite. You wouldn’t use an FSM for “current account balance”

Pros of using:

  • FSMs provide a shared vocabulary for talking about complicated code. Snazzier libraries will even let you run the documentation!

Cons of using:

  • using an FSM with the wrong “kind” of implementation can be painful. For instance, using something compile-time like :gen_statem or Finitomata (linked above) but with states/transitions defined at runtime
  • an FSM is good at enforcing business rules in software like “do X then Y”, but that can cause friction at a company where the real-world process is “do X then Y, unless it’s Tuesday then do Z first instead and then Y and then X”

“Must use”:

  • It depends on what you define as “an FSM”: is a “draft” boolean that starts out false and is deliberately set to true later an FSM? It has two states and a transition between them. :thinking:
4 Likes