Best Practices
A free lesson from Swipe Coding · C++, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
You now know both types of comments in C++: // and /* */. But just as important as the syntax is knowing when and how to use comments effectively. Here are the most important rules for good comments.
Rule 1: Explain the WHY, not the WHAT. Good code is often already self-explanatory — what it does is visible from the code itself. A comment is usually only helpful when it explains the reason behind a decision.
// Bad: just repeats the code // prints Hello std::cout << "Hello" << std::endl; // Good: explains the reason // Greeting shown the first time the app launches std::cout << "Hello" << std::endl;
The comment "// prints Hello" above std::cout << "Hello" << std::endl; is an example of a good comment.
Rule 2: Keep comments up to date. When the code changes but the comment doesn't, you end up with an outdated comment. An outdated comment is often worse than no comment at all — it misleads others (and yourself).
// Prints the user's first name std::cout << "Welcome back!" << std::endl;
A comment that no longer matches the current code is called ____.
Rule 3: Don't over-comment. If every single, obvious line gets its own comment, the code becomes more cluttered instead of clearer. Use comments purposefully: to mark sections or to explain truly tricky parts.
// Bad: a comment for every single, obvious line
int main() {
// prints text
std::cout << "Hello" << std::endl;
return 0;
}
// Good: a comment marks only the important section
int main() {
// --- Greeting ---
std::cout << "Hello!" << std::endl;
std::cout << "Glad you're here." << std::endl;
return 0;
}When is a comment most useful?
Comments
10 questions · pass with 60% — just like in the app.
What are comments in C++ mainly intended for?
Comments are compiled and executed by the C++ compiler just like normal code.
A single-line comment in C++ begins with the characters ____.
What does this code print?
std::cout << "A" << std::endl; // std::cout << "B" << std::endl; std::cout << "C" << std::endl;
A // comment automatically continues onto the next line of code as well.
A multi-line comment in C++ begins with /* and ends with ____.
What happens if you open a /* comment but forget to close it with */?
The comment "// prints Hello" written above std::cout << "Hello" << std::endl; is an example of a good comment.
A comment that no longer matches the code it describes is called ____.
When is a comment most useful?
Keep your streak going in Swipe Coding
Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →