<?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: Mark Truluck</title>
    <description>The latest articles on DEV Community by Mark Truluck (@framelang).</description>
    <link>https://dev.to/framelang</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%2F583835%2F3963c2ff-464d-4d4b-830f-cbd3c33ced82.jpeg</url>
      <title>DEV Community: Mark Truluck</title>
      <link>https://dev.to/framelang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/framelang"/>
    <language>en</language>
    <item>
      <title>Are you Coding in The Upside Down?</title>
      <dc:creator>Mark Truluck</dc:creator>
      <pubDate>Fri, 30 Apr 2021 13:50:20 +0000</pubDate>
      <link>https://dev.to/framelang/are-you-coding-in-the-upside-down-2o44</link>
      <guid>https://dev.to/framelang/are-you-coding-in-the-upside-down-2o44</guid>
      <description>&lt;p&gt;In the Netflix show "Stranger Things", some 1980s kids go to an alternate universe they call &lt;strong&gt;The Upside Down&lt;/strong&gt; under the evil control of a horrible creature called the &lt;strong&gt;Mind Flayer&lt;/strong&gt;. In the Upside Down, things sort of look like our universe, but are confusingly different and it is decidedly easy to make a deadly wrong move.&lt;/p&gt;

&lt;p&gt;Programming can often feel similar, especially when coming in cold to a new piece of complex software and trying to figure out what is going on. This common situation is made harder by a typical lack of documentation and comments. The standard refrain of "the code is the best documentation" is woefully uncomforting when traipsing through a tangled nest of inexplicable conditional logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SLaGFzP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44tzgth0as2orpjn2592.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SLaGFzP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44tzgth0as2orpjn2592.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[&lt;a href="https://k3no.medium.com/organizing-your-python-code-ca5445843368"&gt;Keno Leon&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Like so many things, we know spaghetti code when we see it, but is it possible to characterize bad code in a more objective way? Can you recognize a rhetorical question? Of course you can!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Leaderboard of Horrors
&lt;/h2&gt;

&lt;p&gt;Although there are many ways to obfuscate the purpose of code, my list for worst offenders are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implicit Logical State&lt;/li&gt;
&lt;li&gt;State Fragmentation&lt;/li&gt;
&lt;li&gt;State Entanglement&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What we will see is that these are simply emergent properties of the typical way to code object-oriented classes! So these problems are baked right in to business-as-usual.&lt;/p&gt;

&lt;p&gt;As elevators feature prominently in the battles against the Mind Flayer, we will use one as a poster monster for all the ills of coding-as-usual. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UXUHlXNz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3swmc6e3iv3xcd4ofdi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UXUHlXNz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3swmc6e3iv3xcd4ofdi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's investigate these terrors one-by-one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit Logical State
&lt;/h2&gt;

&lt;p&gt;Everyone knows software has state. Most often the term is discussed with regards to the actual values in the data. For instance, a signed 32 bit integer has a range of [-2147483648 to 2147483647]. That's a lot of possible states!&lt;/p&gt;

&lt;p&gt;However we don't often assign logical meaning to each and every distinct value. Instead we chunk up that total range into just a few subranges with meaning, sometimes quite arbitrarily. For instance we might make this map of value =&amp;gt; logical state for an elevator simulation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Values&lt;/th&gt;
&lt;th&gt;Logical State&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 to 2147483647&lt;/td&gt;
&lt;td&gt;Going Up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-2147483648 to -1&lt;/td&gt;
&lt;td&gt;Going Down&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(And no, I'm not concerned about boundary conditions. Going to floor 10 gazillion is totally possible on this elevator.)&lt;/p&gt;

&lt;p&gt;It's not a very useful elevator yet, but often system design starts with the most common modes of operation and then adding nuance to it. &lt;/p&gt;

&lt;p&gt;Let's take a look at a simple skeleton implementation of this elevator (I know you are worried about how to get off the elevator but don't. It's just an example and you can't really get on yet anyway).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OrDvSVnW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7oe4gl9d5maue4te6p4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OrDvSVnW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7oe4gl9d5maue4te6p4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our table above we have two logical states declared. In our code, however, there is no explicit declaration what they are!&lt;/p&gt;

&lt;p&gt;There is nothing about &lt;code&gt;(direction &amp;gt;= 0)&lt;/code&gt; that just screams this means "Going UP" to the uninformed reader. Instead it's a total superposition of a logical meaning on an arbitrary range of values. This is bad, and as much as you may be saying "I never do this", I'm guessing you've seen it done by somebody else. Am I right?&lt;/p&gt;

&lt;p&gt;Additionally, this boolean can be constructed in a number of ways and get the same functional output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(direction &amp;gt; -1)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;!(0 &amp;gt; direction)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;etc. &lt;/p&gt;

&lt;p&gt;This is the problem of &lt;strong&gt;implicit logical state&lt;/strong&gt;. Code structured to use boolean tests to determine ranges of low level data state that &lt;em&gt;implicitly&lt;/em&gt; define logical state are 1) hard to understand and 2) incredibly fragile. &lt;/p&gt;

