Live data from Hacker News

Why Developers Never Use State Machines

skorks.com

21–30 of 96 posts

Re: Why Developers Never Use State Machines

#21
most state machines you're likely to need in your day-to-day development have nothing in common with their computing theory counterparts

I don't follow this? The core of whichever state machines I write seem to be DFAs, with the alphabet being events causing transitions.

Re: Why Developers Never Use State Machines

#23
Having used state machines a lot for embedded development, I find that they have one huge drawback: the resulting C code is unreadable, which turns maintenance into a nightmare.

SM have a key quality: they are a compact and unambiguous way for specifying a behavior. If your system's behavior is set in stone, it's worth specifying it as a SM and implementing it as one. This SM is also great to include in a spec, standard, RFC etc. But if the system evolves, small changes can require dramatic reshaping of the machine.

Also, they can lead to very efficient implementations, especially if you don't have the benefits of a serious OS with a fancy scheduler underneath.

Re: Why Developers Never Use State Machines

#24
post #21

most state machines you're likely to need in your day-to-day development have nothing in common with their computing theory counterparts I don't follow this? The core of whichever state machines I write seem to be DFAs, with the alphabet being events causing transitions.

I think the point is that you don't need to know all the dry theory and the names for everything.

Re: Why Developers Never Use State Machines

#25
We use FSM to deal with the hiring/applicant flow at http://matchfwd.com

FSM are very common in video games. I've also worked with one in a (very very) big engineering/CAD software when FSM was managing the whole approval/review/audit flow .

It seems there just less common with web dev in general.

Re: Why Developers Never Use State Machines

#26

If you are a C developer, try Ragel [ http://www.complang.org/ragel ]. I use it lot, also can be easily pared with the Lemon parser generator.

For more re: Ragel, look at

http://zedshaw.com/essays/ragel_state_charts.html

What I really like there is the idea of a DSL that generates your state machine code for a protocol.

Re: Why Developers Never Use State Machines

#28
post #3

This man has never heard of video game A.I. programming :)

I had the same reaction. Almost nothing but FSMs in game AI. Makes me wonder if we need to look beyond FSMs for the next level of intelligence.

The natural next step is a PDA. Perhaps you could use this type of system to model an AI that can get distracted or become faced with an intermediate task and then return later to the original objective?

Re: Why Developers Never Use State Machines

#29
Not sure if I understand the author correctly, but it seems to me, that he advises to write the state machine behaviour down as code (in form of a class? api? little frameowork?) when you start a project, so that you will be able to use a cleanly written interface for your state machine when the project gets bigger. Is my understanding correct?

From my experience a state machine is more of a pattern then a real object. Implementing it on the fly is what worked best so far, for me. Of course there is a point where it gets nasty because of complexity. But first that is always the case (complexity IS nasty) and second overcommiting to structure and architecture increases the complexity already in the beginning and might hurt more then it helps. At least fully coding all possible state machine behaviour as the first default task ifor every new project doesn't seem to be a smart thing to do.

Re: Why Developers Never Use State Machines

#30

Simple, because most developers are not computer scientists i.e. no formal training in finite automata, computability, complexity, nondeterminism, regular expressions, non regular languages, context free grammars and languages, Turing machines, halting problem and underlying math in general. If they did, trust me they would use state machines without too much reservation where appropriate. But as it is, the concept i…

Indeed. It gets so tiresome looking at yet another 500 line subroutine with else-if after else-if snaking down page after page.

That said, I managed to get through my BSCS without taking the "Finite State Automata" class. I had to pick up table driven methods later, first just fetching values out of a "table", then later generalizing to code-blocks/functions.

At least one way of conceptualizing and implementing a state machine can be simply summed up for the newb:

* Where am I?

* What just happened?

* What do I do next because of it?

Generate an enum for each of the first 2, and put code references into some kind of "sparsely populated" 2-D array, and you're off to the races. TMTOWTDI, but this approach goes a long way towards demystifying the topic.

Post reply on HN