<?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: Tanisha Sabherwal</title>
    <description>The latest articles on DEV Community by Tanisha Sabherwal (@tanisha03).</description>
    <link>https://dev.to/tanisha03</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%2F510251%2Faf1742bf-656f-4696-bd59-195e0a3e8c67.jpeg</url>
      <title>DEV Community: Tanisha Sabherwal</title>
      <link>https://dev.to/tanisha03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanisha03"/>
    <language>en</language>
    <item>
      <title>Clean Code Part 3: Comments</title>
      <dc:creator>Tanisha Sabherwal</dc:creator>
      <pubDate>Thu, 15 Jul 2021 10:37:21 +0000</pubDate>
      <link>https://dev.to/tanisha03/clean-code-part-3-comments-4757</link>
      <guid>https://dev.to/tanisha03/clean-code-part-3-comments-4757</guid>
      <description>&lt;p&gt;This is part 3 of the series of Clean Code By Robert C. Martin. If you haven't gone through the part 2, &lt;a href="https://dev.to/tanisha03/clean-code-part-2-functions-43mj"&gt;read here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Most code should be self-explanatory. Code also moves around a lot. The comments don’t follow it. We can update the comments as frequently as the code, but it’s probably better to make the code clear enough so that we don’t need the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The proper use of comments is to compensate for our failure to express ourself in the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Good Comments
&lt;/h2&gt;

&lt;p&gt;These comments are necessary and beneficial.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Legal Comments: These include copyright and authorship statements at the start of source file.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Copyright (C) 2020 by Foo Inc. All rights reserved&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Informative comments: These comments are used to convey more information in addition to the code. But in most cases, they can be removed after naming things better.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// return toggleState for accordion&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;....&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we can just change the name to be more meaningful like:&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;isAccordionOpen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Explanation of Intent: Comment beyond the implementation, reveals the intent behind the decision. &lt;br&gt;
For example: This can be used to specify why certain approach are used according to the use case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clarification: These comments are helpful to translate some obscure arguments or return values into a more readable form.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lodash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// divide array into groups of 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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;splitArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Warning of Consequences: Such comments are used to let people know the consequences of certain code snippet.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// not the optimal approach to perform the task&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;TODO comments: The comments may serve as a reminder to add or delete some code, or to clean them up in some way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Emphasis: Comments used to emphasise the importance of some code. It shows that the code is important so that people won’t overlook it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSDoc comments: When writing libraries, documentation or code is served to the masses that use them. This can be done with JSDoc by writing comments in the code, which will generate documentation from it.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Represents a Person.
 * @constructor
 * @param {string} name - The name of the person.
 * @param {number} age - The age of the person.
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&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;h2&gt;
  
  
  Bad Comments
&lt;/h2&gt;

&lt;p&gt;Most comments fall into this category, which should be avoided to maintain a clean code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Mumbling: Hastily plopping a comment, not paying much attention to the details does not serve much purpose.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redundant Comments: As the name suggests, it does not serve any purpose. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Misleading comments: These comments for statements are not precise enough to be accurate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Journal Comments: Comment added to the start of the module every time it is edited leads to overhead. A CHANGELOG should be used for these purposes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Other kinds of bad comments include: Noise Comments, Positional Markers, Closing Braces Comments are our most favourite &lt;strong&gt;Commented-out Code&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't Comment bad code, rather rewrite it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stay tuned in, for Part 4 of the series.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>codereview</category>
      <category>codequality</category>
      <category>programming</category>
    </item>
    <item>
      <title>Clean Code Part 2: Functions</title>
      <dc:creator>Tanisha Sabherwal</dc:creator>
      <pubDate>Mon, 05 Jul 2021 07:43:47 +0000</pubDate>
      <link>https://dev.to/tanisha03/clean-code-part-2-functions-43mj</link>
      <guid>https://dev.to/tanisha03/clean-code-part-2-functions-43mj</guid>
      <description>&lt;p&gt;This is part 2 of the series of Clean Code By Robert C. Martin. If you haven't gone through the part 1, &lt;a href="https://dev.to/tanisha03/clean-code-part-1-meaningful-names-4khp"&gt;read here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smaller Functions
&lt;/h2&gt;

&lt;p&gt;The first rule for functions is that they should be smaller and easily readable by anyone. It should have the least number of lines, each being transparently obvious. Also, avoid complex nested structures and many indent levels.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functions should do one thing. They should do it well. They should do it only.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To ensure that the functions are doing one thing only, it is essential that all the lines inside the function belong to one level of abstraction.&lt;/p&gt;

&lt;p&gt;Abstraction is a fundamental concept in OOPS. In brief and layman’s term and it talks about hiding the “how” part and only expose “what” to the outer world. Level of abstraction is being able to mentally grouping inside the function. In general, different “blocks” of code inside a method is a classic indicator of different level of abstractions. This means, the reader of the code now has to create a “branch” in their mental grouping to read that condition or loop block and merge back to the same level where the block ended.&lt;/p&gt;

