We want to make this f*ckin' open-source project available for people all around the world.

Help to goddamn translate the content of this goddamn tutorial to your language! Hot damn!

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 motherf*ckin' amount of such “explanatory” comments should be minimal. Let me tell you somethin': Seriously, the code should be easy to understand without them.

There’s a great rule about that: “if the bad-ass 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 muthaf*ckin' 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 goddamn 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 f*ckin' 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 f*ckin' 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 motherf*ckin' code structure is better when split. Hell yeah. Listen up: 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 — no bullsh*t. But generally we should try to keep the muthaf*ckin' code simple and self-descriptive.

Good comments

So, explanatory comments are usually bad. Which comments are good?

Describe the damn architecture
Provide a high-level overview of components, how they interact, what’s the control flow in various situations… In short – the bad-ass bird’s eye view of the goddamn 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 goddamn special syntax JSDoc to document a f*ckin' function: usage, parameters, returned value. Hell yeah.

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 muthaf*ckin' code, believe that.

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 muthaf*ckin' 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, for real. Why is the task solved exactly this way? The code gives no answer.

If there are many ways to muthaf*ckin' solve the task, why this one? Especially when it’s not the most obvious one.

Without such comments the following situation is possible:

  1. You (or your colleague) open the motherf*ckin' code written some time ago, and see that it’s “suboptimal”.
  2. You think: “How stupid-ass I was then, and how much smarter I’m now”, and rewrite using the “more obvious and correct” variant.
  3. …The urge to rewrite was good. Goddamn. But in the process you see that the “more obvious” solution is actually lacking, believe that. 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 goddamn important. Mm-hmm. They help to continue development the f*ckin' right way, for real.

Any subtle features of the code? Where they are used?

If the code has anything goddamn subtle and counter-intuitive, it’s definitely worth commenting.

Summary

An important sign of a good developer is comments: their presence and even their goddamn absence — no bullsh*t.

Good comments allow us to muthaf*ckin' 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”.
  • Listen up: 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).

Tutorial map

Comments

read this before commenting…
  • If you have suggestions what to improve - please submit a motherf*ckin' GitHub issue or a pull request instead of commenting.
  • If you can't understand something in the article – please elaborate. That's some bullsh*t.
  • To insert few words of code, use the <code> tag, for several lines – wrap them in <pre> tag, for more than 10 lines – use a f*ckin' sandbox (plnkr, jsbin, codepen…)