Comments in Programming

πŸ“Œ Concept 2 of 13✍️ Written by Tom ReyesπŸ“… Reviewed 2026-04-09⏱ ~5 min read

Comments are the easiest concept on this list β€” and the easiest to get wrong. Here's a working philosophy and the syntax for the three big languages.

Bee, our debugging mascot

From Bee: Comments age badly. Every time you change code, scan the comments above and below it. Outdated comments are worse than no comments.

What Is a Comment?

A comment is text inside source code that the computer ignores when running the program. Comments exist for humans β€” your future self, your teammates, anyone reading the code in six months. The compiler or interpreter strips them out; they have zero effect on what the program does.

Comments are useful for three things: explaining why a piece of code does something non-obvious, leaving reminders or todos, and temporarily disabling code without deleting it.

Syntax in Three Languages

// Single-line comment
let total = 100; // Inline comment after code

/* Multi-line
   comment that spans
   several lines */

/**
 * JSDoc comment β€” used for documentation tools
 * @param {number} n
 */
function double(n) { return n * 2; }
# Single-line comment
total = 100  # Inline comment after code

# Python has no native multi-line comment.
# Convention: stack many # lines, or use a
# triple-quoted string (technically a string
# literal that gets discarded):

"""
This is often used as a multi-line comment.
"""

def double(n):
    """Docstring β€” describes what the function does."""
    return n * 2
// Single-line comment
int total = 100; // Inline comment

/* Multi-line
   comment */

/**
 * Javadoc comment β€” used by the documentation generator.
 * @param n the number to double
 * @return n times two
 */
public static int doubleIt(int n) { return n * 2; }

When to Write a Comment

Good code is mostly self-documenting through clear naming and small functions. Comments earn their keep when they explain things the code itself can't:

  • Why a non-obvious choice was made. "We use a 3-second debounce because the upstream API rate-limits at 0.5 req/s."
  • Workarounds for a specific bug or browser quirk. "Safari 15 has a bug with focus events on input[type=date] β€” hence the timeout."
  • Constants whose meaning isn't obvious from the name. "Threshold of 0.83 β€” empirically the value with best precision/recall on our data."
  • Public API documentation. Any function others will use should describe what it accepts and returns.

When NOT to Write a Comment

The most common beginner mistake is over-commenting. Don't restate what the code obviously says:

javascript
// BAD β€” comments add noise without information
let x = 5;          // Set x to 5
x = x + 1;          // Increment x by 1
return x;           // Return x

Compare with:

javascript
// GOOD β€” comment adds info you can't get from reading the code
let retries = 5; // 5 chosen to match payment provider's max attempts
while (retries-- > 0) {
  if (chargeCard()) return true;
}
return false;
πŸ’‘
The "rename instead of comment" rule

Before adding a comment to clarify a variable or function, try renaming it. // total of all items on a variable named t is wasted ink β€” just rename t to total and skip the comment.

Commenting Out Code

You can wrap code in comment syntax to temporarily disable it without deleting it. This is fine for short experiments. Don't ship code with large blocks of commented-out code β€” version control (git) tracks deleted code; commented-out blocks just clutter the file.

TODO and FIXME Comments

A widely-followed convention: prefix temporary reminders with TODO:, FIXME:, or HACK:. Most editors highlight these visually, and you can grep them later to find work that needs doing.

javascript
// TODO: switch to the new pricing endpoint when it's live
// FIXME: this breaks if the user has more than 1000 items
// HACK: padding by 2px to work around Safari rendering bug

A Small Mental Model

// Comment β€” humans read this let price = 9.99; /* Computer ignores comments */ console.log(price);

Comments live in the source file but are stripped before the program runs.

Best Practices

  1. Comment why, never what β€” the code already says what.
  2. Keep comments next to the code they describe.
  3. Update comments when the code changes β€” outdated comments are worse than none.
  4. Use docstrings for any function someone else will call.
  5. Don't commit large blocks of commented-out code.
  6. Use TODO/FIXME for known issues so you can find them later.
πŸ›
See the bugs in action

We have a dedicated Common Bugs with Comments page β€” five real broken snippets with the fix. Read it before you start writing.

Quick Quiz

Real-World Uses (Production Code, Today)

Open-source PR review

Reviewers look at comments to understand intent. A comment saying "TODO: handle null" tells the reviewer the author already knows about a gap.

Generated docs

Tools like JSDoc, Sphinx, Javadoc read your comments and produce browsable API documentation. The comment IS the docs.

Production debugging

"Why is this here?" is the most-asked question in legacy code. A two-line comment can save a future engineer 30 minutes of git-blame.

Frequently Asked Questions

What's the syntax for comments in Python?

Python uses # for single-line comments. For longer "comments," developers often use triple-quoted strings ("""...""") β€” technically these are string literals, but they're commonly used for multi-line notes and as docstrings inside functions.

How do I write a multi-line comment in JavaScript?

Wrap the text in /* ... */. Anything between those markers is treated as a comment, even across multiple lines.

Should I comment every line of code?

No. Most lines should be self-explanatory through clear naming. Comment only when you need to explain why something is done, not what is done.

What's the difference between a comment and documentation?

Comments live inside source files for code-level context. Documentation (often generated from docstrings/Javadoc/JSDoc) is the broader, user-facing description of how to use a library or API.

Are there any languages without comments?

Practically none. Even minimalist languages like Brainfuck have informal commenting (any character not in the language's instruction set is ignored).

Can comments slow down a program?

No. Comments are stripped during the parse step (or the build step in compiled languages). They have zero runtime cost.

What is a "magic comment"?

A few languages and tools treat certain comments specially β€” like Python's # -*- coding: utf-8 -*- or Ruby's # frozen_string_literal: true. These look like comments but trigger compiler/runtime behavior.

How do I quickly comment out a block in my editor?

Most editors let you select lines and press Ctrl + / (or Cmd + / on Mac) to toggle comments. It works in VS Code, Sublime, JetBrains IDEs, and many others.

βœ…
Key takeaways
  • Comments are notes for humans; the computer ignores them.
  • Explain why, not what.
  • Outdated comments are worse than no comments β€” keep them in sync.
  • Use docstrings for any function others will use.
Was this helpful?
T
Tom Reyes
Reviewer Β· 12 yrs Java/JVM

Tom spent eight years as a backend engineer in fintech (Java + Kotlin) and four as a lead at an enterprise SaaS company. He reviews every Java example on this site and writes the data-structure deep dives. He cares deeply that beginners aren't taught bad habits they'll have to unlearn. More about Tom β†’