&lt;p&gt;Let me explain with an example.&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;checkForValidOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&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;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PLACED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUCCESSFUL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;walletBalance&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the above function, there are two levels of abstraction since mathematical computation is being carried put in the last statement. This can be avoided using a small tweak.&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;checkForValidOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&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;return&lt;/span&gt; 
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PLACED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUCCESSFUL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;generateTotalAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;walletBalance&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, using abstraction, we are essentially hiding the “how” part and preserving the part indicating what the function does. This was a simple example with only one level of abstraction, when function tends to become bigger with many levels of abstraction, the &lt;a href="http://principles-wiki.net/principles:single_level_of_abstraction"&gt;Single Level of Abstraction (SLA)&lt;/a&gt; principle should be followed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow the Stepdown Rule
&lt;/h2&gt;

&lt;p&gt;The rule suggests that the code should be readable in a top-down narrative, descending one level of abstraction per function. It implies that the function ordering is no longer random. A caller function should always reside above the callee function.&lt;/p&gt;

&lt;p&gt;Taking the previous example, the ordering of the functions is best as given below and not the other way around.&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;checkForValidOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&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;return&lt;/span&gt; 
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PLACED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUCCESSFUL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;generateTotalAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;walletBalance&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;generateTotalAmount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&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;price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tax&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;100&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;h2&gt;
  
  
  Functional Arguments
&lt;/h2&gt;

&lt;p&gt;Functions can be broadly classified into the following types based on the number of arguments: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Niladic(zero)&lt;/li&gt;
&lt;li&gt;Monadic(one)&lt;/li&gt;
&lt;li&gt;Dyadic(two)&lt;/li&gt;
&lt;li&gt;Triadic(three)&lt;/li&gt;
&lt;li&gt;Polyadic(more than three)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As the number of arguments for a function increases, complexity increases and it even becomes tougher from a testing point of view.&lt;/p&gt;

&lt;p&gt;When functions need three or more arguments, most of the times, it is easier to wrap some of these arguments into a class/object of its own. This will ensure better readability.&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;describeFruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&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;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numSeeds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&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;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruitName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruitColor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. It's &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruitSize&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. 
  It costs &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. It has &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numSeeds&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. The type if 
  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;The above function is too confusing to read with so many arguments. A better alternative and cleaner versionis given below.&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;describeFruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&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;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&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="s2"&gt; is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. It's 
  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. It costs &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. It has 
  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;numSeeds&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. The type if &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;h2&gt;
  
  
  No Side Effects
&lt;/h2&gt;

&lt;p&gt;Side effects are code in a function that makes changes to things that are outside the function. This can lead to unexpected changes to code outside and cause damaging mistruths.&lt;/p&gt;

&lt;p&gt;For example, the code snippet given below should be avoid.&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;numberOfBoxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;addBoxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;numberOfBoxes&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeBoxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;numberOfBoxes&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, 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;const&lt;/span&gt; &lt;span class="nx"&gt;addBoxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numberOfBoxes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;numberOfBoxes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;removeBoxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numberOfBoxes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;numberOfBoxes&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ending Notes
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't repeat yourself. They are functions for a reason.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Functions are the verbs of the code and do task to avoid any duplication. When structured correctly, functions will be shorter, well named and much readable. Hence, cleaner code.&lt;/p&gt;

&lt;p&gt;Head over to &lt;a href="https://dev.to/tanisha03/clean-code-part-3-comments-4757"&gt;Part 3 of the series&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>codereview</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Clean Code Part 1: Meaningful Names</title>
      <dc:creator>Tanisha Sabherwal</dc:creator>
      <pubDate>Fri, 02 Jul 2021 10:54:32 +0000</pubDate>
      <link>https://dev.to/tanisha03/clean-code-part-1-meaningful-names-4khp</link>
      <guid>https://dev.to/tanisha03/clean-code-part-1-meaningful-names-4khp</guid>
      <description>&lt;p&gt;Naming variables, functions, classes, arguments, and packages is an art not everyone understands. Those who understand, live a technical debt-free life. But few simple pointers can help you reach this goal. While most of these will sound obvious but are not completely implemented by a lot of developers. Read ahead to crack the code for writing meaningful names.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reveal the Intent
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Choosing good names takes time but saves more than it takes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The names of variables, functions, classes should answer three broad questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why it exists&lt;/li&gt;
&lt;li&gt;What it does&lt;/li&gt;
&lt;li&gt;How it is used
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//does not reveal anything&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;daysSinceCreation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// clearly reveals what it stores&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isModalOpen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// depicts the variable is a boolean&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUserID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// denotes a function or task&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, focus on the simplicity of the code rather than simplicity. If a variable requires a comment, it does not reveals its intent well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Disinformation
&lt;/h2&gt;

