Art of styling code you all need to know

Amina Anika
4 min readMay 6, 2021

Semicolons

To use them, or not to use them…

Nowadays freshers are getting confused about whether they will use semicolons or not!! Because some people decided to avoid semicolons in javascript. But a semicolon should be present after each statement, even if it could possibly be skipped. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors.

If

When we use if condition we should make sure that have spaces in them if condition ( a <= 0) and should avoid an extra line for the curly braces.

Bad Style
Good Style

else

When coding many people write else condition in the next line, but standard coding is written in the same line.

Bad Style
Good Style

Function

In our code, we often use several functions to make our website. Most of the case we declare the functions above the code that uses them. But standard style is code first, then call the functions. Because when reading code, we first want to know what it does. If the code goes first, then it becomes clear from the start. Then, maybe we won’t need to read the functions at all, especially if their names are descriptive of what they actually do.

Declare the functions above the code
Code first, then helper functions

Line Length

When we use comments or any string text make sure that it will be split. Because no one likes to read a long horizontal line of code. So, best practice to split them.

Good Comments

Commenting is always a good practice for developers. An important sign for a developer is commenting. Every good developer uses some tricks to do comments. They know that good comments maintain a developer to make code readable. Every junior developer should follow these rules. They should provide a high overview of code, give an explanation of how and why it works.

Syntax

Sometimes we use comments wrongly. Obviously commenting is a good practice but before applying we have to know about avoiding the wrong method of comments.

You have to follow 3 types of syntax: Single line, inline, and multi-line or block-level comments.

We write these comments with two forward slashes //:

// Single line comment

When we write single-line comments at end of the code-line, it’s called an inline comment.

let s = 'Progamming Hero!'; // assign string literal to variable s

Block-level comments are written with opening tags /* and closing tags */:

/*
Line 1
Line 2
*/

Level of detail

Not every time it is required to write a comment. So, let’s take a look below:

let todaysDate = new Date();

In this code, We can easily understand that today’s date is assigned to a variable named todaysDate. Best practices for comments should be practiced by following development guidelines. The proper use of comments in code varies from developer to developer, some can be informal, while others follow formal guidelines to make code readable.

Self documenting

Code should be self-documenting. This is also one way to making your code easier to understand. I think self-documenting code comes with practice and a level of expertise is also required. What should be the maximum length of a variable/function name also becomes a necessity is self-documenting.

Self-documenting code is written using human-readable names, consisting of a phrase in a human language which reflects the symbol’s meaning, such as article.

The code must also have a clear and clean structure so that a human reader can easily understand the algorithm used. It totally depends on a developer (and/or maybe team/company).

Importance

We all know that reading and understanding is much more difficult than writing code. And that’s why comments are always very useful when it comes to understanding another developer’s code. And believe me, it’s not just only for other developers, it’s for the future yourself, too. After looking at our own code after a couple of months, even we sometimes are not sure why we wrote. That’s why good developers use comments and they follow guidelines.

Finally, I would say that,

In developer’s world, a comment is a programmer-readable explanation in the source code of a computer program.

We saw good comments, syntax, and editor self-documenting for comments in your code. In my experience, it’s always better to use a combination of both, self-documenting code and comments to make your code more readable and dev-friendly.

Thanks for reading this article. See you in the next article.

--

--

Amina Anika

I am a front-end developer. I love to know about new technologies.