Code Comments
Write clear, useful comments in English — from inline WHY-comments to full JSDoc blocks and Python docstrings. 6 practice sets.
Reading Code Comments
Understand JSDoc, inline comments, and TODO/FIXME markers in real codebases.
Writing Inline Comments
Practise the "WHY not WHAT" rule — when to comment and how to phrase it clearly.
JSDoc & Docstrings
Write and fix JSDoc comment blocks: @param, @returns, @throws, @example.
Python Docstrings
Compare Google-style, NumPy-style, and Sphinx-style Python docstrings.
README Code Sections
Annotate code blocks and write clear Quick Start and installation sections.
Comment Quality
Spot anti-patterns (lying comments, obvious comments) and rewrite them correctly.
Quick reference
Comment types
- Inline comment: single-line explanation after/above code
- Block comment: multi-line explanation of a function or section
- Docstring: structured comment attached to a function/class (JSDoc, Python)
- TODO: planned future work ("TODO: add retry logic")
- FIXME: known bug or broken code to fix later
- HACK / XXX: temporary workaround; needs revisiting
- NOTE / NB: important contextual information
The "WHY not WHAT" rule
i++; // increment i— bad: says what the code doesi++; // skip the header row— good: says why- Comment on intent, not mechanics
- "We use X because Y" — provides context the code cannot show
- Document edge cases, non-obvious algorithms, external constraints
JSDoc key tags
@param {type} name - description@returns {type} description@throws {ErrorType} when …@example— usage example block@deprecated— marks a function as deprecated@see— cross-reference link
Frequently Asked Questions
What makes a good code comment in English?
A good code comment explains WHY, not WHAT — the code shows what. Good comments describe intent, workarounds, warnings, or non-obvious constraints: "// Using polling instead of WebSockets due to corporate proxy restrictions", "// Don't change this order — breaks the legacy API contract". Avoid commenting obvious code: "// increment counter by 1" adds no value and creates maintenance burden.
What are the different types of code comments in IT?
Comment types: Intent comments (explain why), TODO/FIXME markers (pending work — always include a ticket reference), Warning comments ("// side effect: clears the cache"), API documentation (JSDoc, docstrings — describe parameters, return values, exceptions), Licence headers (legal requirements at file top), Section delimiters (organise long files). Each serves a specific purpose in code maintenance.
What is the correct English style for TODO and FIXME comments?
Best practice format: "TODO(username): description [ticket-ref]" — e.g., "// TODO(alex): remove after migration to v2 API [JIRA-1234]". FIXME indicates a known bug: "// FIXME: race condition under high load — see JIRA-456". HACK or XXX marks questionable code requiring review. Always include a ticket reference — orphaned TODOs without context become technical debt that nobody addresses.
How do I write JSDoc comments in English?
JSDoc format: `@param {type} name - description`, `@returns {type} - what the function returns`, `@throws {ErrorType} - when this exception is raised`, `@example - code example`, `@deprecated - Use [alternative] instead since v[version]`. Use imperative mood in the description: "Returns the user by ID" (not "This function will return..."). Keep descriptions concise — one line per parameter is usually enough.
What English words should I avoid in code comments?
Avoid: (1) Obvious restatements — "// creates a new user" above a `createUser()` function; (2) Dated references — "// added on 2024-01-15" (use git blame); (3) Commented-out code without explanation — delete it or explain why it's kept; (4) First-person pronouns without context — "// I changed this to..." (use commit messages); (5) Negative comments about colleagues — "// this is terrible code by John". Professional, constructive, and impersonal.
What is the difference between inline comments and block comments?
Inline comments appear on the same line as code: `x = x * 2; // double the value for display purposes`. Block comments (docstrings) appear above a function or class and document its interface. Inline comments should be rare — if a line needs a comment to be understood, consider renaming the variable or extracting a function instead. Block/docstring comments are systematic documentation expected for public APIs.
How should I comment on complex algorithms?
Complex algorithm comments should: (1) Reference the algorithm name and source ("// Knuth-Morris-Pratt string matching — see CLRS 3rd ed., p.1002"); (2) Explain time/space complexity ("// O(n log n) — acceptable for n < 10,000 records"); (3) Describe preconditions ("// Assumes input array is sorted in ascending order"); (4) Note why a simpler approach doesn't work ("// Can't use a hash map here because order matters"). These save hours for future maintainers.
How do Python docstrings differ from JavaScript JSDoc?
Python uses triple-quoted docstrings: `"""Summary line.\n\nArgs:\n name (str): Description\n\nReturns:\n dict: Description\n"""`. Common formats: Google style, NumPy style, Sphinx reST. JavaScript uses `/** ... */` JSDoc: `@param {string} name - Description`. Both serve the same purpose — documenting the API contract. The key difference is placement: Python docstrings are inside the function body; JSDoc goes above the function.
When should code comments be updated?
Update comments when: (1) the implementation changes and the comment describes the old behaviour; (2) a workaround is resolved and the warning comment is no longer valid; (3) a TODO is completed (remove the comment); (4) referenced ticket numbers change. Outdated comments are worse than no comments — they actively mislead developers. Code review checkers like "has the comment been updated to match the code?" should be part of your review checklist.
What are good English comment styles for security-sensitive code?
Security comments: "// SECURITY: never log this value — contains credentials", "// IMPORTANT: validate input before this point — SQL injection risk if called directly", "// CSRF token check — do not remove this validation", "// Intentional constant-time comparison — prevents timing attacks". Security warnings should be prominent and specific about the risk. Use uppercase labels (SECURITY, WARNING, IMPORTANT) to make them scannable.