&lt;p&gt;Avoid names which leave false clues which might obscure the meaning of code. Treat it as an honourable human, it should say what it means and mean what it says.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't name a group of accounts as

&lt;code&gt;AccountList&lt;/code&gt;

unless it is a list.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;Avoid two names with subtle differences like

&lt;code&gt;isAccountInformationOfUserRequired&lt;/code&gt;

and

&lt;code&gt;isAccountInformationOfAdminRequired&lt;/code&gt;

.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Make Meaningful Distinctions
&lt;/h2&gt;

&lt;p&gt;Number-series naming like &lt;strong&gt;a1, a2, ...., an&lt;/strong&gt; makes the code non-informative and does not reveals anything about the intention.&lt;/p&gt;

&lt;p&gt;Another avoidable typing of naming is using noise words. Words like variable, NameString, Customer, does not reveal anything and so should be avoided.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Pronounceable and Searchable Names
&lt;/h2&gt;

&lt;p&gt;Use words that are pronounceable so that others can use it for communication. This will enable better collaboration because &lt;strong&gt;"Programming is a Social Activity"&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Imagine someone calling you to fix the bug and mentions using the &lt;strong&gt;&lt;em&gt;function getmddh&lt;/em&gt;&lt;/strong&gt; which gets month, date, day and hour for the post. Saying "get-M-D-D-H" is both frustrating and confusing, so rather just call it getPostTimeStamp.&lt;/p&gt;

&lt;p&gt;Similarly names should be searchable. Single or double letter names are hard to search in a codebase. In this case Ctrl+Shift+F will no longer be at rescue and is such a bade case for giving meaningful names.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Roles
&lt;/h2&gt;

&lt;p&gt;Names should be such that it reveals the role of the variable, this specially is very important in languages like JavaScript everything is either a var, const or let.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Class and Object names should be a noun or noun phrase like AddressParser, WikiPage, and CustomerDetails.&lt;/li&gt;
&lt;li&gt;Method names should be a verb or verb phase like deleteUserEntry, getUserDetails and savePostData.&lt;/li&gt;
&lt;li&gt;Booleans should have prefix like "is" to communicate binary or 0-1 behaviour.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Add Meaningful Context
&lt;/h2&gt;

&lt;p&gt;Sometimes variable names require additional data to give context to generic variable names like firstName, LastName, city, zipcode etc. In such a case adding a prefix like addrfirstName and addrLastName will denote it being a part of a larger Address class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope Length Rule
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Variables with shorter scope should have shorter name and vice-versa because ones with longer scope at used at multiple locations and should be descriptive.&lt;/li&gt;
&lt;li&gt;For classes/functions, the opposite is true because the longer the scope, easier should be the access for class/functions names. It should be short and crisp however for shorter scopes, longer and descriptive names can be helpful to reveal the intent.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Ending Notes
&lt;/h2&gt;

&lt;p&gt;Quoting from the book,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any fool can write code a computer can understand, but it takes a good programmer to write code that humans can understand.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Head over to &lt;a href="https://dev.to/tanisha03/clean-code-part-2-functions-43mj"&gt;Part 2 of the series&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>codequality</category>
      <category>codereview</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Clean Code by Robert C. Martin</title>
      <dc:creator>Tanisha Sabherwal</dc:creator>
      <pubDate>Thu, 01 Jul 2021 18:53:08 +0000</pubDate>
      <link>https://dev.to/tanisha03/clean-code-by-robert-c-martin-41l9</link>
      <guid>https://dev.to/tanisha03/clean-code-by-robert-c-martin-41l9</guid>
      <description>&lt;p&gt;The book opens with an infographic that clearly sums up every developer's life. He calls the skill for developing clean code as craftsmanship, best acquired in two ways:&lt;/p&gt;

&lt;p&gt;1.Knowledge of principles, patterns, practices, and heuristics&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Practice&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pINs4vxg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jjw5u37z9w3cm6n6q2k.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pINs4vxg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jjw5u37z9w3cm6n6q2k.jpeg" alt="Depicting Frustrating Code Review"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will be multiple-part series where I will dissect the chapters of the book and write about key takeaways that resonate with me.&lt;/p&gt;

&lt;p&gt;Hopefully, this will enable me to get out the never-ending cycle of technical debt and increase my efficiency as a developer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eKPqQ_HV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sixyhfqdbdzaqcei33st.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eKPqQ_HV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sixyhfqdbdzaqcei33st.png" alt="Tech Debt is annoying"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/tanisha03/clean-code-part-1-meaningful-names-4khp"&gt;Part 1: Meaningful Names&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/tanisha03/clean-code-part-2-functions-43mj"&gt;Part 2: Functions&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/tanisha03/clean-code-part-3-comments-4757"&gt;Part 3: Comments&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>codereview</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