&lt;p&gt;I know you all are thinking of 10 ways to avoid this - and they would all help. But the fact that you have to devise strategies to mitigate a problem that shouldn't exist in the first place is the point of this article. But more to come on the solution later.&lt;/p&gt;

&lt;p&gt;Now, we can't always avoid performing conditional tests and, in fact, they aren't inherently "bad" in the first place. However, they are often used &lt;em&gt;badly&lt;/em&gt;. And when it relates to determining what our object's internal state is, we can certainly do much better in a lot of cases.&lt;/p&gt;

&lt;p&gt;So while you may be able to dodge the worst effects of implicit state by good software engineering practices, not so with the next situation. Let's add some comments and expose the next kind of serious problem this kind of code structure &lt;strong&gt;enforces&lt;/strong&gt;. (There is no escape for you this time).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i5l06Kqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18eufkc88k7evjsssc1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i5l06Kqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18eufkc88k7evjsssc1t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  State Fragmentation
&lt;/h2&gt;

&lt;p&gt;People naturally think in terms of &lt;em&gt;context&lt;/em&gt; - where am I, when is it, what is the situation. By understanding context we can then better understand what is happening. &lt;/p&gt;

&lt;p&gt;In this example, the two contexts are &lt;code&gt;Going Up&lt;/code&gt; and &lt;code&gt;Going Down&lt;/code&gt;. However, notice how the code for each of these contexts are fragmented across the event handlers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PrKYtfuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu40z9g02tffg4r8aepk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PrKYtfuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu40z9g02tffg4r8aepk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I bet you are so used to this that it doesn't strike you as &lt;strong&gt;bad&lt;/strong&gt;, but it really is.&lt;/p&gt;

&lt;p&gt;This scattering of a logical state throughout event handlers is one of the most challenging aspects of object-oriented programming languages. And it is bad for you (the developer) for the same reason fragmenting files across a hard drive is bad for your computer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VIx7PriE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukj0xxwrfs4wxujslo82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VIx7PriE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukj0xxwrfs4wxujslo82.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your brain has to do the same work as the hard drive - stitch together the parts of a logical "thing" to make complete sense of it. This just isn't how we perceive the world effectively. &lt;/p&gt;

