<?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: Mathan raj</title>
    <description>The latest articles on DEV Community by Mathan raj (@immathanr).</description>
    <link>https://dev.to/immathanr</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%2F65400%2F38070ecd-e781-4fce-a8d8-b5ee37ca6d2d.jpg</url>
      <title>DEV Community: Mathan raj</title>
      <link>https://dev.to/immathanr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/immathanr"/>
    <language>en</language>
    <item>
      <title>Basics of refactoring a bad code | part 1 - If condition</title>
      <dc:creator>Mathan raj</dc:creator>
      <pubDate>Wed, 29 May 2019 19:21:42 +0000</pubDate>
      <link>https://dev.to/immathanr/basics-of-refactoring-bad-code-part-1-if-condition-3c46</link>
      <guid>https://dev.to/immathanr/basics-of-refactoring-bad-code-part-1-if-condition-3c46</guid>
      <description>&lt;p&gt;We have all been in a situation that when we are growing as a developer often we find ourselves writing code that doesn't look right or we have a doubt that whether it follows the best practices. This article is just &lt;strong&gt;part 1&lt;/strong&gt; of the total &lt;strong&gt;n&lt;/strong&gt; series. (I really don't know how many would come 🤷‍♂️)&lt;/p&gt;

&lt;h3&gt;
  
  
  If statement
&lt;/h3&gt;

&lt;p&gt;If statements can be a root cause of writing bad code. Always think twice before writing nested if conditions whether there is a way to simplify it. A badly written &lt;code&gt;if condition&lt;/code&gt; can reduce the readability of the program.&lt;br&gt;
Let's make the following assumptions here. Any clean code will have the following properties.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Readable&lt;/li&gt;
&lt;li&gt;Concise&lt;/li&gt;
&lt;li&gt;Scalable - Should be able to allow more conditions or business logic to be added without affecting the above two factors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example 1 of Bad code&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Improvement 1 - Combining relevant conditions&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Improvement 2 - Applying inversion of condition&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Combining relevant conditions
&lt;/h4&gt;

&lt;p&gt;Combining multiple conditions that are relevant by using the logical operator can reduce down the lines of code and implies a better understanding of the condition. &lt;/p&gt;

&lt;h4&gt;
  
  
  Inversion of condition
&lt;/h4&gt;

&lt;p&gt;Just by inverting the thought process that goes for the condition in if statement can vastly improve the readability.&lt;/p&gt;

&lt;p&gt;Example 2 of Bad code&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Improved Code - Applied short-circuiting + inversion of condition&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Short-circuting
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Fail-fast"&gt;Failing-fast&lt;/a&gt; will help you deal with fewer problems going further. (short-circuiting is the &lt;a href="https://dictionary.cambridge.org/dictionary/english/trump-card"&gt;trump card&lt;/a&gt; remember)&lt;/p&gt;

&lt;p&gt;We have discussed some of the practices I follow when I write&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tips&lt;/strong&gt;: &lt;code&gt;switch&lt;/code&gt; statements can be of a good replacement for if statement in case of checking for enum or strings.&lt;/p&gt;

</description>
      <category>designprinciples</category>
      <category>codingstandards</category>
    </item>
  </channel>
</rss>
