What are Comments?

Comments are text in your program that the compiler ignores. They're used to:

  • Document what your code does
  • Leave notes for yourself and other programmers
  • Temporarily disable code during debugging

Types of Comments

Single-line Comments

Use // for comments that span one line:

// This is a single-line comment
int age = 25;  // You can also put comments at the end of a line

Multi-line Comments

Use /* */ for comments that span multiple lines:

/*
  This is a multi-line comment.
  It can span several lines.
  Everything between the opening and closing is ignored.
*/
⚠️ Warning
Multi-line comments cannot be nested! If you have a /* inside another /* */ comment block, it will cause compilation errors. Use single-line comments (//) when commenting out code that might already contain multi-line comments.

Best Practices

Good Comments Explain Why

// Bad: what the code does (obvious)
x = x + 1;  // Add 1 to x

// Good: why we're doing it
x = x + 1;  // Move to next position in array

Document Complex Logic

// Calculate compound interest using formula: A = P(1 + r/n)^(nt)
double amount = principal * pow(1 + rate/compounds, compounds * time);

Use Comments for TODOs

Keep TODO comments to a minimum, normally YAGNI (You Aren't Gonna Need It)

// TODO: Add input validation
// FIXME: Handle division by zero case

Avoiding Over-commenting

Don't comment obvious code:

// Bad - obvious what's happening
int count = 0;  // Set count to zero
count++;        // Increment count by 1

// Good - comment explains purpose
int count = 0;  // Track number of valid entries

Pseudocoding (SUDO coding)

Pseudocode is a human-readable blueprint or plan for developing software.

Why this is a good idea

  • Clear thinking first: Writing pseudocode makes you focus on the steps instead of syntax.
  • Easier to understand: Someone else (or future you) can read the pseudocode and know what the program should do, even without C++ knowledge.
  • Reduces mistakes: Once the logic is correct in pseudocode, turning it into real code is much easier.

Example

This is a very simple explain to help explain the concept, in the real world you would be do this for a more complex problem.

Write your steps first.

// Start at zero
// Count from 1 to 3
// Add each number
// Show the result

Then use it as a guide to write an implementation.

Don't worry about the for loop we will cover it later. This is one way to implement it.

#include <iostream>

int main() {
    int total = 0;               // Start at zero
    for (int i = 1; i <= 3; i++) // Count from 1 to 3
        total += i;              // Add each number

    // Show the result
    std::cout << total << std::endl;
    return 0;
}

Summary

  • Two types: Single-line (//) and multi-line (/* */)
  • Purpose: Explain why code exists, not what it does
  • Best practices: Document complex logic, use for TODOs, avoid over-commenting obvious code
  • Pseudocoding: Plan your logic with comments first, then implement
  • Warning: Multi-line comments cannot be nested

Good comments make your code readable for others and your future self. Use them wisely to explain the reasoning behind your code, not the mechanics of simple operations.