4 ms·
It is interesting how few languages make state machines easily read when the appear in code ("what's with all the ifs?").
by protomyth 15y ago
It is interesting how few languages make state machines easily read when the appear in code ("what's with all the ifs?").
- 6ren 15y agoYou could use polymorphism - but that scatters the transition logic to the four winds. I guess pattern matching would work well (haven't used it that way myself). An actual state transition table seems the most straightforward way to represent it - but I agree, it's striking that there isn't native support for it. Perhaps there isn't a better way to do it...? EDIT just thinking, a state machine can be modeled as a regular expression (they are formally equivalent). You could represent them as productions; or, as regular expressions. Composed of the input symbols causing the transitions - the states are implicit; but you could associate a function with each symbol, by inserting it afterwards; the 'symbol' itself could be a function that return true/false, for matches: isStart() setup() ( !isEnd() processInput() )* isEnd() teardown() | err() I would guess that Lua deals with these problems a lot (as an embedded game scripting engine, both enemy AI and UI logic might be modeled well by state machines (in part) - perhaps it has a good way of handling them, or at least, good idioms have developed.
- pygy_ 15y agoThis would actually be a nice way to hijack lpeg.
- shubber 15y agoHonestly, modeling state with polymorphism can be really elegant and powerful. There are a few decisions to make (for instance, do States explicitly make transitions happen, or do they return the next state?) But in general, anywhere you see a lot of conditionals, you probably want to consider a polymorphic approach. Especially if you have more than one function with parallel logic trees.
- 6ren 15y agoWhat do you think of my concern of it "scattering the transition logic" across the classes? Each class becomes a production, and only it knows which classes can come next. It's hard to get an overall grasp of how the state machine works, because you have to inspect all the classes to know the transitions. This is a significant concern for me when reading others' code: the individual parts might be easy to understand, but there is usually no documentation of the overall design, of how the parts operate together. And there is no code that ties together all the parts - so you are forced to inspect all the local details in order to get a global overall view. There are many ways to evaluate a program - how efficient it is, how many features it has, how complete it is, whether it is consistent, whether it is correct, how easy it is to prove things about... but I think how much work it is to understand is one of the most important - and usually neglected. For a complex program, having some way to quickly grasp the overall design is key to this, IMHO.
- pygy_ 15y agoIn languages with first class functions and hash tables, you can use this instead (here in Lua): state1.goto = { state2 = function() ... end, state3 = function() ... end } state1.goto.state2() Much cleaner (even though the Lua syntax for function literals is a bit heavy. It would look much nicer in CoffeeScript...).
- justincormack 15y agoYeah, state as functions is really nice, but it gets messy in languages that don't have tail recursion elimination (eg Javascript), as you have to have a trampoline mechanism to call the next state, I generally return the new state function and get the trampoline to call it. Which is less readable then just calling the new state as in your Lua example...
- nitrogen 15y agoIn C it is possible to use functions to represent state. To cause a state change, you return a pointer to the function representing the next state. Each state function manages transitions away from that state. The main event loop repeatedly calls the current state function with any external input relevant to the system. There is also the giant switch block option, with the current state stored as an enum. With a little discipline, I think it should be possible to represent state machines in a readable fashion in most popular programming languages.