3 ms·
I'm fairly sure I've said this before here and elsewhere, but bears repeating, especially for this post. Statecharts is currently probably the most undervalued
by diggan 6y ago
I'm fairly sure I've said this before here and elsewhere, but bears repeating, especially for this post.
Statecharts is currently probably the most undervalued tool when it comes to programming GUIs with state. Statecharts are a continuation of state machines, but with less footguns and better abstractions to be able to build larger systems.
In the end, you either build a GUI that are using state machines implicitly, or explicitly. Tends to be less prone to bugs if you do so explicitly.
If you're interested, here is some starting points (copied from an older comment of mine):
Here is the initial paper from David Harel: STATECHARTS: A VISUAL FORMALISM FOR COMPLEX SYSTEMS (1987) - https://www.inf.ed.ac.uk/teaching/courses/seoc/2005_2006/res https://www.inf.ed.ac.uk/teaching/courses/seoc/2005_2006/res...
Website with lots of info and resources: https://statecharts.github.io/ https://statecharts.github.io/
And finally a very well made JS library by David Khourshid that gives you lots of power leveraging statecharts: https://github.com/davidkpiano https://github.com/davidkpiano
While we're at it, here are some links to previous submissions on HN regarding statecharts with lots of useful and interesting information/experiences:
- https://news.ycombinator.com/item?id=18483704 https://news.ycombinator.com/item?id=18483704
- https://news.ycombinator.com/item?id=15835005 https://news.ycombinator.com/item?id=15835005
- https://news.ycombinator.com/item?id=21867990 https://news.ycombinator.com/item?id=21867990
- https://news.ycombinator.com/item?id=16606379 https://news.ycombinator.com/item?id=16606379
- https://news.ycombinator.com/item?id=22093176 https://news.ycombinator.com/item?id=22093176
- bmitc 6y agoThere is an edX course that covers state charts. It’s excellent, and Davis Harel is one of the co-instructors. https://www.edx.org/course/programming-for-everyone-an-introduction-to-visual https://www.edx.org/course/programming-for-everyone-an-intro...
- diggan 6y agoThat's amazing, didn't know that, thanks a lot for sharing! Fitting that it starts today as well :)
- bmitc 6y agoNo problem! And as just a note, it starts everyday since it is a reoccurring self-paced course. But if I remember correctly, the instructors still answer questions. I really enjoyed learning about the hierarchical state machines using statecharts.
- pixel_tracing 6y agoThis isn’t really solving issues for most teams, how do you handle maintenance and tech debt of these state charts? And why only javascript example? You’re missing mobile
- diggan 6y agoStatecharts are the solution to technical debt, not the source, as you're formalizing the possible states the user can be in, and "locking" it to that. You can even apply analysis to your codebase to figure out if you're actually covering all possible states and transitions. If you take a look at the syllabus of the course, it's not about JavaScript, it's about the formalism and understanding the core concepts, without locking you to a particular technology. Whatever they are teaching in the course, you can apply to JavaScript/Swift as much as you can apply it to Desktop/Mobile. Disclaimer: haven't actually taken the course, but planning to and I've read the description of it.
- pixel_tracing 6y agoThis isn’t answering my original question, how do you get teams to adopt this? Until this process is made easy this will always just be a fever dream of fools in ivory towers
- diggan 6y agoI'm sorry, your "original" question doesn't seem to exist in your previous comments, so hard for me to answer it... You get people to adopt technology or paradigms by explaining the benefits and drawbacks of adopting that set of technology/paradigm and then discussing it together with your team/s. Not sure why it would be different for Statecharts compared to any other paradigm? What process is too hard for you now exactly, to describe states or something? You're already doing this implicitly when you write UI code with state. Statecharts only changes it from being implicit to being explicit. If you're having a hard time actually naming your states, you can use tools like https://sketch.systems/ https://sketch.systems/ to explore what you're building with a simple DSL, then implement it properly in the programming language of your choice.
- sillysaurusx 6y agoI really don't mean to be dismissive of your contribution, so please take this in the best light possible: Yuck. Use continuations with anonymous fns that tell the GUI what to do next. The state is in the closure implicitly! I wish I had an articulate counterargument, but just look at this: https://xstate.js.org/docs/#promise-example https://xstate.js.org/docs/#promise-example Statecharts are a formalism for modeling stateful, reactive systems. Beautiful is in the eye of the beholder, and never moreso when the programmer is blind to the cost of repetition and state. Arc got it right. http://www.paulgraham.com/arcchallenge.html http://www.paulgraham.com/arcchallenge.html Write a program that causes the url said (e.g. http://localhost:port/said http://localhost:port/said) to produce a page with an input field and a submit button. When the submit button is pressed, that should produce a second page with a single link saying "click here." When that is clicked it should lead to a third page that says "you said: ..." where ... is whatever the user typed in the original input field. The third page must only show what the user actually typed. I.e. the value entered in the input field must not be passed in the url, or it would be possible to change the behavior of the final page by editing the url. (defop said req (aform [onlink "click here" (pr "you said: " (arg _ "foo"))] (input "foo") (submit)))
- diggan 6y agoIn no way was that dismissive, I thank you for providing another perspective, that's always welcome in my book! I don't necessarily agree with all the implementation details of xstate, in particular to where the logic tend to be located in practice, and the reliance on the Actor model for many things in the wild. I rather try to guide people to Statecharts as a paradigm overall, and if you happen to use JS, I think xstate is probably the most mature library there. But as all libraries/frameworks, they can be over-relied upon. If you're in the Clojure/Script world, which is where I mainly locate myself, then https://lucywang000.github.io/clj-statecharts/ https://lucywang000.github.io/clj-statecharts/ is all you need and so far the library I've had the best luck with.
- sillysaurusx 6y agoI came back to edit my comment and remove the harsh words, but you left a very kind response. Sorry. This is actually a fascinating example: https://lucywang000.github.io/clj-statecharts/docs/get-started/ https://lucywang000.github.io/clj-statecharts/docs/get-start... ... because it highlights precisely my criticism. ;; define the machine (def machine (fsm/machine {:id :lights :initial :red :context nil :states {:green {:on {:timer {:target :yellow :actions (fn [& _] (println "transitioned to :yellow!")) }}} :yellow {:on {:timer :red}} :red {:on {:timer :green}}} :on {:power-outage :red} })) I just ... don't understand why anyone would do it this way. The code itself already says what to do. Adding this sort of data only subtracts from clarity with no additional flexibility. You might argue that the data model makes it flexible. But I look at that and go, that's what a function is for. The only thing you need is `(println "transitioned to :yellow!")` inside of a function called transition-to-yellow, or if you're feeling adventurous, a function called transition-to which takes yellow as an argument.