<?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>Forem: Brenden Ehlers</title>
    <description>The latest articles on Forem by Brenden Ehlers (@behlers).</description>
    <link>https://forem.com/behlers</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%2F1424436%2F7641de50-fe38-4749-aa02-28b13a5a847d.jpg</url>
      <title>Forem: Brenden Ehlers</title>
      <link>https://forem.com/behlers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/behlers"/>
    <language>en</language>
    <item>
      <title>You shouldn't use else statements</title>
      <dc:creator>Brenden Ehlers</dc:creator>
      <pubDate>Sun, 14 Apr 2024 16:15:30 +0000</pubDate>
      <link>https://forem.com/behlers/you-shouldnt-use-else-statements-33h7</link>
      <guid>https://forem.com/behlers/you-shouldnt-use-else-statements-33h7</guid>
      <description>&lt;p&gt;…most of the time. Let me explain.&lt;/p&gt;

&lt;p&gt;If you studied computer science, you're well aware of the if/else control flow. It was drilled so far into my head for all four years of university it feels like second nature. But, after working as a software engineer, I realized that using else is often a sign of poorly-written code.&lt;/p&gt;

&lt;p&gt;Consider this simple function that creates a user record in a database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsConnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// create the user&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"database not connected"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're not familiar with Go, all this function is doing is creating the user if the database is connected, and raising an error if not. You can ignore the syntactic sugar of receiver functions, pointers, and the error return type as these are specific to Go. The ideas are the same as your preferred language of choice.&lt;/p&gt;

&lt;p&gt;Have you noticed the issue with coding like this?&lt;/p&gt;

&lt;p&gt;It's not obvious in this example, but the problem arises if there are more conditionals, e.g. if you need to check that the user's email is unique. With each new condition, you add another layer of indentation to the core logic. Eventually, it becomes hard to figure out what condition is attached to what error or return, and how the code actually flows.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;guard clauses&lt;/strong&gt; save the day.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a guard clause?
&lt;/h2&gt;

&lt;p&gt;A guard clause is made up of two parts, the guard condition and the guard handler. The guard condition is tested when the function runs. If it evaluates to true, then the execution moves into the guard handler. Otherwise, the execution jumps over the handler and moves on to the rest of the function.&lt;/p&gt;

&lt;p&gt;The guard handler does two tasks: documenting what went wrong and stoping the execution of the function. In most languages, particularly ones with exceptions, these can be done in the same step. Go handles it similarly, by returning an error that includes the context of what went wrong.&lt;/p&gt;

&lt;p&gt;After the guard clause you know the condition must have been true, because otherwise the function execution would've been stopped. This frees you up to perform whatever logic is required in the top-level of the function, instead of within the body of an if statement.&lt;/p&gt;

&lt;p&gt;Consider the function above. How would you refactor it to use a guard clause? &lt;/p&gt;

&lt;p&gt;Here's how I did it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsConnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"database not connected"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c"&gt;// create the user&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first if statement is the guard clause, it'll always return an error when the database isn't connected. After the execution continues past the clause, we know that &lt;code&gt;db.IsConnected()&lt;/code&gt; must be true. Therefore, can implement our core logic without having to check the connectivity status every time we use the database.&lt;/p&gt;

&lt;p&gt;Some other added benefits of using a guard clause are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The core logic is on the initial indentation level.&lt;/li&gt;
&lt;li&gt;It establishes a contract of properties that must be met before the core logic is executed.&lt;/li&gt;
&lt;li&gt;Adding new conditions is easier and cleaner in git diffs, leading to code that is easier to read and review.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We've already looked at the first point, so let's take a deeper look at the last two points.&lt;/p&gt;

&lt;h2&gt;
  
  
  Establishes a Contract
&lt;/h2&gt;

&lt;p&gt;When you want to use a function, how do you know what conditions must be met before it'll work? Simple answer, you don't. The longer answer is the code author should try to help you out. For example, they may document the requirements in a comment above the function signature. But if you've ever worked on a large project, you know that documentation has a tendency to get out of sync with the implementation.&lt;/p&gt;

