<?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: Taofeek Ibrahim Opeyemi</title>
    <description>The latest articles on DEV Community by Taofeek Ibrahim Opeyemi (@codingalgorithm).</description>
    <link>https://dev.to/codingalgorithm</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%2F350029%2F11b05033-c920-4b59-91ea-95e644df768e.jpg</url>
      <title>DEV Community: Taofeek Ibrahim Opeyemi</title>
      <link>https://dev.to/codingalgorithm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingalgorithm"/>
    <language>en</language>
    <item>
      <title>Clean Code (2): Stability</title>
      <dc:creator>Taofeek Ibrahim Opeyemi</dc:creator>
      <pubDate>Mon, 19 Dec 2022 05:32:26 +0000</pubDate>
      <link>https://dev.to/codingalgorithm/clean-code-2-stability-5d01</link>
      <guid>https://dev.to/codingalgorithm/clean-code-2-stability-5d01</guid>
      <description>&lt;h2&gt;
  
  
  TABLE OF CONTENTS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What is Stability in Clean Code&lt;/li&gt;
&lt;li&gt;Why You Should Write A Stable Code&lt;/li&gt;
&lt;li&gt;Unstable Code + Effect&lt;/li&gt;
&lt;li&gt;Improving Unstable Code&lt;/li&gt;
&lt;li&gt;How To Achieve Stability&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Some weeks ago, I started writing about clean code.&lt;/p&gt;

&lt;p&gt;This is the second episode of my clean code series, in the first part, I talk about &lt;a href="https://codingalgorithm.hashnode.dev/clean-code-correctness"&gt;correctness in clean code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Today, I will be discussing code stability.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Stability in Clean Code
&lt;/h2&gt;

&lt;p&gt;Stability has different literal definition.&lt;/p&gt;

&lt;p&gt;Who you ask would determine what type of definition you will get.&lt;/p&gt;

&lt;p&gt;However, if you study these answers well, you will realized they are all referring to the same fact.&lt;/p&gt;

&lt;p&gt;Today and here, I will give you some definitions which would give you basic understanding of what stability is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Stability is when a code is being tested under different valid inputs, scenarios yet still produce correct output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stability occurs when you have no need to modify your code anymore.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stability occurs when your code is free from both major and minor bugs. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You probably have got to a software download page where you are given the option to download either the latest (beta) version or a stable version.&lt;/p&gt;

&lt;p&gt;The beta version works but doesn't have the assurance that you will always get the right performance.&lt;/p&gt;

&lt;p&gt;However, the stable version has undergone series of tests, modifications and improvements thus is guaranteed of performing well under different circumstances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why You Should Write A Stable Code
&lt;/h2&gt;

&lt;p&gt;Just like I have explained above, a stable software is more reliable, likewise a stable code.&lt;/p&gt;

&lt;p&gt;If you are looking for practical example or reason why you should write stable code, here is one:&lt;/p&gt;

&lt;h2&gt;
  
  
  Unstable Code + Effect
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Unstable Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const balance = "234"
const amountToTransfer = "1000"
if(balance &amp;lt; amountToTransfer){
  console.log("Insufficient balance")
  // return and do not perform transaction
}else{
  console.log("Transfer made")
  // perform transaction
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Effects Of Unstable Code
&lt;/h3&gt;

&lt;p&gt;Judging from the apparent view of the code, nothing seems wrong. &lt;/p&gt;

&lt;p&gt;We are comparing these two variables and executing tasks based on the result of the evaluation. &lt;/p&gt;

&lt;p&gt;However, upon running the code, we found out that the code is not executing the way we had expected.&lt;/p&gt;

&lt;p&gt;This could cause financial audit problem, given the fact that the "amountToTransfer" could be more than this.&lt;/p&gt;

&lt;p&gt;The "balance" is obviously smaller than the "amountToTransfer" but the code isn't interpreting this correctly.&lt;/p&gt;

&lt;p&gt;Yeah. &lt;/p&gt;

&lt;p&gt;The issue is with our data type.&lt;/p&gt;

&lt;p&gt;We are comparing strings not numbers.&lt;/p&gt;

&lt;p&gt;If you are comparing strings directly in place of number, JavaScript do not convert them to number implicitly.&lt;/p&gt;

&lt;p&gt;JavaScript treats each number in the variables as a character and finds its equivalence in the unicode character table.&lt;/p&gt;

&lt;p&gt;After comparing there equivalence unicode value, JavaScript returns false for the evaluation thus, running the false block of the if/else control system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Improving Unstable Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const balance = parseInt("234")
const amountToTransfer = parseInt("1000") 

if(balance &amp;lt; amountToTransfer){
  console.log("Insufficient balance")
}else{
  console.log("Transfer made")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; After testing our code and finding out the instability there in, we have decided to convert both variable explicitly to integer before comparing them.&lt;/p&gt;

&lt;p&gt;However, you should also make sure from the start that your "balance" or "amountToTransfer" will always be in integer". &lt;/p&gt;

&lt;p&gt;Also, we all know that bank do charge in "kobo" in Nigeria, "cent" in US and some other non-integer value in your country.&lt;/p&gt;

&lt;p&gt;You will have to go with the correct data type for each variable before using it, might be float, double etc...&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Achieve Stability
&lt;/h2&gt;

&lt;p&gt;The process is simple, but may not be easy to accomplish.&lt;/p&gt;

&lt;p&gt;TEST. &lt;/p&gt;

&lt;p&gt;When you are done with development, test your code, get a group of beta testers and get feedback from them.&lt;/p&gt;

&lt;p&gt;After the feedback, test again. Always test.&lt;/p&gt;

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

&lt;p&gt;Stability is an important part of clean code, it ensures that your code is working correctly given different input or experiencing different cases.&lt;/p&gt;

&lt;p&gt;Thanks for reading through, hope you learnt one or two thing.&lt;/p&gt;

&lt;p&gt;I will like to hear your opinions on this...&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
