<?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: Damon Janis</title>
    <description>The latest articles on DEV Community by Damon Janis (@damonvjanis).</description>
    <link>https://dev.to/damonvjanis</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%2F524879%2Fff3ccf2b-f394-4e9f-865a-0b7c30269e84.jpeg</url>
      <title>DEV Community: Damon Janis</title>
      <link>https://dev.to/damonvjanis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damonvjanis"/>
    <language>en</language>
    <item>
      <title>Early Return in Elixir</title>
      <dc:creator>Damon Janis</dc:creator>
      <pubDate>Wed, 20 Oct 2021 12:29:51 +0000</pubDate>
      <link>https://dev.to/damonvjanis/early-return-in-elixir-4975</link>
      <guid>https://dev.to/damonvjanis/early-return-in-elixir-4975</guid>
      <description>&lt;p&gt;The only thing I've missed while working in Elixir the last few years is an early return.&lt;/p&gt;

&lt;p&gt;In Javascript, you can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;contrivedFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;arg&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;falsy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt; &lt;span class="o"&gt;!==&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wtf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// This will never be evaluated because JS is&lt;/span&gt;
  &lt;span class="c1"&gt;// a sad language where [] does not equal []&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;truthy&lt;/span&gt;&lt;span class="dl"&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;At any point, you can stop the execution of the current function and return a value, and nothing after that point will run.&lt;/p&gt;

&lt;p&gt;Usually, in Elixir, you can get by just fine without that ability. You can use the &lt;code&gt;with&lt;/code&gt; statement, pattern matching, or control flow like &lt;code&gt;case&lt;/code&gt; and &lt;code&gt;cond&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But sometimes, I really miss having an early return. Usually when I'm working in a really messy domain with a lethal combo of tech debt, third-party APIs, and a long chain of processing where things can go wrong in a lot of places.&lt;/p&gt;

&lt;p&gt;I recently ran into a situation like that, and decided to do some digging. I knew that a function stopped execution as soon as an error was &lt;code&gt;raise&lt;/code&gt;d, so I thought I could maybe write a macro for that functionality. But then in the &lt;a href="https://elixir-lang.org/getting-started/try-catch-and-rescue.html"&gt;Elixir guides&lt;/a&gt; for &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt;, and &lt;code&gt;rescue&lt;/code&gt;, I realized that the solution was hiding in plain sight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;contrived_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;!arg&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"falsy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"wtf"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# Elixir is sane and [] does equal [], so this is possible&lt;/span&gt;
  &lt;span class="s2"&gt;"truthy"&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt;
  &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out, when you &lt;code&gt;throw&lt;/code&gt; within a function, execution stops and you can &lt;code&gt;catch&lt;/code&gt; (and return) the value! No macros or extra abstractions needed.&lt;/p&gt;

&lt;p&gt;I don't anticipate that I'll use this pattern very much - in most cases the standard methods of control flow are easier to reason about and follow.&lt;/p&gt;

&lt;p&gt;But, I hope that like me you appreciate having an extra tool in hand for those situations when it feels like the best way to express a bit of logic.&lt;/p&gt;

</description>
      <category>elixir</category>
    </item>
  </channel>
</rss>