&lt;p&gt;Instead, people and hard drives like logically related things kept in the same place. I will assert people in particular (don't know about hard drives) think "contextually" - where am I, what am I doing, what is the situation - and then think about events to decide what to do. &lt;/p&gt;

&lt;p&gt;And this gets to the heart of the matter, as all of the programming languages we are most likely using on our projects have &lt;strong&gt;no syntax or concept of explicit logical state&lt;/strong&gt;.  Instead, as we have seen, logical state is &lt;em&gt;inferred&lt;/em&gt; from low level data. &lt;/p&gt;

&lt;p&gt;The solution that I will be proposing fixes this problem.&lt;/p&gt;

&lt;p&gt;But first, we must check off the last of the worst offenders - and its a doozy.&lt;/p&gt;
&lt;h2&gt;
  
  
  State Entanglement
&lt;/h2&gt;

&lt;p&gt;In the Upside Down I don't know why you would, but say we actually wanted to get on our elevator. Then we'd just hack out a new range and shove a Stopped state right in! Easy peasy.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Values&lt;/th&gt;
&lt;th&gt;Logical State&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 to 2147483647&lt;/td&gt;
&lt;td&gt;Going Up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;Stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-2147483648 to -1&lt;/td&gt;
&lt;td&gt;Going Down&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ok so in our &lt;em&gt;specification&lt;/em&gt; we have now redefined the logical states the software can be in, but we haven't yet changed our code. So before we do, let's look at how our existing conditional logic maps our newly specified logical state ranges:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rgK6O08S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5648szj4r86ccmn6cuc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rgK6O08S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5648szj4r86ccmn6cuc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see where the "State Entanglement" problem arises. Our existing rat's nest of boolean logic has already mapped that new logical state right into the existing behavior for &lt;code&gt;Going UP&lt;/code&gt;. What to do?&lt;/p&gt;

&lt;p&gt;Well, we have to go through ALL our code and disentangle this mess. Let's go step-by-step to see how we get to play "whack-a-mole" with this new state.  &lt;/p&gt;

&lt;p&gt;First lets fix &lt;code&gt;Going Up&lt;/code&gt; and get &lt;code&gt;Stopped&lt;/code&gt; out of there:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--746mK-1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t42oqdkk9bjrna4qpatf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--746mK-1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t42oqdkk9bjrna4qpatf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is a partial fix, but it is interesting to get a visual on what would happen if the developer got distracted and checked it in accidentally when invited out to a spaghetti lunch. &lt;/p&gt;

&lt;p&gt;Now we have a situation where the stopped state is mapped to be entangled with &lt;code&gt;GoingDown&lt;/code&gt; in the first method and &lt;code&gt;GoingUp&lt;/code&gt; in the second. If you were to look at this code while the developer was out enjoying meatballs, how would you know that actually &lt;em&gt;both&lt;/em&gt; were wrong?&lt;/p&gt;

&lt;p&gt;As a final bad situation to explore, here we have all of the methods updated with the proper conditional logic for the existing two states: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZtsKUUpY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trzccszfe577jthfmdn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZtsKUUpY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trzccszfe577jthfmdn8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However notice that the &lt;code&gt;else&lt;/code&gt; clause has "captured" the new &lt;code&gt;Stopped&lt;/code&gt; state. Else clauses are a catch-all, and in many situations when a new logical state is added they will accidentally wind up collected there like leaves in a storm drain. It's not a great outcome. &lt;/p&gt;
&lt;h2&gt;
  
  
  Fixed! Or is it?
&lt;/h2&gt;

&lt;p&gt;Here we can finally see the proper introduction of our &lt;code&gt;Stopped&lt;/code&gt; state to the elevator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o8xH4vHb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wxv2vcwvxxzrvfd0n4y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o8xH4vHb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wxv2vcwvxxzrvfd0n4y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While each state is now properly disentangled, we see that the logical states are being torn apart up and down the object-oriented class.&lt;/p&gt;

&lt;p&gt;Do you feel your mind being a bit flayed? I'm afraid it is going to get worse before it gets better.&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting Spagged
&lt;/h3&gt;

&lt;p&gt;This next example doesn't add any new twists to our terrifying tale, but it does help to visually emphasize the Cambrian explosion of fractured logical state metastasizing throughout the codebase:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GJsb5TMY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzov2dmdwpykoyvgnekd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GJsb5TMY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzov2dmdwpykoyvgnekd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eventually it evolves (or devolves?) into a creature you are scared to touch, much less eat:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0E_AjAbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oejvacxso1ojc72o1vfp.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0E_AjAbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oejvacxso1ojc72o1vfp.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://tophdimgs.com/379198-flying-spaghetti-monster.html"&gt;Flying Spaghetti Monster&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just the kind of thing you might expect to find in the Upside Down. &lt;/p&gt;

&lt;p&gt;So what can turn the world right side up? What can save us from going mad from the mind-flaying effects of &lt;strong&gt;Implicit Logical State&lt;/strong&gt;, &lt;strong&gt;State Fragmentation&lt;/strong&gt; and &lt;strong&gt;State Entanglement&lt;/strong&gt;? Glad you asked.&lt;/p&gt;
&lt;h2&gt;
  
  
  Put Your Code In a Box!
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A place for everything and everything in its place - English proverb&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;There is nothing tidier or less spaghetti like than  putting things in boxes.  When you put things in boxes (with a good lid of course) they are much less likely to wriggle out and ruin your day.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uOFUzlpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92jnbotqr0dcm37z860o.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uOFUzlpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92jnbotqr0dcm37z860o.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how do you choose which box to put something in?&lt;/p&gt;

&lt;p&gt;For code, the best answer is an appropriate, small &lt;em&gt;contextual&lt;/em&gt; box. We've identified three contexts for our strange elevator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q48I0fkw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3zoz9qwjgseucq721cz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q48I0fkw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3zoz9qwjgseucq721cz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The "right side up" way to think about what our elevator does is encompassed by being in one of those three contexts, or &lt;em&gt;states&lt;/em&gt;. Everything you can do in our elevator maps to being in one of those states.&lt;/p&gt;

&lt;p&gt;Happily, computer science provides a theoretical, as well as practical, approach to working with states - the &lt;em&gt;state machine&lt;/em&gt; (aka automata). &lt;/p&gt;

&lt;p&gt;If this is the best way to organize software, why isn't everyone doing it? The simple answer is that the popular languages programmers use do not make it a natural act to structure code as a state machine. And that is where &lt;em&gt;&lt;strong&gt;Frame&lt;/strong&gt;&lt;/em&gt; comes in.&lt;/p&gt;
&lt;h2&gt;
  
  
  Frame - a Language for State Machines
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Frame&lt;/strong&gt; is a markdown language for specifying state machines, or automata more generally. Frame's additional big benefit is that it can generate code in many object oriented languages (and more to come) as well as documentation. &lt;/p&gt;

&lt;p&gt;To tie this all together, here is a demo of our elevator specified in Frame and implemented in JavaScript:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/frame-lang/embed/KKajwZM?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, the code is structured around the logical states of the system and not around the events that can happen. This inversion of the structure of code immediately makes the purpose of the software evident and it's organization "natural". Developers no longer have to toil in the bizzare realm of the "Upside Down" and can instead frolic in the light with the clarity of context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T6hA3fDY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4axb2mlcd1ou2uloaqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T6hA3fDY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4axb2mlcd1ou2uloaqo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To find out more about Frame, have a look at &lt;a href="https://frame-lang.org"&gt;the Frame Website&lt;/a&gt; as well as the online version of the &lt;a href="https://framepiler.frame-lang.org"&gt;Framepiler&lt;/a&gt;. The Framepiler is also an &lt;a href="https://github.com/frame-lang/frame_transpiler"&gt;open source project&lt;/a&gt; that is available under MIT License to use in any way you can imagine. It &lt;em&gt;will&lt;/em&gt; lead you to a happier place.&lt;/p&gt;

&lt;p&gt;Happy Framing!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>State Machines Run Your Life! But don't be scared...</title>
      <dc:creator>Mark Truluck</dc:creator>
      <pubDate>Fri, 16 Apr 2021 17:22:09 +0000</pubDate>
      <link>https://dev.to/framelang/state-machines-run-your-life-but-don-t-be-scared-1ife</link>
      <guid>https://dev.to/framelang/state-machines-run-your-life-but-don-t-be-scared-1ife</guid>
      <description>&lt;p&gt;[Image: REUTERS/Mario Anzuoni]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State Machine&lt;/strong&gt; - just the juxtaposition of those two words seems, well, ominous. Is this the totalitarian government of 1984 taking over? Or the Terminator? Or a little of both?&lt;/p&gt;

&lt;p&gt;In truth, state machines are already all around you. All software is fundamentally described by them and I really can't do better than &lt;a href="https://dev.to/davidkpiano/you-don-t-need-a-library-for-state-machines-k7h"&gt;this article&lt;/a&gt; to explain what they are and why you - assuming you are a developer - should be &lt;em&gt;very&lt;/em&gt; interested.&lt;/p&gt;

&lt;p&gt;Personally I am &lt;em&gt;very&lt;/em&gt; interested in state machines. I believe they are the best way to write intelligible code that aligns to how we, as software creators, actually think about the solutions we are developing.&lt;/p&gt;

&lt;p&gt;The problem is, the programming languages we use don't have state as a first class entity. Instead they are, most typically, protected data records with privileged libraries attached - aka "object-oriented programming". If "state" is mentioned at all in these languages, it is described as the summation of the values of the data record being shielded from harm by the outside world by the language semantics.  &lt;/p&gt;

&lt;p&gt;Another contender is "functional programming" languages. Although &lt;a href="https://www.geeksforgeeks.org/functional-programming-paradigm/#:~:text=Functional%20programming%20is%20a%20programming,is%20%E2%80%9Chow%20to%20solve%E2%80%9D."&gt;theoretically joined at the hip with state machines&lt;/a&gt;, their mathematical purity of immutability often obscures the very practical and common purpose of most software - to change things.&lt;/p&gt;

&lt;p&gt;So if state machines are omnipresent in software but obscured by the languages we work with, how can we use them in our daily lives as developers?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Frame System Specification Language
&lt;/h2&gt;

&lt;p&gt;This is exactly the problem I encountered as a young developer in the 90's (ancient history I know) with the advent of the Unified Modeling Language (UML). A key part of the the UML specification was a state machine diagram type (more accurately the &lt;a href="https://www.sciencedirect.com/science/article/pii/0167642387900359"&gt;Statechart&lt;/a&gt; diagram). At first I struggled to understand how to faithfully implement system behavior defined in these model specifications. But, over time, I developed a coding pattern that was easy to understand (at least for me) and repeatable in all of the programming languages I was using.&lt;/p&gt;

&lt;p&gt;Eventually I developed a shorthand for expressing this pattern which I now call &lt;a href="https://frame-lang.org"&gt;Frame&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Frame?
&lt;/h2&gt;

&lt;p&gt;I describe Frame as a &lt;em&gt;textual markdown language for system design&lt;/em&gt;. The benefits of Frame are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It turns object-oriented classes into state machines in multiple languages (currently 6)&lt;/li&gt;
&lt;li&gt;It creates high-level UML documentation that gives the big picture of what the system does.&lt;/li&gt;
&lt;li&gt;It is a simple textual language (you don't have to draw anything) &lt;/li&gt;
&lt;li&gt;Its syntax is intentionally terse and symbolic for maximum information density and (hopefully) clarity&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So Frame makes it easy to create state machines - great. But why is the really useful? Glad you asked.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are State Machines good for?
&lt;/h2&gt;

&lt;p&gt;As stated before (yes I said it), state machines compartmentalize code in a way makes it easy to understand what the software is supposed to be doing at any point. This makes building complex software far more intuitive. For instance, when you are thinking about your house, you think about it as a set of joined spaces you traverse in a constrained way. &lt;/p&gt;

&lt;p&gt;That is - I can't simply go from the basement den to the upstairs bath. I first have to leave the den, go up the stairs, go down the hall, and then I can enter the bath. This is the very natural kind of structure state machines impose upon the organization of software and makes it much easier to 1) know how many compartments exist and 2) how they relate to each other.&lt;/p&gt;

&lt;p&gt;Ok, ok - I know you are now anxious to use state machines in all your upcoming projects. So show me some Frame!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let there be - Frame.
&lt;/h2&gt;

&lt;p&gt;You're finally old enough so lets shed some light on the subject:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#LampSpec

    -interface-

    toggle

    -machine-

    $Off
        |toggle|
            -&amp;gt; $On ^
    $On
        |&amp;gt;|
            turnOn() ^
        |&amp;lt;|
            turnOff() ^
        |toggle|
            -&amp;gt; $Off ^

    -actions-

    turnOn
    turnOff
##
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is - different. &lt;/p&gt;

&lt;p&gt;Yes it is and there is a lot to unpack, but that isn't going to happen in this article. &lt;em&gt;Fortunately&lt;/em&gt;, you can find lots of detailed information on Frame syntax at &lt;a href="https://frame-lang.org"&gt;https://frame-lang.org&lt;/a&gt;, but for fun right now you can play with a demo of the Frame lamp for hours (press Toggle over and over again):&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/frame-lang/embed/abpOjyN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here we can see how the UML documentation makes it easy to see the compartments at a glance. As there are only two (well, technically three but whose counting), the utility of the state machine for dealing with complexity is minimal. But as you add more states, and then add other system components that interact with them, the utility becomes highly valuable very quickly.&lt;/p&gt;

&lt;p&gt;To see the code behind the demo take a look at it in the &lt;a href="https://framepiler.frame-lang.org/example/aHR0cHM6Ly9naXN0LmdpdGh1Yi5jb20vZnJhbWUtbGFuZy8wNmViMjYxNWYxYzc2MjgzZTI5MDM5OTU3OWMzYjUzYw=="&gt;Framepiler&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this whirlwind introduction to &lt;a href="https://frame-lang.org"&gt;Frame&lt;/a&gt; has intrigued you to learn more. If so please consider joining the &lt;a href="https://www.reddit.com/r/statemachines/"&gt;State Machines&lt;/a&gt; Reddit and/or the &lt;a href="https://discord.com/invite/CfbU4QCbSD"&gt;Art of the State&lt;/a&gt; Discord. I would love to talk Frame with you!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
