<?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: Dan Bar-Shalom</title>
    <description>The latest articles on DEV Community by Dan Bar-Shalom (@danbars).</description>
    <link>https://dev.to/danbars</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%2F1262572%2F5021ac05-d81e-48f0-9e1f-159986d9ee09.png</url>
      <title>DEV Community: Dan Bar-Shalom</title>
      <link>https://dev.to/danbars</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danbars"/>
    <language>en</language>
    <item>
      <title>I've created a text-to-form builder - feedback appreciated</title>
      <dc:creator>Dan Bar-Shalom</dc:creator>
      <pubDate>Mon, 19 May 2025 15:42:14 +0000</pubDate>
      <link>https://dev.to/danbars/ive-created-a-text-to-form-builder-feedback-appreciated-3foa</link>
      <guid>https://dev.to/danbars/ive-created-a-text-to-form-builder-feedback-appreciated-3foa</guid>
      <description>&lt;p&gt;For several years &lt;a href="https://form-data.com" rel="noopener noreferrer"&gt;form-data.com&lt;/a&gt; offered a forms-backend as a service, for people building their own static forms.&lt;/p&gt;

&lt;p&gt;Creating a form builder was an idea that I played with for a long time. However, as a developer I didn’t like most of the form builders out there. It always feels like I have to fill-in a (really complicated) form just to create a form.&lt;/p&gt;

&lt;p&gt;What I ended up with was a text-to-form builder. On the left you write simple text descriptor, and on the right you get a form. And of course, as with any product these days, you can just ask the AI to write it for you. Publish the form is simple, and you’ve got yourself a hosted form.&lt;/p&gt;

&lt;p&gt;And the backend gets all you’d expect from a forms the backend, including spam filter, email notifications, auto-response emails and integrations.&lt;/p&gt;

&lt;p&gt;I’d really appreciate if you give it a go and share any feedback - the studio is open without registration, and if you want to publish you need to signup without credit-card, and you get 100 free credits.&lt;/p&gt;

&lt;p&gt;Try it out here: &lt;a href="https://studio.form-data.com" rel="noopener noreferrer"&gt;studio.form-data.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next step is releasing the cli so developers can manage the form descriptor as code in their own repo.&lt;/p&gt;

&lt;p&gt;You can see the studio in &lt;a href="https://www.youtube.com/watch?v=Fryxiukm--w" rel="noopener noreferrer"&gt;this youtube&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Trimming JavaScript: ditch verbosity, gain readability</title>
      <dc:creator>Dan Bar-Shalom</dc:creator>
      <pubDate>Sun, 21 Apr 2024 14:32:12 +0000</pubDate>
      <link>https://dev.to/danbars/trimming-javascript-ditch-verbosity-gain-readability-3fko</link>
      <guid>https://dev.to/danbars/trimming-javascript-ditch-verbosity-gain-readability-3fko</guid>
      <description>&lt;p&gt;Are you familiar with expressions like these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;a !== undefined &amp;amp;&amp;amp; a !== null ? a : b&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;if (a.x === undefined || a.x === null) { a.x = b }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{ foo: foo, bar: bar }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a ? a.b : undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;function getX(o) { return o.x }&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What do they all have in common? They are highly verbose, and all have a nice shorthand alternatives. Let's see how we can improve each one of these:&lt;/p&gt;

&lt;h2&gt;
  
  
  Nullish coalescing operator &lt;code&gt;??&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This operator checks the value of the left hand-side operand - if it's null or undefined, it returns the right hand-side operand. Otherwise it returns the left hand-side, without evaluating the right hand-side.&lt;br&gt;
This allows simplifying the following expression:&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To 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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike &lt;code&gt;||&lt;/code&gt; operator that checks for any falsy value, this operator only checks for nullish values - meaning &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;. Therefore it is safer to use in cases that &lt;code&gt;a&lt;/code&gt; is a boolean which can be &lt;code&gt;false&lt;/code&gt; or a number which can be &lt;code&gt;0&lt;/code&gt;, and then &lt;code&gt;a || b&lt;/code&gt; will return the value of &lt;code&gt;b&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Nullish coalescing assignment &lt;code&gt;??=&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This operator only evaluates the right operand and assigns to the left if the left operand is null or undefined.&lt;br&gt;
So we can simplify this expression:&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to 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="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;??=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JSON shorthand syntax
&lt;/h2&gt;

