Secrets of coding style and good commenting

Nusrat Jahan Nisha
3 min readNov 3, 2020

Any fool can write code that a computer can understand. Good programmers write code that humans can understand too. So, we should try to write good and clean code so that any human can understand it. Today I am going to teach you some secrets about coding style and commenting to improve your coding quality.

Curly Braces

  1. You should not do it. If you want to write an if conditions in one line, you can try that without curly braces.
if (i > 5) {alert(“Bad Practice”);}

2. Like this. Simple but effective

if (n < 0) alert(“Good Practice”);

3. Don’t try to use that. You’ll get an error.

if (i > 5)
alert(“Bad Practice.”);

4. Great practice will be like this.

  • Give space after writing if.
  • Put a gap after the condition.
  • Use curly braces in a separate line.
  • After using the keyword(return, alert) give a space.
if (i > 5) {
alert (“Great Practice”);
}

Line length

No one likes to scroll and read a long horizontal line of code. Best practice will be to divide the long line into multiple short lines so that anyone can read it easily.

const badPractice= "A computer is like a violin. You can imagine a novice trying first a phonograph and then a violin. The latter, he says, sounds terrible. That is the argument we have heard from our humanists and most of our computer scientists. Computer programs are good, they say, for particular purposes, but they aren’t flexible. Neither is a violin, or a typewriter, until you learn how to use it.";const goodPractice = "A computer is like a violin.
You can imagine a novice trying first a phonograph and then a violin.
The latter, he says, sounds terrible.
That is the argument we have heard from our humanists and most of our computer scientists.
Computer programs are good, they say, for particular purposes, but they aren’t flexible.
Neither is a violin, or a typewriter, until you learn how to use it. ";

Semicolons

For every programmer, if they find a bug, then the main suspect will be a semicolon. For javascript, it is a life and death situation. You can’t imagine your code without the semicolon .You’ll have to put a semicolon in every line. Otherwise, you’ll get an error.
Bonus => Don’t treat a semicolon as an end of the line but rather as a possibility of a new beginning.

Function

  1. You can call the function before the actual function.
const mark = 34;
result(mark);
function result(n) {
if (n > 33) {
console.log("Good Practice. You have passed the exam.");
}
else {
console.log("You have failed the exam");
}
}

2. You can call the function after the actual function is written.

function result(n) {
if (n > 33) {
console.log("Good Practice. You have passed the exam.");
}
else {
console.log("You have failed the exam");
}
}
const mark = 34;
result(mark);

Both the method is good practice. But programmers mostly use the first one.

Comments

There is a saying that => Good code is its own best documentation. As you are about to add a comment, ask yourself, “How can I improve the code so that this comment isn’t needed.”

But yet if you want to add a comment to your code, you can follow some rules.

  1. Don’t write a long description of your code. Write to make it short and simple.
  2. Try to write the function in this way so that commenting is not needed.
  • Not like this one.
//This is a leap year functionfunction number() {console.log("Bad practice");}
  • If you name the function isLeapYear, then everyone will understand it is a function of a leap year. You don’t have to write a comment.
function isLeapYear() {console.log("Good practice");}

--

--