As we know from the chapter Code structure, comments can be single-line: starting with // and multiline: /* ... */.
We normally use them to describe how and why the code works.
At first sight, commenting might be obvious, but novices in programming often use them wrongly.
Bad comments
Novices tend to use comments to explain âwhat is going on in the codeâ. Like this:
// This code will do this thing (...) and that thing (...)
// ...and who knows what else...
very;
complex;
code;
But in good code, the amount of such âexplanatoryâ comments should be minimal. Seriously, the code should be easy to understand without them.
Thereâs a great rule about that: âif the code is so unclear that it requires a comment, then maybe it should be rewritten insteadâ.
Recipe: factor out functions
Sometimes itâs beneficial to replace a code piece with a function, like here:
function showPrimes(n) {
nextPrime:
for (let i = 2; i < n; i++) {
// check if i is a prime number
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
alert(i);
}
}
The better variant, with a factored out function isPrime:
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(n) {
for (let i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
Now we can understand the code easily. The function itself becomes the comment. Such code is called self-descriptive.
Recipe: create functions
And if we have a long âcode sheetâ like this:
// here we add whiskey
for(let i = 0; i < 10; i++) {
let drop = getWhiskey();
smell(drop);
add(drop, glass);
}
// here we add juice
for(let t = 0; t < 3; t++) {
let tomato = getTomato();
examine(tomato);
let juice = press(tomato);
add(juice, glass);
}
// ...
Then it might be a better variant to refactor it into functions like:
addWhiskey(glass);
addJuice(glass);
function addWhiskey(container) {
for(let i = 0; i < 10; i++) {
let drop = getWhiskey();
//...
}
}
function addJuice(container) {
for(let t = 0; t < 3; t++) {
let tomato = getTomato();
//...
}
}
Once again, functions themselves tell whatâs going on. Thereâs nothing to comment. And also the code structure is better when split. Itâs clear what every function does, what it takes and what it returns.
In reality, we canât totally avoid âexplanatoryâ comments. There are complex algorithms. And there are smart âtweaksâ for purposes of optimization. But generally we should try to keep the code simple and self-descriptive.
Good comments
So, explanatory comments are usually bad. Which comments are good?
- Describe the architecture
- Provide a high-level overview of components, how they interact, whatâs the control flow in various situations⦠In short â the birdâs eye view of the code. Thereâs a special language UML to build high-level architecture diagrams explaining the code. Definitely worth studying.
- Document function parameters and usage
- Thereâs a special syntax JSDoc to document a function: usage, parameters, returned value.
For instance:
/**
* Returns x raised to the n-th power.
*
* @param {number} x The number to raise.
* @param {number} n The power, must be a natural number.
* @return {number} x raised to the n-th power.
*/
function pow(x, n) {
...
}
Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.
By the way, many editors like WebStorm can understand them as well and use them to provide autocomplete and some automatic code-checking.
Also, there are tools like JSDoc 3 that can generate HTML-documentation from the comments. You can read more information about JSDoc at https://jsdoc.app.
- Why is the task solved this way?
-
Whatâs written is important. But whatâs not written may be even more important to understand whatâs going on. Why is the task solved exactly this way? The code gives no answer.
If there are many ways to solve the task, why this one? Especially when itâs not the most obvious one.
Without such comments the following situation is possible:
- You (or your colleague) open the code written some time ago, and see that itâs âsuboptimalâ.
- You think: âHow stupid I was then, and how much smarter Iâm nowâ, and rewrite using the âmore obvious and correctâ variant.
- â¦The urge to rewrite was good. But in the process you see that the âmore obviousâ solution is actually lacking. You even dimly remember why, because you already tried it long ago. You revert to the correct variant, but the time was wasted.
Comments that explain the solution are very important. They help to continue development the right way.
- Any subtle features of the code? Where they are used?
-
If the code has anything subtle and counter-intuitive, itâs definitely worth commenting.
Summary
An important sign of a good developer is comments: their presence and even their absence.
Good comments allow us to maintain the code well, come back to it after a delay and use it more effectively.
Comment this:
- Overall architecture, high-level view.
- Function usage.
- Important solutions, especially when not immediately obvious.
Avoid comments:
- That tell âhow code worksâ and âwhat it doesâ.
- Put them in only if itâs impossible to make the code so simple and self-descriptive that it doesnât require them.
Comments are also used for auto-documenting tools like JSDoc3: they read them and generate HTML-docs (or docs in another format).
Comments
<code>tag, for several lines â wrap them in<pre>tag, for more than 10 lines â use a sandbox (plnkr, jsbin, codepenâ¦)