<?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: parvej-code</title>
    <description>The latest articles on DEV Community by parvej-code (@parvejcode).</description>
    <link>https://dev.to/parvejcode</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%2F720821%2F1315933d-a755-4ba1-8c1e-8818717e730c.jpeg</url>
      <title>DEV Community: parvej-code</title>
      <link>https://dev.to/parvejcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parvejcode"/>
    <language>en</language>
    <item>
      <title>How do I refactor IF_ELSE condition</title>
      <dc:creator>parvej-code</dc:creator>
      <pubDate>Wed, 18 May 2022 05:02:36 +0000</pubDate>
      <link>https://dev.to/parvejcode/how-do-i-refactor-ifelse-condition-2gje</link>
      <guid>https://dev.to/parvejcode/how-do-i-refactor-ifelse-condition-2gje</guid>
      <description>&lt;p&gt;Writing if-else is a daily job for a programmer. Whenever we write code, we check if something is true or false. But writing too many if-else conditions makes code unreadable. Below are some steps which I follow to refactor my if-else blocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method calling:
&lt;/h2&gt;

&lt;p&gt;Sometimes based on one parameter, we had to do different operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeClass {
  public action(status: string) {
    if (status === 'draft') {
      //Do some operations
    } else if (status === 'confirmed') {
      //Do some operations
    } else if (status === 'payed') {
      //Do some operations
    } else if (status === 'shipped') {
      //Do some operations
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can improve this by calling the method based on the parameter value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeClass {
  public action(status: string) {
    if (typeof this[status] === 'undefined') {
      //Throw your exception, do some default operations and return
    }
    return this[status]()
  }

  public draft() {
    //Do the draft operations
  }

  public confirmed() {
    //Do the confirm operations
  }

  public payed() {
    //Do the payed operations
  }

  public shipped() {
    //Do shipped operations
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Object literal:
&lt;/h2&gt;

&lt;p&gt;If you only have to return one value based on a parameter, then you can use an object literal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (operator === '=') {
  return a === b;
} else if (operator === '&amp;lt;') {
  return a &amp;lt; b;
} else if (operator === '&amp;gt;') {
  return a &amp;gt; b;
} else if (operator === '&amp;gt;=') {
  return a &amp;gt;= b;
} else if (operator === '&amp;lt;=') {
  return a &amp;lt;= b;
} else if (operator === 'like') {
  return String(a).toLowerCase().includes(String(b).toLowerCase());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refactored&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;action(operator) {
    const operators = {
      '=': (a, b) =&amp;gt; a === b,
      '&amp;lt;': (a, b) =&amp;gt; a &amp;lt; b,
      '&amp;gt;': (a, b) =&amp;gt; a &amp;gt; b,
      '&amp;gt;=': (a, b) =&amp;gt; a &amp;gt;= b,
      '&amp;lt;=': (a, b) =&amp;gt; a &amp;lt;= b,
      like: (a, b) =&amp;gt; String(a).toLowerCase().includes(String(b).toLowerCase()),
    };

    if (typeof operators[operator] === 'undefined') {
      //Do your operation and return
    }
    return operators[operator];
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: We can also use a factory design pattern for it. But in most cases, it will be an overkill &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>refactorit</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
