Write code for humans, not machines only 🤓

JSwL: Episode 4

·

2 min read

Comments are essential if you want anyone to understand your code. But here's the interesting part: they help you too.

If you leave a project for a period of time, you can quickly ascertain what each function or statement does with proper comments.

Comments do not have any negative impact on your code, as there are many tools which minify code before publishing to a production server.

A side note: more comments are usually better than fewer, but don't go into explaining a variable assignment. Your variable name should be intuitive.

Here are 3 quotes:

“As time goes on, programs become more and more complex. It becomes necessary to add comments which describe what the code does and why.

Comments can be put into any place of a script. They don’t affect its execution because the engine simply ignores them.

Please, don’t hesitate to comment your code.” - JS info

“it is possible to write comments into your JavaScript code that will be ignored by the browser, and exist to provide instructions to your fellow developers on how the code works (and you, if you come back to your code after six months and can't remember what you did). Comments are very useful, and you should use them often, particularly for larger applications.” - MDN docs

“Code is every bit as much, if not more, for the developer as it is for the compiler.

You should strive not just to write programs that work correctly, but programs that make sense when examined.

These are bits of text in your program that are inserted purely to explain things to a human. The interpreter/compiler will always ignore these comments.” - Kyle Simpson

2 code samples

// sample 1
// This comment occupies a line of its own
alert('Hello');

alert('World'); 
// This comment follows the statement

// sample 2
/* An example with two messages.
This is a multiline comment.
*/
alert('Hello');
alert('World');

1 exercise

function multiply(num) {
    return num * 2
}

var a = 2
var b = a + 2
var c = multiply(b)
console.log(c) // output?

That's a wrap! see you on the next one.

Connect with me on Twitter.

Â