2 ms·
State machines are really the ideal application of goto, where the semantics map directly - go to this state, making the flow of the code very clear. The perfor
by userbinator 11y ago
State machines are really the ideal application of goto, where the semantics map directly - go to this state, making the flow of the code very clear. The performance benefits are nice too, so in that sense I think it's beneficial for both the machine and the programmer.
It'd be funny to see the results of an implementation with "proper OOP design using the State design pattern", I have a feeling that could be worse than the original switch/loop.
- radicalbyte 11y agoIt depends, I prefer switches with a single line, which call out to some service to be preferable to the State pattern. This is because the State pattern encourages you to design these "everything and the kitchen sink" style classes. It's a bit crazy to implement the State pattern than have the various state object just do single calls (or no-ops) when the switching has been implemented to be readable. Note: if your language doesn't support enumerations and switching over them then the OO pattern may be better. If I didn't had static analysis telling me when I've missed a state then it's a bug waiting to happen. The State pattern makes a missing state hard to miss ;)
- chubot 11y agoI've been using re2c lately and it's really great. It's this gem from the 90's that not a lot of people know about; I encountered it in the Ninja build system, where they were concerned about parsing megabytes of text very quickly. It basically generates a bunch of gotos from readable regexes. http://re2c.org/ http://re2c.org/ One thing I haven't explored is "push vs pull" scanners. It's basically the same thing as threads vs. async, so it could be really useful for async servers. Normal scanners will pull data in when needed, but if you pass -f, it can apparently have data pushed upon it. I guess Ragel can be used easily like this since Mongrel is async (?)
- Puts 11y agoThe thing that really makes GOTOs feel right for state machines is that you are not wasting any CPU cycles for things that can be made pre-compilation. A transition table solution for example would be a great solution if it was built in to the language and the code controlling the flow of the program was generated during compilation and not in real time every time the program is executing. The only reason you would want to generate the states in real time during execution is if you would need a totally unpredictable state machine, and that's not what state machines are good for. :D
- deleted 11y ago[deleted]