Live data from Hacker News

Why developers never use state machines (2011)

skorks.com

1–10 of 121 posts

Re: Why developers never use state machines (2011)

#2
I've used state machines in the context of hardware design, particularly fpgas and asics. In hardware land, you use a state machine to implement something like software, where your circuit is able to remember what happened earlier so it can behave differently accordingly. It's very crude relative to their software counterparts.

They are not as common in software because software is one giant state machine already. Variables hold state, there is state within state (classes, composition etc). Sometimes entities in game design have explicit state machines for their behavior but that's to minimize complexity rather than due to absolute necessity

Re: Why developers never use state machines (2011)

#3
Many moons ago I built a UI using a fairly basic UI toolkit (X/Motif) so I had to handle all the state of the UI menus and dialogs along with the status of the current file (new/saved/unsaved/etc). In the end when it was all working I thought to myself that I should have used a state machine.

Re: Why developers never use state machines (2011)

#4
State machines are useful when the same input/event requires different handling based on the current state. There are not that many applications when this is true. Most of the time only two handlers in each state are needed, success and failure, which are much better modeled through a normal code than an explicit state machine.

At the framework level they might be pretty useful, but they rarely appear at the first version, but as a result of refactoring.

Re: Why developers never use state machines (2011)

#5
Most people attempt to achieve Idempotence whenever possible, and there are several reasons this is desirable in HA systems. Fundamentally, the REST paradigm broke peoples brains, and set many projects on a doomed trajectory into the sun.

That being said, for some types of problems a high-latency finite state machine greatly simplifies the complexity of handling data stream consolidation efficiently. The trick is knowing when it should be avoided like in shared-state awareness problems... you know, the "lets pause the internet while I do this one op" guy we all work with sometimes... =)

Re: Why developers never use state machines (2011)

#7
I've used state machines twice, both times for GUIs.

It took more time to program than a bunch of messy if-else logic would, but it was well worth it. The knowledge that your GUI can't find itself in an invalid state is realy reasuring, and also it's preety easy to add new states/logic after you get over the initial hump.

Re: Why developers never use state machines (2011)

#8
I'm an EEE, and one of the strange things I've noticed is that, for whatever reason, people from a CompSci background is really, really have trouble building and understanding even simple state machines.

I suspect this unintuitiveness is the main reason why we don't see more state machines, as even in the case where they're an optimal solution many SWEs will shy away from them.

Locally-rendered UIs are probably one of the best examples of where state machines shine. State-machine based ImGuis lower complexity by orders of magnitude, yet, people still resist.

Re: Why developers never use state machines (2011)

#9
The answer is LabVIEW. State machines in a graphical language are a joy and very easy to work with. It’s clear what state you’re looking. At and how to trace the execution.

Most text language based state machines are a mess and difficult to reason about.

FWIW I maintain a state machine library for Ruby called wicked that (IMHO) isn’t too bad, but can still get gnarly if you’re not careful.

Re: Why developers never use state machines (2011)

#10
I recently designed a state machine serialization that is extremely powerful.

My problem with most state machines is that they are tied to your implementation language. And they are ad hoc.

An implementation programming language is itself a state machine - if you think of memory as being the states the machine can be in.

My state machine serialization supports and defines behaviour between concurrent threads and collections.

It defines a state machine between threads in async/await thread pool

Here is the serialization:

  next_free_thread = 2
  task(A) thread(1) assignment(A, 1) = running_on(A, 1) | paused(A, 1)

  running_on(A, 1)
  thread(1)
  assignment(A, 1)
  thread_free(next_free_thread) = 
    fork(A, B)
    |         send_task_to_thread(B,  next_free_thread)
    | running_on(B, 2)                         paused(B, 1)
                                    
    running_on(A, 1)
   | { yield(B, returnvalue) | paused(B, 2) }
      { await(A, B, returnvalue) | paused(A, 1) }
   | send_returnvalue(B, A, returnvalue)
This defined a progression of a task on a thread and that can fork to a background task and then synchronize with a yield later on
Post reply on HN