4 ms·
What? Where? Most sane people write FSM as switches inside an infinite loop
by xondono 6y ago
What? Where? Most sane people write FSM as switches inside an infinite loop
- recursive 6y agoRight here, by me. Or perhaps I'm insane. Using goto ensures that all of a particular state's logic is nearby in code. It also eliminates the need to check an exhaustive list of irrelevant switch cases at runtime.
- megameter 6y agoRelevant example case is in a branching interpreter dispatch loop vs compiling threaded code, and all the hybrid concepts in between: https://en.m.wikipedia.org/wiki/Threaded_code https://en.m.wikipedia.org/wiki/Threaded_code It's a very old CS construct where right answers depend on the environment. In many modern structured languages you don't have a goto at hand so you're ushered towards switch statements, but then you can opt to do weird things like throw an exception and use the handler to jump.
- loopz 6y agoMakes perfect sense. Languages with first class function values may shine here, ie. Go.
- Mikhail_Edoshin 6y agoUsing a direct goto should be faster because of fewer conditionals.