<?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: Sujan Maharjan</title>
    <description>The latest articles on DEV Community by Sujan Maharjan (@rasujan).</description>
    <link>https://dev.to/rasujan</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%2F580638%2Fa6904244-986f-4987-aa66-d829fb89bf9e.jpeg</url>
      <title>DEV Community: Sujan Maharjan</title>
      <link>https://dev.to/rasujan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rasujan"/>
    <language>en</language>
    <item>
      <title>JavaScript Clean Codes</title>
      <dc:creator>Sujan Maharjan</dc:creator>
      <pubDate>Mon, 26 Apr 2021 05:06:16 +0000</pubDate>
      <link>https://dev.to/rasujan/javascript-clean-codes-i31</link>
      <guid>https://dev.to/rasujan/javascript-clean-codes-i31</guid>
      <description>&lt;h2&gt;
  
  
  Originally By Codevolution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Avoid magic numbers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (password.length &amp;lt; 8) {
    // Display error message
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MIN_PASSWORD_LENGTH = 8
if (password.length &amp;lt; MIN_PASSWORD_LENGTH) {
    // Display error message
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Avoid additional context
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const employee = {
    employeeName: 'John Doe',
    employeeAge: 26,
    employeeSalary: 40000
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const employee = {
    name: 'John Doe',
    age: 26,
    salary: 40000
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Use default arguments instead of short-circuiting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// All products receive 10% discount
function calculateDiscount(discount){
    const minDiscount = discount || 10
    // ...Rest of the function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// All products receive 10% discount
function calculateDiscount(discount = 10){
    const minDiscount = discount
    // ...Rest of the function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Restrict function arguments to 3
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function toastNotification('Login Successful', duration, style, className, icon, role)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const options = {
    duration: 3000,
    style: {},
    className: '',
    icon: '🙂',
    role: 'status'
}

function toastNotification('Login Successful`, options)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Functions should do one thing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function notifyUsers(users) {
    users.forEach(user =&amp;gt;{
        const userRecord = database.lookup(user)
        if (userRecord.isSubscribed()){
            notify(user)
        }
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function notifySubscribedUsers(users) {
    users.filter(isSubscribedUser).forEach(notify)
}

function isSubscribedUser(user){
    const userRecord = database.lookup(user)
    return userRecord.isSubscribed()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Avoid boolean flags as parameters
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getitemCost(itemCost, isMember){
    const MEMBER_DISCOUNT = 0.30
    const NORMAL_DISCOUNT = 0.10
    let cost = 0
    if(isMember){
        cost = itemCost * (1 - MEMBER_DISCOUNT)
    } else {
        cost = itemCost * (1 - NORMAL_DISCOUNT)
    }
    return cost
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getItemCost(itemCost) {
    const NORMAL_DISCOUNT = 0.10
    let cost = itemCost * (1- NORMAL_DISCOUNT)
    return cost
}

function getItemCostForMember(itemCost){
     const MEMBER_DISCOUNT = 0.10
    let cost = itemCost * (1- MEMBER_DISCOUNT)
    return cost
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Encapsulate Conditionals
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(status === "loading" &amp;amp;&amp;amp; isEmpty(produtionList)){
    // ...Rest of the code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function shouldDisplayLoader (status, productList){
    return status === "loading" &amp;amp;&amp;amp; isEmpty (productList);
}

if (shouldDisplayLoader(requestStatus, productList)){
    // ...Rest of the code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Avoid contractions when naming
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const curColor = "#333"
function sndNotif() {
}
function onBtnClk() {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const currentColor() {

}
function sendNotification(){}
function onButtonClick(){}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. Use prefix when naming boolean variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const LoggedIn = true
const followers = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isLoggedIn = true
const hasFollowers = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. Use a verb when naming functions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don't do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fullName(firstName, lastName){
    this.fullName = `${firstName} ${lastName}`
}
function fullName(){
    return `${firstName} ${lastName}
}
function count(){
    this.count = this.initialCount
}
function async products(){
    const products = await fetch('/api/products')
    return products
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setFullName(firstName, lastName){
    this.fullName = `${firstName} ${lastName}`
}
function getFullName(){
    return `${firstName} ${lastName}
}
function resetCount(){
    this.count = this.initialCount
}
function async fetchProducts(){
    const products = await fetch('/api/products')
    return products
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Link to the original content: &lt;a href="https://youtu.be/vPXzVNmCPg4"&gt;https://youtu.be/vPXzVNmCPg4&lt;/a&gt;&lt;/p&gt;

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