Re:Ask HN: Can a state machine be stateless?
by Someone on Sunday June 12, 2022 @05:39PM
(#31718536)
You probably mean to return a new current state. If so, as others already said, the function isn’t a state machine, but the (current state, function) pair is.
Advantage of that approach is that it makes it easier to test the state machine (if you want a test that checks what it does on receiving input I while in state S you don’t have to somehow get it into state S, but just call it with those inputs) and that it means you can (easily) write the function so that it is reentrant, and thus allows you to instantiate the state machine multiple times (if only for running tests in parallel)
For some state machines, it may not even be possible to get them in state S enough times to test them with all inputs you want to test them with, if you ‘hide’ the current state inside the transition function.
Advantage of that approach is that it makes it easier to test the state machine (if you want a test that checks what it does on receiving input I while in state S you don’t have to somehow get it into state S, but just call it with those inputs) and that it means you can (easily) write the function so that it is reentrant, and thus allows you to instantiate the state machine multiple times (if only for running tests in parallel)
For some state machines, it may not even be possible to get them in state S enough times to test them with all inputs you want to test them with, if you ‘hide’ the current state inside the transition function.