<?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: Jeremy Fairbank</title>
    <description>The latest articles on DEV Community by Jeremy Fairbank (@elpapapollo).</description>
    <link>https://dev.to/elpapapollo</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%2F183540%2F13b5a6c5-6969-4624-930c-9c92a71af70c.jpeg</url>
      <title>DEV Community: Jeremy Fairbank</title>
      <link>https://dev.to/elpapapollo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elpapapollo"/>
    <language>en</language>
    <item>
      <title>Programming Elm Now in Print</title>
      <dc:creator>Jeremy Fairbank</dc:creator>
      <pubDate>Wed, 03 Jul 2019 16:59:41 +0000</pubDate>
      <link>https://dev.to/elpapapollo/programming-elm-now-in-print-290g</link>
      <guid>https://dev.to/elpapapollo/programming-elm-now-in-print-290g</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://programming-elm.com/blog/2019-07-03-programming-elm-now-in-print"&gt;programming-elm.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;After 2 years, 2 months, and 29 days of waking early, sacrificing weekends, and rewriting for Elm 0.19, I can proudly say that &lt;a href="https://programming-elm.com"&gt;&lt;em&gt;Programming Elm&lt;/em&gt;&lt;/a&gt; is now available in print! Writing this book presented a challenging, but rewarding journey that taught me a ton about writing and teaching.&lt;/p&gt;

&lt;p&gt;If you're a front-end developer tired of the JavaScript framework churn or want to build more resilient and maintainable applications, then you need to learn Elm. I have tailored &lt;a href="https://programming-elm.com"&gt;&lt;em&gt;Programming Elm&lt;/em&gt;&lt;/a&gt; for front-end developers new to Elm who want to quickly learn how to build maintainable applications with it. You'll start with basics such as Elm's syntax and creating functions and advance all the way to building a single-page application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Elm?
&lt;/h2&gt;

&lt;p&gt;More and more front-end developers are choosing Elm to build applications for benefits such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;No runtime exceptions in practice:&lt;/em&gt; Elm's compiler catches problems early to prevent exceptions at runtime for your users.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;No &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; errors:&lt;/em&gt; Elm offers more versatile types for representing &lt;code&gt;null&lt;/code&gt;. The compiler also ensures you handle all possible nulls in your application.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;No JavaScript fatigue:&lt;/em&gt; You don't have to choose and wire up different frameworks and libraries to build an application. Elm has a built-in framework for creating applications, the Elm Architecture.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Predictable code:&lt;/em&gt; All Elm code is free from side effects, so you can trust your functions to always produce the same result based on their arguments.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Immutable data types:&lt;/em&gt; You don't have to worry about your code or third-party code changing data unexpectedly and causing bugs. Your data will be consistent and safe.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Strong static types:&lt;/em&gt; Elm's compiler uses static types to ensure you call functions with the right types of arguments. You won't run into subtle type-coercion bugs.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Custom types:&lt;/em&gt; Elm's custom types let you create entirely new types for clearly modeling your business domain. Powerful pattern matching prevents undefined situations by ensuring you handle your custom types consistently.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Advanced tools:&lt;/em&gt; Elm's &lt;code&gt;Debug&lt;/code&gt; module makes it easy to inspect data to catch bugs, and add placeholders to your code until you're ready to implement it. Third-party tools such as create-elm-app let you quickly bootstrap Elm applications and offer powerful development servers for immediate development feedback.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's in the Book?
&lt;/h2&gt;

&lt;p&gt;The first five chapters of this book focus on how to build applications. You will create a photo sharing application called Picshare and add new functionality in each chapter.&lt;/p&gt;

&lt;p&gt;Chapter 1 introduces you to Elm, explains some of the basics of functional programming, and lets you create a basic Picshare application.&lt;/p&gt;

&lt;p&gt;Chapter 2 explains Elm's framework for building applications, the Elm Architecture. You'll use the Elm Architecture to manage state and events in the Picshare application.&lt;/p&gt;

&lt;p&gt;Chapter 3 expands on the Picshare application.  You'll learn patterns for refactoring code and how to add new features to the Picshare application.&lt;/p&gt;

&lt;p&gt;Chapter 4 lets you create a more realistic Picshare application. Front-end applications typically need to communicate with servers to be useful. You'll learn how to call APIs and safely decode JSON into static types.&lt;/p&gt;

