Live data from Hacker News

Why Developers Never Use State Machines (2011)

skorks.com

121–130 of 164 posts

Re: Why Developers Never Use State Machines (2011)

#121
post #12

Traditional programming languages and imperative thinking don't lend themselves to writing state machines, so its not really that surprising that they're under utilized. Its kind of frustrating constantly reading posts where people shit on functional programm(ers|ing) for being too ivory tower-y or whatever, when its kind of hard not to be when a lot of the older FP guys have spent the last like 20-30 years going "yo…

I don't think there is a conflict between imperative thinking and state machines, but I do agree that lots of programming languages neither provide built-in features for expressing state machines nor support expressing them particularly elegantly, and that the superior support for general abstraction in certain functional languages reduce the friction of use.

It's pretty easy to imagine, though, a class based OOP language where each class was also an HSM, with methods defined per state and identifying transitions (as well as states and their entry and exit behavior being explicitly defined.)

Re: Why Developers Never Use State Machines (2011)

#122
post #117

One thing formal state machines make difficult is component reuse. Let's say you have a music playback control component you wish to reuse across a feature rich player, as well as a mini playback controller. You COULD just use the full state machine to power the mini player, but most of the transitions would be unused. It would be unclear to the next maintainer which aspects were needed for each use case, thus making…

Ah... your "independent playback progress/state class" is another state machine, just better with an architecture that suits your use case. "Full state machine" is a misnomer and common misunderstanding of the power of state machines. Any state machine bordering on complexity that is hard to manage can be broken into multiple smaller, easier to grasp and manage state machines. And state machines transparently integrate with functional programming, so any ideas contrary to that is yet another misunderstanding of them.

Re: Why Developers Never Use State Machines (2011)

#123
post #78

Earlier quoted context omitted.

>the older FP guys have spent the last like 20-30 years going "yo this is dope you guys should really be doing this" and getting largely ignored I've not been ignoring them. I tried at least three different Haskell tutorials and articles explaining how cool it is. The problem was the author would show some code sand say 'see how easy it is to do this, and this, and this!', but I'd look at the code and have no idea wh…

> The problem was the author would show some code sand say 'see how easy it is to do this, and this, and this!', but I'd look at the code and have no idea whatever what it was doing or how it was doing it, and the guide/tutorial wouldn't tell me. After a few tries, I just gave up. You're better off learning OCaml or F# for a small project or two, honestly. The jump from pure procedural to pure functional is big enoug…

FWIW, I came to Haskell by way of OCaml, and it seems to have gone well.

Re: Why Developers Never Use State Machines (2011)

#124

Earlier quoted context omitted.

Haskell code for that looks quite nice as well - however, it's less about a language per se but rather about availability of libraries and how mature / well-built their API is. For quite a few FP languages the ecosystem is there and you can do all these problem domains nicely, they just tend to be skipped in tutorials as they rely on non-core third party libraries (sometimes with multiple nice but very different alte…

> Haskell code for that looks quite nice as well - however, it's less about a language per se but rather about availability of libraries and how mature / well-built their API is. That might be the case, but you simply never see tutorials for writing a CRUD-like web application in Haskell. Maybe they exist, but they don't seem to show up very often here or on reddit or whatever.

https://www.yesodweb.com/book

http://haskell-servant.readthedocs.io/en/stable/tutorial/

http://happstack.com/docs/crashcourse/index.html

Re: Why Developers Never Use State Machines (2011)

#125
post #117

One thing formal state machines make difficult is component reuse. Let's say you have a music playback control component you wish to reuse across a feature rich player, as well as a mini playback controller. You COULD just use the full state machine to power the mini player, but most of the transitions would be unused. It would be unclear to the next maintainer which aspects were needed for each use case, thus making…

This was a nice thing in the example from Raganwald's blog post. The `transitionsTo` decorator:

  function transitionsTo (stateName, fn) {
    return function (...args) {
      const returnValue = fn.apply(this, args);
      this[STATE] = this[STATES][stateName];
      return returnValue;
    };
  }
By separating the state transitions from the state actions in this way it allows the functional components to be reused much more easily. You can construct the miniplayer using only the states you need with only the transitions you need (rather than obscured/hidden capabilities).

Re: Why Developers Never Use State Machines (2011)

#126
post #83

Earlier quoted context omitted.

Heh, very often their problem domain is also an issue. "We're going to write a compiler in our shiny FP language". "We're going to implement a Fourier transform". Nah, thanks, could you show me instead how I can get the Employee record from an XML file, stick it in a PostgreSQL database and also send it down the wire through a REST API as JSON?

you forgot the "oh and requirements will change randomly as you're getting employee record from xml file to etc etc

[deleted]

Re: Why Developers Never Use State Machines (2011)

#127

The biggest source of resistance that I've run into is not with the concept of state machines but with common implementations. Two examples: * A quarter-century ago I implemented a cluster membership protocol and coordination system as a state machine. This provided great benefits in terms of verifiability, extensibility, etc. but some developers complained while the current state was explicit the history of how we g…

I find the biggest resistance is the obvious fact that implementing a state machine requires planning: mapping out the goals, identify which are your formal states, map out the transitions, evaluate the overall logic requirement and then design the state machine's data structure and classes... All the "planning" is the opposite of what most "code slinging macho programmer personalities" want to do; they want to jump into coding an just wing it. That is the real issue here: Undisciplined development.

Re: Why Developers Never Use State Machines (2011)

#128
post #12

Traditional programming languages and imperative thinking don't lend themselves to writing state machines, so its not really that surprising that they're under utilized. Its kind of frustrating constantly reading posts where people shit on functional programm(ers|ing) for being too ivory tower-y or whatever, when its kind of hard not to be when a lot of the older FP guys have spent the last like 20-30 years going "yo…

> a lot of the older FP guys have spent the last like 20-30 years going "yo this is dope you guys should really be doing this"

many of "the older FP guys" actually think "it's cool, it's mathematical. But it's not practical."

Re: Why Developers Never Use State Machines (2011)

#129
post #83

Earlier quoted context omitted.

Heh, very often their problem domain is also an issue. "We're going to write a compiler in our shiny FP language". "We're going to implement a Fourier transform". Nah, thanks, could you show me instead how I can get the Employee record from an XML file, stick it in a PostgreSQL database and also send it down the wire through a REST API as JSON?

you forgot the "oh and requirements will change randomly as you're getting employee record from xml file to etc etc

In my experience, that's really not an argument against Haskell. I find it easier to make large changes in my representation of the world in Haskell than in any other language I've worked in.

Re: Why Developers Never Use State Machines (2011)

#130
post #117

One thing formal state machines make difficult is component reuse. Let's say you have a music playback control component you wish to reuse across a feature rich player, as well as a mini playback controller. You COULD just use the full state machine to power the mini player, but most of the transitions would be unused. It would be unclear to the next maintainer which aspects were needed for each use case, thus making…

Reuse is important, but realistically, how much code is actually re-used? Sure, you have a lot of meaningful subcomponents or libraries that should be correctly isolated for re-use (and really, for testing purposes) but moving a state machine into the core of an application and away from segmented components and libraries hardly harms whatever reuse is likely to happen.
Post reply on HN