<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>criterion | Hugo Ferreira</title>
    <link>https://hugo.ferreira.cc/tags/criterion/</link>
    <description>Recent content in Criterion by Hugo Ferreira</description>
    
    <image>
      
      <title>criterion | Hugo Ferreira</title>
      <url>https://hugo.ferreira.cc/hf-bw.jpg</url>
      <link>https://hugo.ferreira.cc/tags/criterion/</link>
      
    </image>
    <generator>Hugo</generator>
    <language>en-GB</language>
    
    <copyright>2000–2024 by Hugo Ferreira · CC BY 4.0</copyright>
    
    <lastBuildDate>Mon, 15 May 2023 22:16:15 +0000</lastBuildDate>
    <atom:link href="https://hugo.ferreira.cc/tags/criterion/index.xml" rel="self" type="application/rss+xml" />
    <item>
      
      <title>💭 Mental model for Booleans vs Enumerates</title>
      <link>https://hugo.ferreira.cc/booleans-vs-enums/</link>
      
      <pubDate>Mon, 15 May 2023 22:16:15 +0000</pubDate>
      <guid>https://hugo.ferreira.cc/booleans-vs-enums/</guid>
      <description>&lt;p&gt;In programming, &lt;a href=&#34;https://en.wikipedia.org/wiki/Boolean_data_type&#34;&gt;Booleans&lt;/a&gt; and &lt;a href=&#34;https://en.wikipedia.org/wiki/Enumerated_type&#34;&gt;Enumerates&lt;/a&gt; are common data types to represent the concept of “choice”.&lt;/p&gt;
&lt;p&gt;When there are several things to choose from, the Enumerate is the only one to support it, so that’s clear.&lt;/p&gt;
&lt;p&gt;But what about when you have two things to choose from? There might be some confusion between both data types since either of them supports handling binary choices.&lt;/p&gt;
&lt;p&gt;The confusion arrises in particular when you want to add the choice of a new behaviour, on top of an existing one. For example:&lt;/p&gt;</description>

      
        
        
        <enclosure url="https://hugo.ferreira.cc/booleans-vs-enums/switch-selector.jpg" length="38597" type="image/jpeg" />
      
      <content:encoded><![CDATA[<p>In programming, <a href="https://en.wikipedia.org/wiki/Boolean_data_type">Booleans</a> and <a href="https://en.wikipedia.org/wiki/Enumerated_type">Enumerates</a> are common data types to represent the concept of “choice”.</p>
<p>When there are several things to choose from, the Enumerate is the only one to support it, so that’s clear.</p>
<p>But what about when you have two things to choose from? There might be some confusion between both data types since either of them supports handling binary choices.</p>
<p>The confusion arrises in particular when you want to add the choice of a new behaviour, on top of an existing one. For example:</p>
<blockquote>
<p>We&rsquo;re torn between adding a <code>boolean</code> called <code>supportsBarMode</code> or having a more descriptive model with an <code>enum</code> that lists all the supported options <code>[FooMode, BarMode]</code>.</p>
<p>The <code>enum</code> solution feels a bit <a href="https://en.wikipedia.org/wiki/You_aren't_gonna_need_it">YAGNI</a> at the moment, but we&rsquo;re open to suggestions. What do you think?</p>
</blockquote>
<hr>
<p>Don’t tie yourself to whether you’ll need more options in the future or not. Instead, look at how you can <em>communicate intent</em> the best way possible.</p>
<p>The mental model for encoding these binary choices is to identify which of these scenarios we are in:</p>
<ul>
<li><strong>Switch something on/off</strong> — e.g. enabling or disabling an extra option</li>
<li><strong>Select mode of operation</strong> — e.g. choosing between ways of doing things</li>
</ul>
<h3 id="boolean--switch-something-onoff">Boolean = Switch something on/off</h3>
<p><img alt="red switch with a 1 on top, a 0 on the bottom, and turned to the 1 position" loading="lazy" src="/booleans-vs-enums/switch.jpg"></p>
<p>A Boolean fits the 1st scenario. A light switch is either on (have light) or off (don’t have light). When it’s off, there’s no “default alternative behaviour” associated with it: the thing we want to turn on just isn’t there.</p>
<h3 id="enumerate--select-mode-of-operation">Enumerate = Select mode of operation</h3>
<p><img alt="a coaxial selector with the input on the bottom, an A and B position of top, and the central nob set to the A position" loading="lazy" src="/booleans-vs-enums/selector.jpg"></p>
<p>An Enumerate fits the 2nd scenario. A coaxial selector is either on position A or on position B. It lets you choose between two options. There is a clear meaning associated with each position.</p>
<hr>
<p>In the example above, the Enumerate data type would be the right choice.</p>
<blockquote>
<p>… an <code>enum</code> that lists all the supported options <code>[FooMode, BarMode]</code></p>
</blockquote>
<p>Even if we never add other options to that Enumerate, it is not a case of <a href="https://en.wikipedia.org/wiki/You_aren't_gonna_need_it">YAGNI</a>. The Enumerate could have only 2 options forever that it would still communicate the correct intent.</p>
<hr>
<details>
<summary>Shared to…</summary>
<ul>
<li><a href="https://www.linkedin.com/posts/hugocf_mental-model-for-booleans-vs-enumerates-activity-7064254945008529411-hUeH">LinkedIn</a></li>
<li><a href="https://mastodon.online/@hugocf/110378984935193931">Mastodon</a></li>
<li><a href="https://hugocf.medium.com/mental-model-for-booleans-vs-enumerates-df70d917fb41">Medium</a></li>
<li><a href="https://twitter.com/hugocf/status/1658489697843433472?s=20">Twitter</a></li>
</ul>
<p>🔒 <em>(groups)</em></p>
<ul>
<li><a href="https://springernature.slack.com/archives/C01NT4ECRQ8/p1684150514547979?thread_ts=1650552510.015869&amp;cid=C01NT4ECRQ8">Springer Nature Slack</a></li>
<li><a href="https://t.me/c/1363309933/8809">Telegram</a></li>
</ul>
</details>
]]></content:encoded>
    </item>
  </channel>
</rss>