&lt;p&gt;Chapter 5 takes Picshare's interactivity further. You'll use Elm subscriptions with WebSockets to receive updates in real time.&lt;/p&gt;

&lt;p&gt;The next six chapters focus on advanced patterns for scaling, debugging,&lt;br&gt;
integrating, and maintaining Elm applications.&lt;/p&gt;

&lt;p&gt;Chapter 6 addresses the problem of scaling complex applications containing lots of code. You'll use patterns such as reusable helper functions, extensible records, and message wrappers to refactor an application into a more maintainable state.&lt;/p&gt;

&lt;p&gt;Chapter 7 introduces Elm's tooling. Although Elm's compiler prevents tons of bugs through static types, bugs can still occur from logic errors. You'll use Elm's &lt;code&gt;Debug&lt;/code&gt; module to debug values at runtime. You'll also bundle and deploy an application with powerful third-party tools.&lt;/p&gt;

&lt;p&gt;Chapter 8 covers interacting with JavaScript code, which is important for accessing impure APIs or migrating existing JavaScript applications to Elm. You'll learn how to add a new feature with Elm to an existing JavaScript application.&lt;/p&gt;

&lt;p&gt;Chapter 9 introduces testing to ensure your code is correct. You'll use elm-test to create a module with test-driven development, test properties of your code with fuzz testing, and test an Elm application with elm-html-test.&lt;/p&gt;

&lt;p&gt;Chapter 10 teaches you how to build modern single-page applications with Elm. You'll learn how to handle routes and coordinate different page components.&lt;/p&gt;

&lt;p&gt;Chapter 11 concludes with speeding up your code. You'll learn about common performance issues, how to measure performance, and how to optimize applications with efficient algorithms, lazy design patterns, and the &lt;code&gt;Html.Lazy&lt;/code&gt; module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Your Copy
&lt;/h2&gt;

&lt;p&gt;So, what are you waiting for? Say goodbye to runtime exceptions and unmaintainable code. Learn how to build safe and maintainable front-end applications. Grab a copy of &lt;a href="https://programming-elm.com"&gt;&lt;em&gt;Programming Elm&lt;/em&gt;&lt;/a&gt; from &lt;a href="https://pragprog.com/book/jfelm/programming-elm"&gt;The Pragmatic Programmers&lt;/a&gt; today.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>functional</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Solving the Boolean Identity Crisis: Part 3</title>
      <dc:creator>Jeremy Fairbank</dc:creator>
      <pubDate>Sat, 22 Jun 2019 15:00:42 +0000</pubDate>
      <link>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-3-2f8</link>
      <guid>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-3-2f8</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://programming-elm.com/blog/2019-06-17-solving-the-boolean-identity-crisis-part-3"&gt;programming-elm.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;In the &lt;a href="https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-2-12k"&gt;last post&lt;/a&gt;, you learned how boolean return values cause boolean blindness. Boolean blindness can create bugs in conditional statements by letting code access data that shouldn't be available. We swapped out boolean return values with &lt;code&gt;Maybe&lt;/code&gt; and a custom type to wrap the data in a particular type constructor and provide more meaningful return values. This let the compiler ensure code only accessed data when it was truly available.&lt;/p&gt;

&lt;p&gt;In this post, you will see that boolean properties in Elm records open the door to invalid state configurations. Boolean properties require more complex conditional code and more tests to prevent bugs. You will learn that custom types—seeing a pattern here—eliminate the need for complex code and tests by harnessing the power of the compiler to prevent invalid state configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;I begin my talk &lt;a href="https://www.youtube.com/watch?v=8Af1bh-BVY8"&gt;Solving the Boolean Identity Crisis&lt;/a&gt; with a problem I encountered while building applications with &lt;a href="https://redux.js.org"&gt;Redux&lt;/a&gt; and &lt;a href="https://reactjs.org"&gt;React&lt;/a&gt;.  When fetching data from a server, I would track the state of fetching that data with multiple boolean properties. Unfortunately, I brought that pattern over to the Elm applications I built.&lt;/p&gt;

&lt;p&gt;For example, let's say we're building a application for tracking rescue dogs. We need to fetch a dog from the server. Initially, we wouldn't have a dog, so we would likely have a model like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That seems reasonable so far. Next, we want to display a loading spinner while we fetch the dog from a server. So, we could add a &lt;code&gt;fetching&lt;/code&gt; property to the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fetching&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;fetching&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, we will display the spinner. When &lt;code&gt;fetching&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, we will display nothing.&lt;/p&gt;