&lt;p&gt;Guard clauses help with this by including the requirements within the code itself, which means the they will never get out of sync. It allows future developers to understand what a function needs to work at a glance, without the tedious back and forth of referencing documentation and making sure the documentation has been updated in sync with the code.&lt;/p&gt;

&lt;p&gt;If you've ever heard the term &lt;a href="https://www.cs.unc.edu/~stotts/COMP145/CRC/DesByContract.html"&gt;Design by Contract&lt;/a&gt;, this is very similar to that idea. Given a specific set of inputs, that are checked by the guard clauses when the function runs, and assuming there are no side effects, you ensure a valid and accurate output. Working this way allows future developers to know how they need to implement their features to be in compliance with the functions they want to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding New Conditions
&lt;/h2&gt;

&lt;p&gt;Imagine you're adding another condition to the function. How would you do it in the original function (without the guard clause) vs. in the guarded function?&lt;br&gt;
 &lt;br&gt;
In the unguarded function, you'd have to add another, nested, if/else statement within the original conditional, which adds another level of indentation and obfuscates the intent of the function. Take a look at the git diff below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; func (db *DB) createUser(user *User) error {
   if db.IsConnected() {
&lt;span class="gd"&gt;-    // create the user
-    return nil
&lt;/span&gt;&lt;span class="gi"&gt;+    if !db.UserExists(user) {
+      // create the user
+      return nil
+    } else {
+      return fmt.Errorf("user already exists")
+    }
&lt;/span&gt;   } else {
     return fmt.Errorf("database not connected")
   }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare this to the when we use guard clauses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; func (db *DB) createUser(user *User) error {
   if !db.IsConnected() {
     return fmt.Errorf("database not connected")
   }
&lt;span class="gi"&gt;+
+  if db.UserExists(user) {
+    return fmt.Errorf("user already exists")
+  }
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;   // create the user
   return nil
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diff for the guarded function is much easier to read. If you've ever done a code review, you can see how much easier the latter function is to review, too. Being able to understand what a function does at a glance is essential to the maintainability, and that's exactly what guard functions do. If you want a maintainable system, use guard clauses.&lt;/p&gt;

&lt;h2&gt;
  
  
  When can you use else?
&lt;/h2&gt;

&lt;p&gt;Just like every other rule in software development, there's times when you should use &lt;code&gt;else&lt;/code&gt; instead of a guard clause.  My rule of thumb for when to use a guard clause vs. using an else statement is: if it alters the flow of a program, use a guard clause. So if you need to log an error, or call a setup function, etc., use a guard clause. If you just need to log that a non-essential component of the application failed, for example, use an &lt;code&gt;else&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Below is an example of logging a failure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;StartApplication&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;analyticsConnected&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;analytics&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;analyticsConnected&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"analytics connected"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failed to connect to analytics service"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c"&gt;// continue with starting the application&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While some marketing people may argue that collecting analytics is the most important part of an application, us developers know that the application will work just fine without them. So when we start an application, if our analytics fail, we can continue with startup and retry starting the analytics later.&lt;/p&gt;

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

&lt;p&gt;Despite having if/else drilled into my brain during university, in my career I have found that I rarely use the if/else flow. Instead, I opt for guard clauses, and I hope you have been convinced to use them, too. Personally, I consider using &lt;code&gt;else&lt;/code&gt; a sign of bad code, but obviously it is situation dependent. Whatever your coding style, I encourage you to give guard clauses a try and see how they simplify your code.&lt;/p&gt;

&lt;p&gt;If you have any questions or comments, I'd love to hear them! Feel free to leave a comment below or shoot me a message on &lt;a href="https://twitter.com/brendenehlers"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@markusspiske?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Markus Spiske&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/captcha-cvBBO4PzWPg?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>coding</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
