Having each state as a function, with a descriptive name is nice, but you really have to dig through the whole code to see which transitions are possible and which are not.
Having each state as a function, with a descriptive name is nice, but you really have to dig through the whole code to see which transitions are possible and which are not.
[1] https://en.wikipedia.org/wiki/Finite-state_machine#Mathemati...
I've seen enough state machines in the wild that are just a dispatcher with a bunch of functions mutating a single state variable. Whether we call these contraptions state machines or not is just quibbling.
As others have pointed out, there are better ways to implement them; yet, I rarely come across those.
I also tends to have state machine calling other machine or being composed of 2-3 smaller ones.
I save the state in some serialized form in DB if needed.
It’s great for customer flow : like; tracking where they are in the KYC process or onboarding and deciding what to do next.
Without having 50 nested switch statement.
There are techniques to keep those large state machines manageable, such as structuring them with sub-state machines.