<?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: Daniel A. White</title>
    <description>The latest articles on DEV Community by Daniel A. White (@daniel).</description>
    <link>https://dev.to/daniel</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%2F14313%2Ffe6d937f-6ec7-4a18-95f8-8e811dddb99d.jpeg</url>
      <title>DEV Community: Daniel A. White</title>
      <link>https://dev.to/daniel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daniel"/>
    <language>en</language>
    <item>
      <title>Don't be afraid to expand your code for readability</title>
      <dc:creator>Daniel A. White</dc:creator>
      <pubDate>Thu, 30 Mar 2017 18:47:27 +0000</pubDate>
      <link>https://dev.to/daniel/dont-be-afraid-to-expand-your-code-for-readability</link>
      <guid>https://dev.to/daniel/dont-be-afraid-to-expand-your-code-for-readability</guid>
      <description>

&lt;p&gt;Variable names are cheap. Conditional checks are cheap. So why limit yourself when there's a cleaner way to express your code and increase its maintainability.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use long variable names
&lt;/h1&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var flag = true; // :(
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;flag&lt;/code&gt; isn't very descriptive of what the boolean should be controlling. Don't be afraid to expand the name of variables to express what they really do. JavaScript minifiers' jobs are to shorten the variable names down - it isn't your job to do that - it's your job to write readable and maintainable code.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var shouldSaveDocument = true; // :)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;
  
  
  Don't be afraid to break down conditionals
&lt;/h1&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// :(
if (doc.hasChanged &amp;amp;&amp;amp; !doc.isReadOnly &amp;amp;&amp;amp; fs.hasFreeSpace &amp;amp;&amp;amp; user.wantsToSave) {
    doc.save();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When doing some sort of precondition checking, don't throw them all into one &lt;code&gt;if&lt;/code&gt;. The logical operators become a jumbled mess. It's hard to change or debug them especially when they are all on one line. Don't be afraid to break down conditionals and retest.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var shouldSaveDocument = true;

// retest the flag here so that operations can be added/removed easily and can see them clearly in a diff.
if (shouldSaveDocument &amp;amp;&amp;amp; !doc.hasChanged) {
     trace('not saving unchanged document');
     shouldSaveDocument = false;
}

if (shouldSaveDocument &amp;amp;&amp;amp; doc.isReadOnly) {
     trace('not saving read-only document');
     shouldSaveDocument = false;
}

if (shouldSaveDocument &amp;amp;&amp;amp; !fs.hasFreeSpace) {
     trace('not saving document when there is not any freespace');
     shouldSaveDocument = false;
}

if (shouldSaveDocument &amp;amp;&amp;amp; !user.wantsToSave) {
     trace('not saving document as the user does not want to');
     shouldSaveDocument = false;
}

if (shouldSaveDocument) {
    doc.save();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


</description>
      <category>conditionals</category>
      <category>variables</category>
      <category>namingthings</category>
    </item>
  </channel>
</rss>
