Comments
Comments are notes in your source code that the compiler completely ignores. They exist purely for humans — you, and anyone else who reads your code later — to understand what’s going on.
Single-line comments
A double slash // comments out everything from that point to the end of the line.
int score = 0; // tracks the player's current score
Multi-line comments
Text wrapped between /* and */ can span multiple lines, which is useful for longer explanations or temporarily disabling a block of code.
/*
This function calculates the average of all
scores in the provided list and returns it
as a floating-point value.
*/
double average(const std::vector<int>& scores) {
// ...
}When to comment
The most valuable comments explain why something is done, not what is being done — the “what” should usually be clear from well-named variables and functions.
Explain non-obvious business logic or a tricky algorithm's intent.
Document why a workaround exists, especially for a compiler quirk or platform-specific bug.
Flag unfinished work with a consistent marker like
// TODO:so it's easy to search for later.Avoid restating exactly what the next line already says in plain English.
Documentation comments
/** * Calculates the average of a list of scores. * * @param scores A list of integer scores. Must not be empty. * @return The arithmetic mean as a double. */ double average(const std::vector<int>& scores);
Good vs bad commenting
Not useful — restates the obvious
// increment i by 1 i++; // set total to 0 int total = 0;
Useful — explains intent
// Skip index 0: it's a sentinel value reserved by the file format.
for (int i = 1; i < entries.size(); i++) {
process(entries[i]);
}