A brief look into code quality
Tags: codequality, javascript, cleancode
Published: 2020-02-19 06:22:08

---
title: A brief look into code quality
published: true
description: An intro to clean code 
tags: code-quality, javascript, clean-code
cover_image: https://anjanak.com/content/images/size/w2000/2020/02/coding-computer-data-depth-of-field-577585.jpg
---

When we are writing code, it should be human-readable and clean. This article is about some basics of writing readable and clean code.

###Syntax

![syntax](https://lh6.googleusercontent.com/ksPSIwKnG0CkdPKkORMnwtGoeXMbCGXyUONk1u8uRkWYi4_La3xFvP2dTxASCTioIxJTOtPDm7f5EczL_sksOGtNbTpdrymVNsiV1Xbt2DAMg4ou8n2rCs-uoPS4wwnpP6Gq2G73)

Now let’s discuss these syntax and best practices in detail.

###Curly Braces

```
If (condition) {
    // do this
    // do this
}
```
In JavaScript language curly braces are written with the opening bracket on the same line not on a new line and there should be a space before the opening curly bracket as above example.

- Curly braces are not needed for a single-line construct.

```
if (i <= 0) {alert(`It should not be ${i}`);}
```
- If it is short, we can use it without curly braces.

```
if (i <= 0) return null;
```

- Don’t split to a separate line without braces.

```
if (i <= 0) 
    alert(`It should not be ${i}`);
```

- The best way. This is usually more readable.

```
if (i <= 0) {
    alert(`It should not be ${i}`);
}
```

###Semicolon

The majority of developers put semicolons after each statement but it is optional and rarely used. In JavaScript there are problems where a line break is not interpreted as  a semicolon, going the code vulnerable to errors.

```
let sum = a + b;
```

###Indents

- Horizontal indents  

    2 or 4 spaces( 4 spaces = key Tab). For instance, to align the arguments with the opening bracket.              

```
function fruit(banana,
       mango,
       avocado 
    ) {
        
}
```
- Vertical indents
    Function can be divided into logical blocks. In the below example appears three vertically split. First one is initialization of variables, the second one main loop and the third one returning the result.

```
function sum(a) {
    let total = 0;
  
    while(a > 0) {
      total += a;
    }
    
    return total;
  }
```
  If we want to make the code more readable we can add extra newline.

###Line Length

Don’t write long horizontal lines of code because no one prefers to read it. Back tick quotes(&#96;&#96;) help to split the long string into multiple lines.

```
let pra = `
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
  Integer eu convallis sem. 
  Praesent in facilisis ligula. 
  Curabitur iaculis metus lacus, vitae dapibus odio iaculis vitae. 
  Morbi imperdiet ultricies tortor ac dignissim. 
  Quisque at mi a purus dignissim tincidunt eu eu ipsum. 
  Phasellus pharetra vitae neque id fermentum.
`;

```

Same as the if statement. For example,

```
if (
  id === 10001 &&
  name === 'Anjana Kumari' &&
  address === 'Kandy'
) {
  login();
}
```

The maximum line length is usually 80 or 120 characters but it will depend upon the team-level.

###Nesting Level

If we can use `continue` directive, we can easily avoid extra nesting. For example,

```
while (i < 10) {
  if (!condition) continue;
  // do that
}
```

###Style Guides

Style guides includes how to write code. If we work as a team we can use the same style guide. Then the code can see uniform. Also teams can use their own style guide too. The popular style guides are Google JavaScript Style Guide and StandardJS etc.

###Automated Linters

Automated Linter tools can automatically detect the style of our code and make suggestions. Also, the linter tools can detect typos in variable names and function names. Some of the popular linting tools are JSLint, JSHint, and ESHint.

###Summary

Carly Braces, Semicolons, Indents, Line Length and Nesting Levels rules described aim to increase the readability of our code.

We should always think about writing better code. There are two things to keep mind when we write codes. First one is  what makes the code more readable and easier to understand? And the second one is what can help us avoid errors?

Finally, we should be up to date with reading the most popular style guides.

Original post in my [personal blog](https://anjanak.com/a-brief-look-into-code-quality/)