Secrets of coding style and good commenting

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”);}
if (n < 0) alert(“Good Practice”);
if (i > 5)
alert(“Bad Practice.”);
  • 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

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

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");
}
}
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);

Comments

  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");}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store