Live data from Hacker News

Statecharts: hierarchical state machines

statecharts.dev

31–40 of 91 posts

Re: Statecharts: hierarchical state machines

#31
post #13

Hierarchical state charts have been formalized as a type of UML diagram. Details: https://en.wikipedia.org/wiki/UML_state_machine

I think it's either "hierarchical state machines" or just "statecharts", otherwise it's a bit like saying ATM machine or CC card.

Re: Statecharts: hierarchical state machines

#32

"No statechart will survive contact with real world applications". I mean, when you have external dependencies, multilayer protocols, multithreading, perf requirements, the state will becomes an ugly mess. One can only dream of a clean statechart.

Sometimes the complexity is unavoidable and you have to pick the lesser of many evils. Oftentimes complexity management is trying to model the system in such a way that the complexity moves to the area which is most easily understood. It doesn't make the system less complex, only easier to reason about and maintain. State-oriented design is just a (very powerful) tool in the toolbox.

Even when I haven't actually used a state machine, I've modelled problems as state machines just to help me think about the system. Thinking in terms of state can often help traditional software designs.

Re: Statecharts: hierarchical state machines

#33
A long time ago, I wrote a compiler from a curly-brace encoding of Statecharts, to Java.

More recently, I avoided some early SwiftUI bugs by modeling some of the iOS app interaction using Statecharts, in an ASCII art comment in the code, and implementing that.

Re: Statecharts: hierarchical state machines

#34
I love statecharts, but I wanted to challenge some of the "why"s in this post.

> It’s easier to understand a statechart than many other forms of code.

There's a lot of arguable quantifiers here, but when I was started at https://gadget.dev (no longer working there), we started with hierarchical statecharts as a way for users to specify their behaviour (with the ability to customize transitions with code snippets). No matter what we did, users just found it incredibly confusing. This was with a customer based that had decent spread across the "how technical are you" spectrum.

Did we do a poor job of introducing it to the user? Maybe. We were forced to switch everything over to flat code though.

> As complexity grows, statecharts scale well.

I think there's a big asterisk on this one, likely. I had to undo a lot of unfortunate statechart code that was introduced for some of the backend of gadget, and I consider myself a pretty good programmer. Perhaps it's just the difference in paradigm, or the accidental complexity of how we worked with xstate, but I reduced 1000+ lines of very hard to read and extend statechart code to a few hundred lines of imperative code, with some hardcoded states. It was actually easier and faster for me to do this than fix bugs that existed somewhere in there (I tried, and failed).

Every engineer at the company (it's a small company :P) agreed that it was substantially easier to read and understand.

I think this perhaps echoes things said elsewhere here, like dflock's comment on the boundary between "xstate" and "not xstate".

All that being said, I'll restate: I love the idea of statecharts, I just have scars from dealing with them. I think they were perhaps applied in situations where the benefit was low, and the overhead of understanding the bits and bobs of xstate was too high of a cost for any perceivable benefit it may have offered.

> It’s worth noting that you’re already coding state machines, except that they’re hidden in the code.

I will say that this is a great point that may often be overlooked.

When I interview candidates, one problem I enjoy doing (and they do too!) is "let's try building vim" problem. We build out a few basic commands — move left, move right, replace character under cursor — to edit a string of text. The candidates that do the best often see how parsing can be encoded in a state machine and plan with that in mind. They perhaps still just encode it as if/else statements with a state variable, but that still gives the better outcome. Something like this:

      0-9
   ---------
   |       |
   |       v        hj
   |-- (number N) -----> move N spaces
           |
           |   r
           ------> (read char c) ---> replace N characters with c
[EDIT] And I should say, I _am_ repeating some of what's stated in the linked site (under "Why should you not use statecharts?")

Re: Statecharts: hierarchical state machines

#36

The title contains hierarchical, which does not come back in the post. You probably need hierarchy, otherwise state charts become unweildingly large.

A statechart without hierarchy is just a state machine. It's the composition and hierarchy that turns a state machine into a statechart.

Is the very first example not one without hierarchy and thus just a state machine?

Re: Statecharts: hierarchical state machines

#37
post #27

I wonder if it's possible to combine statecharts with durable execution engines like Temporal, DBOS, Restate, etc. At work we use Cloudflare Workflows for managing onboarding and payment workflows. It generates flowchart diagram that is useful for quickly reasoning about what the workflow does, which I guess is what statecharts is trying to achieve.

When you say "it generates flowchart diagrams…" what exactly is generating them? Is it built into cloudflare workers or is it something your team created?

Cloudflare workers shows a visualisation of your workflow in their dashboard, but it’s imperfect

Re: Statecharts: hierarchical state machines

#38

Glad to see statecharts still getting attention! I created XState, a JS/TS library for authoring, executing, and visualizing state machines/statecharts: https://github.com/statelyai/xstate I've been working on it for 10+ years. The main thing I've learned is that statecharts are most valuable when they're treated as executable behavior, not just documentation. That doesn't mean you need to use them everywhere or mode…

Xstate has helped me clean up some hairy situations in the past. Looking forward to the next version, and thanks for your great work!

Re: Statecharts: hierarchical state machines

#39

Earlier quoted context omitted.

A statechart without hierarchy is just a state machine. It's the composition and hierarchy that turns a state machine into a statechart.

Is the very first example not one without hierarchy and thus just a state machine?

Technically yes, that's just a state machine. On https://statecharts.dev/what-is-a-state-machine.html the website itself also admits that that example is a "simple state machine", and on https://statecharts.dev/what-is-a-statechart.html you get the better explanation with

> A statechart is a state machine where each state in the state machine may define its own subordinate state machines, called substates

Post reply on HN