&lt;p&gt;The shorthand syntax was introduced with ES6 and is already pretty common. &lt;br&gt;
Use it to shorten 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;talk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;talk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;talk&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to that:&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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;talk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;talk&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optional chaining &lt;code&gt;?.&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This operator accesses an object's property just like &lt;code&gt;.&lt;/code&gt; does. However, unlike &lt;code&gt;.&lt;/code&gt;, if the object is null or undefined, the expression will return &lt;code&gt;undefined&lt;/code&gt; instead of throwing an error.&lt;br&gt;
This allows replacing this code:&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="nx"&gt;a&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with that:&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="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This operator is pretty strong, and can be used in various ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chaining: &lt;code&gt;a?.b?.c?.d&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Call interface method: &lt;code&gt;someInterface.customMethod?.()&lt;/code&gt;
Note that in this case, if &lt;code&gt;customMethod&lt;/code&gt; exists, but it is not a function, you'd still get an exception &lt;code&gt;someInterface.customMethod is not a function&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Access dynamic property with bracket notation: &lt;code&gt;x?.[propname]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Access array items: &lt;code&gt;array?.[50]&lt;/code&gt; - even if array is nullish, you'd get &lt;code&gt;undefined&lt;/code&gt; instead of an exception&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Arrow functions
&lt;/h2&gt;

&lt;p&gt;There's a lot to say about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;arrow functions&lt;/a&gt;, but in the context of this post I want to focus on a specific use - 1 liner methods that calculate something or access some property. They have 2 attributes that help with code shortening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrow functions that do not have a block body wrapped with curly brackets &lt;code&gt;{/*...*/}&lt;/code&gt; have an implicit &lt;code&gt;return&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;in a single-param arrow function you do not have to put the argument in parentheses 
Meaning, you can change this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&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="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with 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;const&lt;/span&gt; &lt;span class="nx"&gt;getX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is super useful, for example, for mapping functions.&lt;br&gt;
Note that there are some caveats though:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrow functions do not have &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Arrow functions do not have &lt;code&gt;arguments&lt;/code&gt;. You can use spread operator instead. E.g &lt;code&gt;(...args) =&amp;gt; args[0]&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Arrow functions cannot call &lt;code&gt;super&lt;/code&gt; or be used as constructors&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>readability</category>
      <category>tips</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building an API-product - what is your biggest challenge?</title>
      <dc:creator>Dan Bar-Shalom</dc:creator>
      <pubDate>Mon, 19 Feb 2024 04:50:57 +0000</pubDate>
      <link>https://dev.to/danbars/building-an-api-product-what-is-your-biggest-challenge-2efo</link>
      <guid>https://dev.to/danbars/building-an-api-product-what-is-your-biggest-challenge-2efo</guid>
      <description>&lt;p&gt;If you are building an API which is a product - what is the biggest challenge for you productizing it? &lt;br&gt;
For me - the API is the easiest part, but everything around can take 5X-10X time to complete:&lt;br&gt;
website, user management, validating API tokens, developer admin panel, docs, etc...&lt;/p&gt;

&lt;p&gt;Cover image by RealToughCandy on &lt;a href="https://www.pexels.com/photo/programmer-holding-a-paper-cutout-with-an-api-quote-11035364/"&gt;Pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>buildinpublic</category>
      <category>sell</category>
    </item>
    <item>
      <title>How to structure your code for readability</title>
      <dc:creator>Dan Bar-Shalom</dc:creator>
      <pubDate>Mon, 22 Jan 2024 19:44:45 +0000</pubDate>
      <link>https://dev.to/danbars/how-to-structure-your-code-for-readability-455c</link>
      <guid>https://dev.to/danbars/how-to-structure-your-code-for-readability-455c</guid>
      <description>&lt;p&gt;When writing code, it is often tempting to wrap the main flow inside multiple if statements in order to validate that certain conditions are met. &lt;br&gt;
For example, let's say I'm writing a simple method that returns the length of a string. It might look like 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;function&lt;/span&gt; &lt;span class="nf"&gt;getLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&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;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;parameter must be a string&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;While this guards against unexpected parameter types, the main flow is enclosed within an if statement. A more refined approach looks like 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;function&lt;/span&gt; &lt;span class="nf"&gt;getLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;parameter must be a string&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;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&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;On the surface, the distinction may appear subtle, yet the structure of the code makes the main code flow more readable.&lt;/p&gt;

&lt;p&gt;This advantage becomes even more apparent when there are multiple exclusion flows. You can turn this nested structure:&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;function&lt;/span&gt; &lt;span class="nf"&gt;foo&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="cm"&gt;/*condition A*/&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="cm"&gt;/*condition B*/&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="cm"&gt;/*condition C*/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/*main flow*/&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="cm"&gt;/*handle !condition C*/&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cm"&gt;/*handle !condition B*/&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="cm"&gt;/*handle !condition A*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into 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;function&lt;/span&gt; &lt;span class="nf"&gt;foo&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="cm"&gt;/* !condition A */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* 
    handle !condition A 
    return
    */&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="cm"&gt;/* !condition B */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* 
    handle !condition B 
    return
    */&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="cm"&gt;/* !condition C */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* 
    handle !condition C 
    return
    */&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="cm"&gt;/*main flow*/&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is cleaner and more readable code, easier to understand and maintain.&lt;/p&gt;

</description>
      <category>code</category>
      <category>codereview</category>
    </item>
  </channel>
</rss>
