<?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>Forem: Chas Richards</title>
    <description>The latest articles on Forem by Chas Richards (@chazzie).</description>
    <link>https://forem.com/chazzie</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%2F1187102%2F63bcd78f-9854-4511-91e8-3a791ba8655d.jpeg</url>
      <title>Forem: Chas Richards</title>
      <link>https://forem.com/chazzie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chazzie"/>
    <language>en</language>
    <item>
      <title>Why clean code makes JavaScript programming easier</title>
      <dc:creator>Chas Richards</dc:creator>
      <pubDate>Tue, 16 Jan 2024 15:06:56 +0000</pubDate>
      <link>https://forem.com/ionos/why-clean-code-makes-javascript-programming-easier-25cp</link>
      <guid>https://forem.com/ionos/why-clean-code-makes-javascript-programming-easier-25cp</guid>
      <description>&lt;h3&gt;
  
  
  From the perspective of a Frontend Developer
&lt;/h3&gt;

&lt;p&gt;I am a qualified Frontend Developer and work at IONOS as Community Relations Manager, where I am responsible for Developer Relations. For years, I have been observing that the topic of clean code is becoming relevant again and again, which is why I am sharing the experiences I have had with it.&lt;/p&gt;

&lt;p&gt;JavaScript is one of the most popular programming languages in the world, and for me as a Frontend Developer, JavaScript has become indispensable. The scripting language, which is often associated with interactive websites, makes it possible to create dynamic websites, control user interactions and develop modern web applications.&lt;/p&gt;

&lt;p&gt;Anyone who has worked with this programming language knows how important it is to be able to understand your own code or the code of others. For me, writing so-called clean code is therefore an essential skill that you should master. At the latest at the next code review or for a new project with a new development team, the question will come up at some point: What does clean code actually mean to you?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is clean code important to me?&lt;/strong&gt;&lt;br&gt;
After more than 5 years of working from home, I have personally realized how important clean code is in a software project. Short-term agreements within a team are becoming more and more complex, as the daily routine is characterized by more meetings due to physical separation. Pair programming with a face-to-face exchange is taking place less and less frequently, and code reviews are also shorter and reduced to the essentials. Whereas face-to-face meetings can sometimes go into more detail and provide further impulses.&lt;/p&gt;
&lt;h2&gt;
  
  
  My criteria for clean code
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Readability and comprehension
&lt;/h3&gt;

&lt;p&gt;Clean code is particularly important when it comes to the &lt;strong&gt;readability&lt;/strong&gt; and &lt;strong&gt;comprehensibility&lt;/strong&gt; of the code. If I work in a team, I have to make sure that my code is readable and understandable for all team members so that collaboration can work smoothly. There’s nothing worse than when the next code review only raises questions about the code you’ve written.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example: Meaningful and readable variables&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Difficult&lt;/p&gt;

&lt;p&gt;“foo”, for example, is often used as a placeholder in programming if you don’t have a suitable variable name to hand. Make sure you avoid this at the productive level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = moment().format('DD.MM.YYYY');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Better&lt;/p&gt;

&lt;p&gt;Use names that are meaningful and that people know immediately what they are talking about. Programming languages are international, so everyone should be able to read your code, so make sure you use English words for your variables. This applies not only to variables, but also to functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CURRENT_DATE = moment().format('DD.MM.YYYY');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Maintainability
&lt;/h3&gt;

&lt;p&gt;Another point is the &lt;strong&gt;maintainability&lt;/strong&gt; of my written code. The more complex the code, the more time-consuming maintainability becomes, as it becomes more difficult to identify and rectify errors. If the requirements change, it is also guaranteed that every team member can find and change the part to be changed more quickly. Maintainability is essential for large projects. Due to the large amount of source code, you can quickly lose track of things and no longer find your way around your own code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example: Searchable variable names&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Difficult&lt;/p&gt;

&lt;p&gt;In this example, it is not clear what this high number means. Make sure you give it a searchable name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setInterval(close, 86400000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Better&lt;/p&gt;

&lt;p&gt;Constants, in this case “const”, allow you to assign fixed and non-overwritable variables. Perfect for the fixed milliseconds per day. These constants are always capitalized and written in snake case. Always name them so that you can find them again in the future.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MILLISECONDS_PER_DAY = 86400000;
setInterval(close, MILLISECONDS_PER_DAY);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example: Array methods&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Difficult&lt;/p&gt;

&lt;p&gt;In my opinion, the For loop is one of the most frequently used functions in JavaScript and, as you can see from its structure, it is also very error-prone. In addition, the For loop also returns the empty fields in an array. This can lead to confusion, as you always have to check carefully that no errors have occurred during iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const studentsGrades = [3, , 1, 2, , 2, 5];

for (i = 0; i &amp;lt; studentsGrades.length; i++) {
  console.log(studentsGrades[i]);
}

//Output: 3, undefined, 1, 2, undefined, 2, 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Better&lt;/p&gt;

&lt;p&gt;If you use the predefined array methods, the “forEach” function is the best choice for the same example. This shortens the regular For loop and also automatically does not output the empty fields. The readability of the code also plays a very important role here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const studentsGrades = [3, , 1, 2, , 2, 5];

studentsGrades.forEach((studentGrade) =&amp;gt; {
  console.log(studentGrade);
});

//Output: 3, 1, 2, 2, 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Professionalism
&lt;/h3&gt;

&lt;p&gt;In my opinion, writing clean code also demonstrates &lt;strong&gt;professionalism&lt;/strong&gt;. Those who value high quality also show this in their code and at the same time take care of high-quality software. Clean code therefore also acts as a business card.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Writing clean code not only helps you, but also your team and is essential for every software developer. The examples above are based on my experience and are only intended as a guide. It is only a fraction of what is possible with clean code.&lt;/p&gt;

&lt;p&gt;Writing clean code should not be a one-time process. You should want to continuously improve and simplify your code so that it always remains readable and maintainable. The more often you deal with the topic, the better and faster you will write your code in the future and the easier it will be to collaborate with your team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further links:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/ryanmcdermott/clean-code-javascript"&gt;Further examples of clean code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://khalilstemmler.com/blogs/camel-case-snake-case-pascal-case/"&gt;Further information on camelCase vs. Snake_Case vs. PascalCase&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>cleancode</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