&lt;p&gt;Once we have the dog, fetching should be &lt;code&gt;False&lt;/code&gt;, but we want to display the dog. We could add a &lt;code&gt;success&lt;/code&gt; boolean property to indicate we have the dog.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fetching&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, if success is &lt;code&gt;True&lt;/code&gt;, we display the dog. Otherwise, if it's &lt;code&gt;False&lt;/code&gt; and fetching is &lt;code&gt;False&lt;/code&gt;, then we're back in a "ready to fetch" state and should display nothing. (Alternatively, we could look at a combination of &lt;code&gt;fetching&lt;/code&gt; and if &lt;code&gt;dog&lt;/code&gt; is &lt;code&gt;Just&lt;/code&gt; or &lt;code&gt;Nothing&lt;/code&gt; to decide what state we're in.)&lt;/p&gt;

&lt;p&gt;All seems well, but the dog could not exist on the server or we could encounter other server errors. We need to know if the request failed and handle any errors appropriately. Well, we could add an &lt;code&gt;error&lt;/code&gt; boolean property along with an &lt;code&gt;errorMessage&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fetching&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If error is &lt;code&gt;True&lt;/code&gt;, then we can display the &lt;code&gt;errorMessage&lt;/code&gt;. Otherwise, we'll need to examine the other boolean properties to determine what to do.&lt;/p&gt;

&lt;p&gt;If we were to handle this in the &lt;code&gt;view&lt;/code&gt; function, it might look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;viewError&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorMessage&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetching&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;viewSpinner&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;viewDog&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;viewSearchForm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;view&lt;/code&gt; function has a couple of issues.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It suffers from &lt;a href="https://dev.to/blog/2019-05-30-solving-the-boolean-identity-crisis-part-2"&gt;boolean blindness&lt;/a&gt;.  We depend on certain boolean properties to be true before attempting to access data. Nothing stops us from accessing data in other branches such as &lt;code&gt;model.dog&lt;/code&gt; or &lt;code&gt;model.errorMessage&lt;/code&gt;. (Granted, if we tried to access &lt;code&gt;model.dog&lt;/code&gt;, we'd still have the safety of &lt;code&gt;Maybe&lt;/code&gt;.)&lt;/li&gt;
&lt;li&gt;It requires more thorough automated testing to ensure we handle all cases properly. We could leave out all the &lt;code&gt;else if&lt;/code&gt; branches and the code would still compile even if it was incorrect.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also, our model can arrive at incorrect configurations like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Just&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tucker"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fetching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt;
&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt;
&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt;
&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uh oh!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All boolean properties are true, we have a dog, and we have an &lt;code&gt;errorMessage&lt;/code&gt;.  We're hard-pressed to determine what state we're really in. We have no choice but to depend on the arbitrary ordering of the if-else conditionals in &lt;code&gt;view&lt;/code&gt; to make that decision. Of course, we'll need a strong test suite to ensure we can't configure the model like this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prevent Invalid State
&lt;/h2&gt;

&lt;p&gt;I finally realized the problem with how I represented my data. I thought the states of fetching data (ready, fetching, success, and error) were separate from one another. Really, they are different &lt;em&gt;state values&lt;/em&gt; of the same overall state. That sounds like a &lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine"&gt;state machine&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A state machine can only be in one state value at a time. My record representation forbid that by letting multiple state values be &lt;code&gt;True&lt;/code&gt;. Elm has an awesome type system and compiler. We should leverage them as much as possible to prevent invalid state configurations by essentially creating a state machine. &lt;/p&gt;

&lt;p&gt;We could introduce a new custom type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;RemoteDoggo&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Ready&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Fetching&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Success&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;RemoteDoggo&lt;/code&gt; type has four constructors that map to each possible state, &lt;code&gt;Ready&lt;/code&gt;, &lt;code&gt;Fetching&lt;/code&gt;, &lt;code&gt;Success&lt;/code&gt;, and &lt;code&gt;Error&lt;/code&gt;. We wrap a &lt;code&gt;Dog&lt;/code&gt; with the &lt;code&gt;Success&lt;/code&gt; constructor and wrap a &lt;code&gt;String&lt;/code&gt; error message with the &lt;code&gt;Error&lt;/code&gt; constructor.  Then, we can update the model to look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;RemoteDoggo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We remove all but the &lt;code&gt;dog&lt;/code&gt; property and change the &lt;code&gt;dog&lt;/code&gt; property to the &lt;code&gt;RemoteDoggo&lt;/code&gt; type. We can now transform the &lt;code&gt;view&lt;/code&gt; function into this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Ready&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;viewSearchForm&lt;/span&gt;

        &lt;span class="kt"&gt;Fetching&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;viewSpinner&lt;/span&gt;

        &lt;span class="kt"&gt;Success&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;viewDog&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;

        &lt;span class="kt"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;viewError&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of worrying about the order of boolean properties, we pattern match on the &lt;code&gt;dog&lt;/code&gt; property with a &lt;code&gt;case&lt;/code&gt; expression. We map each constructor to the appropriate view helper function.&lt;/p&gt;

&lt;p&gt;Our code has gained a few benefits here by this change.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code makes the states explicit with the &lt;code&gt;RemoteDoggo&lt;/code&gt; type.&lt;/li&gt;
&lt;li&gt;We eliminated boolean blindness. We can only access the dog in &lt;code&gt;Success&lt;/code&gt; and the error message in &lt;code&gt;Error&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We have compiler enforced UI states. If we forget to handle one of the &lt;code&gt;RemoteDoggo&lt;/code&gt; values, then our code won't compile.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
     &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
         &lt;span class="kt"&gt;Ready&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
             &lt;span class="n"&gt;viewSearchForm&lt;/span&gt;

         &lt;span class="kt"&gt;Fetching&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
             &lt;span class="n"&gt;viewSpinner&lt;/span&gt;

         &lt;span class="kt"&gt;Success&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
             &lt;span class="n"&gt;viewDog&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;

         &lt;span class="c1"&gt;-- forgetting to handle errors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Will produce this compiler error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; This `case` does not have branches for all possibilities:

 |&amp;gt;    case model.dog of
 |&amp;gt;        Ready -&amp;gt;
 |&amp;gt;            viewSearchForm
 |&amp;gt;
 |&amp;gt;        Fetching -&amp;gt;
 |&amp;gt;            viewSpinner
 |&amp;gt;
 |&amp;gt;        Success dog -&amp;gt;
 |&amp;gt;            viewDog dog

 Missing possibilities include:

     Error _
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now our code is clearer and safer thanks to custom types.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Learned
&lt;/h2&gt;

&lt;p&gt;In this post, you learned that boolean properties can cause invalid state configurations, which create bugs that the compiler can't catch. Boolean properties lead to complex if-else expressions with arbitrary ordering that are hard to follow. You saw that by reducing the boolean properties down to one property with a custom type you can write more explicit code with compiler safety. If you have some code with multiple boolean properties like this, try refactoring to a custom type to make your code clearer and safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Resources
&lt;/h2&gt;

&lt;p&gt;For more info on how to use Elm's type system to prevent invalid state configurations, watch Richard Feldman's talk &lt;a href="https://www.youtube.com/watch?v=IcgmSRJHu_8"&gt;Making Impossible States Impossible&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For a more general type similar to &lt;code&gt;RemoteDoggo&lt;/code&gt;, check out the &lt;a href="https://package.elm-lang.org/packages/krisajenkins/remotedata/latest"&gt;krisajenkins/remotedata&lt;/a&gt; package.&lt;/p&gt;

&lt;p&gt;To learn more about how to build Elm applications effectively, grab a copy of my book &lt;a href="https://programming-elm.com"&gt;Programming Elm&lt;/a&gt; from The &lt;a href="https://pragprog.com/book/jfelm/programming-elm"&gt;Pragmatic Programmers&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>functional</category>
      <category>webdev</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Solving the Boolean Identity Crisis: Part 2</title>
      <dc:creator>Jeremy Fairbank</dc:creator>
      <pubDate>Fri, 21 Jun 2019 18:43:55 +0000</pubDate>
      <link>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-2-12k</link>
      <guid>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-2-12k</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://programming-elm.com/blog/2019-05-30-solving-the-boolean-identity-crisis-part-2"&gt;programming-elm.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;In the &lt;a href="https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-1-54bp"&gt;previous post&lt;/a&gt;, we explored how boolean arguments obscure the intent of code. We replaced boolean arguments with custom type values to make code more explicit and maintainable.&lt;/p&gt;

&lt;p&gt;In this post, you will discover that boolean return values cause a problem known as &lt;em&gt;boolean blindness&lt;/em&gt;. Boolean blindness can create accidental bugs in if-else expressions that the compiler can't prevent. You will learn how to replace boolean return values with custom types to eliminate boolean blindness and leverage the compiler for safer code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;In my talk, &lt;a href="https://www.youtube.com/watch?v=8Af1bh-BVY8"&gt;Solving the Boolean Identity Crisis&lt;/a&gt;, I share a tale from a &lt;a href="https://www.cs.cmu.edu/~15150/previous-semesters/2012-spring/resources/lectures/09.pdf"&gt;lecture&lt;/a&gt; by &lt;a href="http://dlicata.web.wesleyan.edu"&gt;Dan Licata&lt;/a&gt;, a professor at Wesleyan University.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sometimes, when I'm walking down the street, someone will ask me "do you know what time it is?" If I feel like being a literalist, I'll say "yes." Then they roll their eyes and say "okay, [tell] me what time it is!" The downside of this is that they might get used to demanding the time, and start demanding it of people who don't even know it.  It's better to ask "do you know what time is it, and if so, please tell me?". [T]hat's what "what time is it?" usually means. This way, you get the information you were after, when it's available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we translate this into code, it might look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;doYouKnowTheTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;
&lt;span class="n"&gt;doYouKnowTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;


&lt;span class="n"&gt;tellMeTheTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;tellMeTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;


&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;doYouKnowTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;tellMeTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Does anybody really know what time it is?"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;doYouKnowTheTime&lt;/code&gt; function accepts a &lt;code&gt;Person&lt;/code&gt; type and checks if the &lt;code&gt;time&lt;/code&gt; field isn't the empty string. Then, we branch on a call to &lt;code&gt;doYouKnowTheTime&lt;/code&gt; inside the &lt;code&gt;currentTime&lt;/code&gt; function. If it returns &lt;code&gt;True&lt;/code&gt;, then we call &lt;code&gt;tellMeTheTime&lt;/code&gt; to return the value of &lt;code&gt;person.time&lt;/code&gt;. Otherwise, we return a default time.&lt;/p&gt;

&lt;p&gt;This code may look fine but it suffers from a couple of problems.&lt;/p&gt;

&lt;p&gt;First, as Dan rightly points out, people could demand time of others that don't have it. Nothing stops us from writing this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;doYouKnowTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;tellMeTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;tellMeTheTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="c1"&gt;-- returns empty string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can still call &lt;code&gt;tellMeTheTime&lt;/code&gt; when &lt;code&gt;person.time&lt;/code&gt; is the empty string. This would likely cause a bug.&lt;/p&gt;

&lt;p&gt;Second, the fact that we can cause the previous situation surfaces a data-modeling code smell. Strings notoriously cause trouble because any string is valid according to the type system. The compiler can't enforce that a given string is not empty. This is a weak substitute for a more meaningful data type.&lt;/p&gt;

&lt;p&gt;We want to give the compiler better type information so it can constrain this code to only access the time when it's truly available. Let's explore how to make this code clearer and safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix the Boolean Blindness
&lt;/h2&gt;

&lt;p&gt;The first problem stems from boolean blindness. When you reduce information to a boolean, you lose that information easily. The information that boolean carries is only known inside the &lt;code&gt;if&lt;/code&gt; check. As soon as you branch into the body of the if-else expression, you become &lt;em&gt;blind&lt;/em&gt; to the original information that got you there. Because that boolean loses information, you must backtrack to recover it when you need it again.&lt;/p&gt;

&lt;p&gt;Dan offers this solution to boolean blindness, "boolean tests let you &lt;em&gt;look&lt;/em&gt;, options let you &lt;em&gt;see&lt;/em&gt;."&lt;/p&gt;

&lt;p&gt;Dan is referring to the &lt;code&gt;option&lt;/code&gt; type in &lt;a href="https://en.wikipedia.org/wiki/ML_%28programming_language%29"&gt;ML&lt;/a&gt;. In Elm, we call it the &lt;code&gt;Maybe&lt;/code&gt; type. What Dan means is that booleans only tell you if something is present. The &lt;code&gt;Maybe&lt;/code&gt; type tells you if it's present by giving it to you when it's available. Let's rewrite our example with &lt;code&gt;Maybe String&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;


&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Just&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;

        &lt;span class="kt"&gt;Nothing&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Does anybody really know what time it is?"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We update the &lt;code&gt;time&lt;/code&gt; field to be &lt;code&gt;Maybe String&lt;/code&gt;. Then, we add a &lt;code&gt;whatTimeIsIt&lt;/code&gt; function that returns &lt;code&gt;person.time&lt;/code&gt;. Inside &lt;code&gt;currentTime&lt;/code&gt; we now call &lt;code&gt;whatTimeIsIt&lt;/code&gt; and pattern match on the result. If the person has the time, then we immediately have access to it inside &lt;code&gt;Just&lt;/code&gt;. No need to first check with an if-else expression. If the person doesn't have the time, i.e. &lt;code&gt;Nothing&lt;/code&gt;, then we return our default. &lt;/p&gt;

&lt;p&gt;We can't accidentally access the time if it's not present because the compiler will enforce the &lt;code&gt;Maybe&lt;/code&gt; type constraint.&lt;/p&gt;

&lt;p&gt;We still have a problem, though. The time inside &lt;code&gt;Just&lt;/code&gt; could be the empty string, which is an invalid time. Let's fix that next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Time.Posix
&lt;/h2&gt;

&lt;p&gt;We need a better type for encoding the time to avoid the empty string. Luckily, Elm has a package for working with time called &lt;a href="https://package.elm-lang.org/packages/elm/time/latest/"&gt;elm/time&lt;/a&gt;. It offers a &lt;code&gt;Posix&lt;/code&gt; type to represent Unix time, or the amount of time that has passed since midnight UTC on January 1, 1970. We can use the &lt;code&gt;Posix&lt;/code&gt; type and then convert it to a formatted time when needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Time&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Posix&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toHour&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toMinute&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Posix&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Posix&lt;/span&gt;
&lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;


&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Just&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromInt&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toHour&lt;/span&gt; &lt;span class="n"&gt;utc&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;:"&lt;/span&gt;
                &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromInt&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toMinute&lt;/span&gt; &lt;span class="n"&gt;utc&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kt"&gt;Nothing&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Does anybody really know what time it is?"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We import the &lt;code&gt;Time&lt;/code&gt; module and expose &lt;code&gt;Posix&lt;/code&gt;, &lt;code&gt;toHour&lt;/code&gt;, &lt;code&gt;toMinute&lt;/code&gt;, and &lt;code&gt;utc&lt;/code&gt;.  We change the &lt;code&gt;time&lt;/code&gt; field to &lt;code&gt;Maybe Posix&lt;/code&gt; and update the type annotation for &lt;code&gt;whatTimeIsIt&lt;/code&gt;. Inside the &lt;code&gt;Just&lt;/code&gt; branch of &lt;code&gt;currentTime&lt;/code&gt;, we now know we have a valid time thanks to the &lt;code&gt;Posix&lt;/code&gt; type. We use the &lt;code&gt;toHour&lt;/code&gt; and &lt;code&gt;toMinute&lt;/code&gt; functions along with &lt;code&gt;String.fromInt&lt;/code&gt; and the &lt;code&gt;utc&lt;/code&gt; time zone to build a formatted string time.&lt;/p&gt;

&lt;p&gt;This is great. Because of static types, the compiler will enforce our code to only access a valid time when it exists.&lt;/p&gt;

&lt;p&gt;We could go one step further to improve this code. If a person doesn't have the time, then it's &lt;code&gt;Nothing&lt;/code&gt;. But, that doesn't explain &lt;em&gt;why&lt;/em&gt; the person doesn't have time. We can replace &lt;code&gt;Maybe&lt;/code&gt; with our own custom type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;CurrentTime&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;CurrentTime&lt;/span&gt; &lt;span class="kt"&gt;Posix&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;NoWatch&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;InAHurry&lt;/span&gt;


&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CurrentTime&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;currentTime&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;whatTimeIsIt&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;CurrentTime&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromInt&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toHour&lt;/span&gt; &lt;span class="n"&gt;utc&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;:"&lt;/span&gt;
                &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromInt&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toMinute&lt;/span&gt; &lt;span class="n"&gt;utc&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kt"&gt;NoWatch&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I don't have the time."&lt;/span&gt;

        &lt;span class="kt"&gt;InAHurry&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sorry, I'm in a hurry."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We introduce a &lt;code&gt;CurrentTime&lt;/code&gt; custom type with three constructors, &lt;code&gt;CurrentTime&lt;/code&gt;, &lt;code&gt;NoWatch&lt;/code&gt;, and &lt;code&gt;InAHurry&lt;/code&gt;. The &lt;code&gt;CurrentTime&lt;/code&gt; constructor wraps &lt;code&gt;Posix&lt;/code&gt;. We then change the &lt;code&gt;time&lt;/code&gt; field to be &lt;code&gt;CurrentTime&lt;/code&gt;. In the &lt;code&gt;currentTime&lt;/code&gt; function, we handle all three constructors. The &lt;code&gt;CurrentTime&lt;/code&gt; branch stays the same as the previous &lt;code&gt;Just&lt;/code&gt; branch. The &lt;code&gt;NoWatch&lt;/code&gt; and &lt;code&gt;InAHurry&lt;/code&gt; branches each return a string that describes why the person doesn't have the time.&lt;/p&gt;

&lt;p&gt;Now, we have made the code more precise about why a person doesn't have the time and have encoded better business domain rules into the code with custom types.  Plus, we still have the compiler to ensure we can only access a valid time in the &lt;code&gt;CurrentTime&lt;/code&gt; branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Learned
&lt;/h2&gt;

&lt;p&gt;In this post, you learned that boolean return values cause boolean blindness.  You saw that boolean blindness can lead to human error by letting code access data in incorrect places. You discovered that built-in custom types such as &lt;code&gt;Maybe&lt;/code&gt; or your own custom type let you test and access the presence of data.  Additionally, the compiler ensures you access data only when it's truly available. Try refactoring some of your own code to replace a boolean return value with a more meaningful custom type to make your code more maintainable.&lt;/p&gt;

&lt;p&gt;To learn more about how to build Elm applications effectively, grab a copy of my book &lt;a href="https://programming-elm.com"&gt;Programming Elm&lt;/a&gt; from The &lt;a href="https://pragprog.com/book/jfelm/programming-elm"&gt;Pragmatic Programmers&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>functional</category>
      <category>webdev</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Solving the Boolean Identity Crisis: Part 1</title>
      <dc:creator>Jeremy Fairbank</dc:creator>
      <pubDate>Thu, 20 Jun 2019 19:22:47 +0000</pubDate>
      <link>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-1-54bp</link>
      <guid>https://dev.to/elpapapollo/solving-the-boolean-identity-crisis-part-1-54bp</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://programming-elm.com/blog/2019-05-20-solving-the-boolean-identity-crisis-part-1"&gt;programming-elm.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;Back in September 2017, I presented the talk "Solving the Boolean Identity Crisis" at ElmConf. The talk highlights the downsides of using booleans in Elm code and offers ways to write clearer, more maintainable code. This post and the next couple of posts will share what I explored in that presentation. You can preview what's to come by watching my talk on &lt;a href="https://www.youtube.com/watch?v=8Af1bh-BVY8"&gt;YouTube&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post, you will see how boolean function arguments obscure the intent of code. Then, you will learn how to replace boolean arguments with Elm's custom types to write more understandable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Look at this function call to understand the problem with boolean arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;OGG"&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We pass in a string argument &lt;code&gt;"OGG"&lt;/code&gt; and a boolean argument &lt;code&gt;True&lt;/code&gt; to a &lt;code&gt;bookFlight&lt;/code&gt; function. If you encountered this in an Elm codebase, you might wonder what the boolean argument does.&lt;/p&gt;

&lt;p&gt;Boolean arguments hide the intent of code. We don't know the significance of the &lt;code&gt;True&lt;/code&gt; value here without looking up the definition of &lt;code&gt;bookFlight&lt;/code&gt;. The boolean argument makes this code harder to understand, especially as a newcomer learning the codebase.&lt;/p&gt;

&lt;p&gt;Looking up the definition, we find this. (I use &lt;code&gt;...&lt;/code&gt; to signify irrelevant code.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="n"&gt;airport&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The boolean argument is called &lt;code&gt;isPremium&lt;/code&gt;, so it means the booking customer has a premium status. We use an if-else expression to branch on &lt;code&gt;isPremium&lt;/code&gt;. If &lt;code&gt;isPremium&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, we're not certain what status this customer has. We have to assume that the customer has a "regular" status because the code makes that implicit. We've lost the explicit intent of this code by using a boolean argument.&lt;/p&gt;

&lt;p&gt;This code will present future problems if we need more than one customer status.  For example, let's say we need to introduce a new economy status. We could introduce another boolean argument called &lt;code&gt;isRegular&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="n"&gt;airport&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="n"&gt;isRegular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isRegular&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After checking if &lt;code&gt;isPremium&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, we check if &lt;code&gt;isRegular&lt;/code&gt; &lt;code&gt;True&lt;/code&gt;.  Otherwise, the implicit customer status is economy.&lt;/p&gt;

&lt;p&gt;Now, function calls will look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;OGG"&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt; &lt;span class="kt"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's even more confusing. We could easily mix up the order of the boolean arguments too and accidentally book a customer with the wrong status. Also, we could easily pass in two &lt;code&gt;True&lt;/code&gt; arguments. A customer can't have both premium and regular status. We have to let the first boolean argument &lt;code&gt;isPremium&lt;/code&gt; take precedence in the if-else expression to deal with this invalid argument permutation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Show Intent
&lt;/h2&gt;

&lt;p&gt;We can clean up the &lt;code&gt;bookFlight&lt;/code&gt; function by replacing the boolean arguments with an Elm custom type. Instead of hiding statuses behind boolean values, let's make them explicit. We can easily encode each type of status like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;CustomerStatus&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Premium&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Regular&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Economy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We add a &lt;code&gt;CustomerStatus&lt;/code&gt; custom type with three values, or constructors. Each value perfectly encodes each status, &lt;code&gt;Premium&lt;/code&gt;, &lt;code&gt;Regular&lt;/code&gt;, and &lt;code&gt;Economy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can update the &lt;code&gt;bookFlight&lt;/code&gt; function like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;CustomerStatus&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="n"&gt;airport&lt;/span&gt; &lt;span class="n"&gt;customerStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;customerStatus&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Premium&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;

        &lt;span class="kt"&gt;Regular&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;

        &lt;span class="kt"&gt;Economy&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;bookFlight&lt;/code&gt; function makes it clear how to handle each customer status without implicit if-else branching. Additionally, the compiler ensures we handle each status. In the previous version of &lt;code&gt;bookFlight&lt;/code&gt; with two boolean arguments, nothing would prevent us from accidentally forgetting to handle the &lt;code&gt;else if isRegular&lt;/code&gt; branch. The compiler would accept this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="n"&gt;airport&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="n"&gt;isRegular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isPremium&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we forgot the &lt;code&gt;Regular&lt;/code&gt; branch in the version with the &lt;code&gt;CustomerStatus&lt;/code&gt; type, the code would not compile.&lt;/p&gt;

&lt;p&gt;This code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="n"&gt;airport&lt;/span&gt; &lt;span class="n"&gt;customerStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;customerStatus&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Premium&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;

        &lt;span class="kt"&gt;Economy&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Would result in this compiler error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This `case` does not have branches for all possibilities:

|&amp;gt;    case customerStatus of
|&amp;gt;        Premium -&amp;gt;
|&amp;gt;            ...
|&amp;gt;
|&amp;gt;        Economy -&amp;gt;
|&amp;gt;            ...

Missing possibilities include:

    Regular
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Custom types provide compiler safety along with explicit code. Now calls to &lt;code&gt;bookFlight&lt;/code&gt; declare the intent of code because we pass in the &lt;code&gt;CustomerStatus&lt;/code&gt; directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;bookFlight&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;OGG"&lt;/span&gt; &lt;span class="kt"&gt;Premium&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we ran into the above function, we would more easily understand what's happening. We're booking a flight for a premium customer. We've made the code clearer and more maintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Learned
&lt;/h2&gt;

&lt;p&gt;In this post, you learned how boolean arguments can make code confusing and unmaintainable by hiding the intent of code. You saw how replacing boolean arguments with custom type values created better, safer code. Try this technique out on your own Elm project. Find a function that accepts a boolean argument and see if you can make a custom type that more explicitly encodes the meaning of that boolean argument when it's &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To learn more about how to build Elm applications effectively, grab a copy of my book &lt;a href="https://programming-elm.com"&gt;Programming Elm&lt;/a&gt; from The &lt;a href="https://pragprog.com/book/jfelm/programming-elm"&gt;Pragmatic Programmers&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>functional</category>
      <category>webdev</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
