Comments in JavaScript
Code Quality: Comments
How do you reduce the number of unnecessary comments in your code?
View Answer:
Interview Response: The key to reducing unnecessary comments in your code is to write self-descriptive code. The best way to achieve this is to replace a chunk of code with a function.
Code Example: Confusing Code
function showPrimes(n) {
nextPrime: for (let i = 2; i < n; i++) {
// check if i is a prime number <-- this is an unnecessary comment
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
alert(i);
}
}
Code Example: Good Code
// 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;
}
What are Good comments in JavaScript?
View Answer:
Interview Response: Good comments describe the architecture and lean less toward explaining what the code is doing. Good comments provide the team with a high-level overview of the components and how they interact. Good comments give a bird's eye view of the code. Good comments include documenting function parameters and their usage as well.
Code Example:
/**
* 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) {
...
}