22
loading...
This website collects cookies to deliver better user experience
The proper use of comments is to compensate for our failure to express ourself in the code.
// Copyright (C) 2020 by Foo Inc. All rights reserved
// return toggleState for accordion
const toggle = () => ....
const isAccordionOpen = () => ...
Explanation of Intent: Comment beyond the implementation, reveals the intent behind the decision.
For example: This can be used to specify why certain approach are used according to the use case.
Clarification: These comments are helpful to translate some obscure arguments or return values into a more readable form.
import * as _ from "lodash";
// divide array into groups of 2
const arr = [1, 2, 3, 4, 5];
const splitArray = _.chunk(arr, 2);
// not the optimal approach to perform the task
const getTask = () => ...
TODO comments: The comments may serve as a reminder to add or delete some code, or to clean them up in some way.
Emphasis: Comments used to emphasise the importance of some code. It shows that the code is important so that people won’t overlook it.
JSDoc comments: When writing libraries, documentation or code is served to the masses that use them. This can be done with JSDoc by writing comments in the code, which will generate documentation from it.
/**
* Represents a Person.
* @constructor
* @param {string} name - The name of the person.
* @param {number} age - The age of the person.
*/
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
Mumbling: Hastily plopping a comment, not paying much attention to the details does not serve much purpose.
Redundant Comments: As the name suggests, it does not serve any purpose.
Misleading comments: These comments for statements are not precise enough to be accurate.
Journal Comments: Comment added to the start of the module every time it is edited leads to overhead. A CHANGELOG should be used for these purposes.
Don't Comment bad code, rather rewrite it.