11 ms·
Isn't FSM more like writing a program consisting only of GOTO's? I call this the "loopz-effect", where any single state may have effect in any other state of th
by loopz 6y ago
Isn't FSM more like writing a program consisting only of GOTO's? I call this the "loopz-effect", where any single state may have effect in any other state of the program.
- ajuc 6y agoIt's the opposite. In a FSM you have clearly defined states and all possible transitions between them. If you write the same code as normal imperative code that modifies some variables - to understand what it does you have to "run it" in your brain and reverse engineer the states and transitions yourself.
- davemp 6y agoIt's more like having a switch statement inside of a loop. The variable you switch on is the next state and the case you're in is the current state.
- retrac 6y agoStrictly speaking, yes. But you should think of it as a formalized abstraction over that; a way to reason about something you can easily translate into a bunch of goto statements. It's just like how all if/then and function call constructs compile down to goto, as well. Some languages will offer more abstraction over this, but you can still do such constructs in assembly language if you want to by hand-holding the machine through every step. If you do find yourself writing assembly language (or C, for that matter!) then you should do just that; implement high-level constructs at the low level with a bunch of jump statements. The resulting code is verbose and superficially seems gnarly, but will also be far more scalable and robust. But there are compilers for a reason! There are some examples of automatic FSM to C compilers elsewhere in this thread. Some very high-level languages have embedded domain languages for FSM generation as well.
- garethrowlands 6y agoYes. And not only that, but GOTOs are the normal recommended way of implementing an FSM.
- xondono 6y agoWhat? 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.
- amw-zero 6y agoA FSM is simply a structure that progresses over time, where the next state is dependent on the previous state. By your usage of “goto,” I feel like you’re implying like this is a negative thing. But state is an inherent part of computation. A Turing Machine can be seen as a slightly more powerful state machine, and most if not all of the code that we write can be easily modeled with the less powerful state machine. And no, functional programming does not actually help this.