# 4. Commenting on obvious things

Most comments can be replaced with names of variables and functions. Before you write a comment, remember this.

For example, code like this:<br>

```
// This function sums only odd numbers in an array
const sum = (val) => {
  return val.reduce((a, b) => {
    if (b % 2 === 1) { // If the current number is odd
      a+=b;            // Add current number to accumulator
    }
    return a;          // The accumulator
  }, 0);
};
```

\
You can replace it with this:<br>

```
const sumOddValues = (array) => {
  return array.reduce((accumulator, currentNumber) => {
    if (isOdd(currentNumber)) { 
      return accumulator + currentNumber;
    }
    return accumulator;
  }, 0);
};
```

\
But sometimes you can only clarify the code with a comment. In that case, focus on the question: "WHY this code is needed," not "WHAT this code does. Here's an example of code where comments only clutter up the code:<br>

```
// create a variable and initialize it to 0
let sum = 0;
// Loop over array
array.forEach(
  // For each number in the array
  (number) => {
    // Add the current number to the sum variable
    sum += number;
  }
);
```

\
Don't do that if you are a programmer. And if you are the employer of such programmers, fire them right now.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tester-ai.gitbook.io/en/12.-developers-mistakes/4.-commenting-on-obvious-things.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
