<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adam Fraser-Kruck</title>
    <description>The latest articles on DEV Community by Adam Fraser-Kruck (@adam_fraserkruck_7bd95c6).</description>
    <link>https://dev.to/adam_fraserkruck_7bd95c6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2111295%2F426a7c60-e0bf-496f-ad85-848b0122cfb3.jpg</url>
      <title>DEV Community: Adam Fraser-Kruck</title>
      <link>https://dev.to/adam_fraserkruck_7bd95c6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adam_fraserkruck_7bd95c6"/>
    <language>en</language>
    <item>
      <title>Generate state machines from draw.io+ with StateSmith (JavaScript, TypeScript, Python, Java, C#, C++, C)</title>
      <dc:creator>Adam Fraser-Kruck</dc:creator>
      <pubDate>Mon, 23 Sep 2024 04:43:08 +0000</pubDate>
      <link>https://dev.to/adam_fraserkruck_7bd95c6/generate-state-machines-from-drawio-with-statesmith-javascript-typescript-python-java-c-c-c-181i</link>
      <guid>https://dev.to/adam_fraserkruck_7bd95c6/generate-state-machines-from-drawio-with-statesmith-javascript-typescript-python-java-c-c-c-181i</guid>
      <description>&lt;p&gt;Hi. I'm the author of &lt;a href="https://github.com/StateSmith/StateSmith" rel="noopener noreferrer"&gt;StateSmith&lt;/a&gt;: a free and open source program that turns your state machine diagrams into code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosaec3zw6srg4saz87lt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosaec3zw6srg4saz87lt.png" alt="StateSmith transforms diagrams into code" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article is a quick intro for &lt;code&gt;JavaScript&lt;/code&gt;/&lt;code&gt;TypeScript&lt;/code&gt; developers, but we also support &lt;code&gt;Python&lt;/code&gt;, &lt;code&gt;Java&lt;/code&gt;, &lt;code&gt;C#&lt;/code&gt;, &lt;code&gt;C++&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The generated state machines have zero dependencies, are easy to read/debug, and can run anywhere (web, nodejs, embedded script...). It only requires ECMAScript 5 (ES5) which was released in 2009.&lt;/p&gt;

&lt;p&gt;A single diagram is transformed into a single code file that is ready to be used in your project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0svvnbdli3sv5m59gc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0svvnbdli3sv5m59gc1.png" alt="single diagram, single code file" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  JavaScript Applications
&lt;/h2&gt;

&lt;p&gt;Reasons you might try StateSmith as a JavaScript developer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You use state machines in multiple programming languages. Learn one tool that can generate code for all of them.&lt;/li&gt;
&lt;li&gt;You want powerful diagramming tools that work offline and don't require a paid subscription.&lt;/li&gt;
&lt;li&gt;You think your state machine may grow to a medium or large size.&lt;/li&gt;
&lt;li&gt;You are interested in game development, IoT, robotics, embedded, ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I've really enjoyed using StateSmith for modelling relatively complex enemy "AI" for a js game I'm working on. The enemy blobs have 15 high level behaviors. I'll be releasing a tutorial on this soon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flqadpwh60y55v1oyl2mq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flqadpwh60y55v1oyl2mq.gif" alt="game dev AI state machine example in action" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;StateSmith has a lot of features that make it easy to work with both simple and complex state machines. The below &lt;code&gt;draw.io&lt;/code&gt; diagram shows the high level states of my enemy blob "AI". Notice how the &lt;code&gt;IDLE&lt;/code&gt; and &lt;code&gt;HUNTING&lt;/code&gt; states are visually collapsed. You can jump into them to see their sub-states. This is super helpful for managing large complex state machines.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcvlf3l983nb3q4l1h2p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcvlf3l983nb3q4l1h2p1.png" alt="game dev AI state machine example" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;StateSmith is also handy for smaller state machines that have lots of transitions between states (e.g. a game character with many animations). One nice thing about draw.io and StateSmith is that you can embed images/gifs in your diagrams if you want.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gyf8npnr9e8ej3dg9e4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gyf8npnr9e8ej3dg9e4.gif" alt="game dev character state machine" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Features and Interactive Examples 🌟
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://statesmith.github.io/fundamentals-1/" rel="noopener noreferrer"&gt;fundamentals-1&lt;/a&gt; webpage has simple interactive examples that let you explore most StateSmith features.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fop2n8i1cfc1icg74kdub.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fop2n8i1cfc1icg74kdub.gif" alt="fundamentals gif" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Start 🚀
&lt;/h2&gt;

&lt;p&gt;Want to jump right in and just try it?&lt;/p&gt;

&lt;p&gt;The below tutorials use different diagram tools (&lt;code&gt;PlantUML&lt;/code&gt; or &lt;code&gt;draw.io&lt;/code&gt;), but otherwise mirror each other closely.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/StateSmith/tutorial-3" rel="noopener noreferrer"&gt;Tutorial-3&lt;/a&gt; - &lt;code&gt;PlantUML&lt;/code&gt;. A good choice if you like text based diagramming.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/StateSmith/tutorial-4" rel="noopener noreferrer"&gt;Tutorial-4&lt;/a&gt; - &lt;code&gt;draw.io&lt;/code&gt;. Has text and video 🎥 lessons. Good if you want more diagram control or will be making large designs.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Main Repo
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://github.com/StateSmith/StateSmith" rel="noopener noreferrer"&gt;https://github.com/StateSmith/StateSmith&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Comparison to XState
&lt;/h2&gt;

&lt;p&gt;In the JavaScript world, XState is by far the most popular state machine library.&lt;/p&gt;

&lt;p&gt;First of all, I want to say that I think David Khourshid (the creator of &lt;a href="https://github.com/statelyai/xstate" rel="noopener noreferrer"&gt;XState&lt;/a&gt;) is a really good guy. He's done a lot for the state machine js community and has a lot of passion for state machines. I hope his commercialization of XState via &lt;a href="https://stately.ai/" rel="noopener noreferrer"&gt;Stately&lt;/a&gt; is successful. It would be nice to see more people making a living with open source software.&lt;/p&gt;

&lt;p&gt;For this comparison, keep in mind that I'm an embedded systems developer that enjoys web/app/game development on the side. I'm not a professional web developer.&lt;/p&gt;

&lt;p&gt;Please let me know if I make any mistakes.&lt;/p&gt;

&lt;h3&gt;
  
  
  General Tool vs Specialized Tool 🛠️
&lt;/h3&gt;

&lt;p&gt;StateSmith is more of a swiss army knife for state machines. It has a lot of features and supports many programming languages, but isn't specialized for any one use case. While StateSmith does have a lot of interactive learning resources &amp;amp; tutorials, it does not yet have tutorials for specific web frameworks.&lt;/p&gt;

&lt;p&gt;In contrast, XState focuses only on JavaScript/TypeScript and has tie-ins/tutorials for popular web frameworks.&lt;/p&gt;

&lt;p&gt;There is some overlap in features between StateSmith and XState/Stately, but each have unique features.&lt;/p&gt;

&lt;h3&gt;
  
  
  Free vs Paid 💰
&lt;/h3&gt;

&lt;p&gt;StateSmith is fully free and open source. This includes the diagramming and simulation tools.&lt;/p&gt;

&lt;p&gt;XState is free and open source, but you really want a paid subscription to &lt;code&gt;Stately&lt;/code&gt; if your design approaches a "medium" size. Without a paid subscription, &lt;code&gt;Stately&lt;/code&gt; is unusable for many users as your diagrams must be public. &lt;/p&gt;

&lt;p&gt;A paid subscription isn't necessarily a bad thing though (if you can afford it). One of the big benefits of &lt;code&gt;Stately&lt;/code&gt; being a paid service is that they have staff working on it full time. This means they can provide better support and add more features. In contrast, StateSmith is largely just Adam (me) working on it in my free time :) StateSmith has had some great contributions from the community, but we don't yet have a core team of open source developers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Managing Complexity ⚖️
&lt;/h3&gt;

&lt;p&gt;If your state machine is small, you are likely to be more productive in XState (or hand coding).&lt;/p&gt;

&lt;p&gt;StateSmith really starts to shine when your state machine grows to a medium or large size.&lt;/p&gt;




&lt;p&gt;Thanks for reading! I hope you found this interesting. If you have any questions, feel free to ask. I'm happy to help.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>productivity</category>
      <category>python</category>
      <category>java</category>
    </item>
  </channel>
</rss>
