Skip to content

Code Comment Policy

This content is not available in your language yet.

A comment carries a fact the code cannot state. Before writing one, name the criterion that justifies it. If none fits, write no comment and improve the name, type, or function boundary instead.

  • a. External behavior of Discord, an LLM provider, a protocol, or a library.
  • b. A security, privacy, or redaction reason.
  • c. A cache, ordering, transaction, or acknowledgement invariant, including anything that must happen before slow work.
  • d. Why a constant has its value, or what a sentinel or dense pattern means. A name already says what a constant is for, so a comment that repeats it goes.
  • e. A measured performance tradeoff.
  • f. Why a lint or type suppression cannot be removed.
  • g. A constraint someone could break while simplifying the code.
  • h. A constraint learned from production or debugging. Write only the constraint; the story of finding it belongs in the commit message.
// Discord rejects a second acknowledgement, so modal branches return before deferral.
if (opensModal) return showModal();

When unsure, omit the comment and make the name, type, or function boundary carry the meaning.

  • The next statement in English: // Parse and validate composite-key format above parseCompositeKey(...).
  • A restated function, type, variable, or parameter name.
  • Step numbers (// 1. Parse the value, // 5c-2. Build the menu) or // Rule 3: scaffolding.
  • Section banners or dividers; extract a named function instead.
  • Commented-out code.
  • What the code used to do, a line number, or an old phase or task label.
  • Incident history (“added after last quarter’s outage”). Git history holds it.
  • Point-in-time measurements (“31k entries”, “adds ~40 ms”). They read as current and go stale.
  • Commentary about the comment (“Two exclusions are load-bearing”, “Important:”).
  • Justification aimed at a reviewer. That belongs in the PR description.
  • Diff narration on a new guard, helper, or field, or copies of one domain rule across call sites. Put why a change happened in the commit message, and keep shared rationale on the shared helper.
  • Extra clauses added to an existing comment to cover new branches.

Keep each comment to one sentence with no semicolon-joined clauses. A new comment may wrap over two lines.

// Never sweep the client's own member: discord.js resolves permissions through it.

When changing code that already has a comment, do not expand the comment to describe the new code. Leave it alone when its constraint still holds, delete it when the updated code or a clearer name carries the meaning, or replace it with a shorter constraint when the invariant changed.

Examples in docs are illustrative. Deployment sizes, record counts, and host details do not belong in this public repository (see Docs Authoring).

Use TypeScript types and names to describe declarations. Preserve a constraint that protects against an actual bug in a single // sentence. The single-line /** @public <caller> */ directive is allowed for Knip exemptions on release-only or dynamically imported exports.

Em dashes, en dashes, and spaced double hyphens are not allowed in authored prose: comments, JSDoc, pages under docs/, and user-facing strings in src/locales/. Replace each with punctuation that names the relationship between the two halves:

RelationshipUseExample
Second half explains the firstColonRetirement is process-local: a restart refetches every guild's stickers.
Cause, so or becauseDiscord rejects a second acknowledgement, so modal branches return early.
AsideParenthesesThe probe caches its own failure (until the next restart).
Independent clausesPeriod or semicolon (docs and locales)The webhook send failed. The bot path is the fallback.
Term and definitionColon`large-v3`: ~4-5 GB VRAM
Numeric rangePlain hyphen1-5 minutes

If none fits, the sentence is doing two things; split it. Japanese text uses :, 。, and (). A dash that is data stays: a CLI flag (--no-build-isolation), Discord’s -# marker, a URL, or quoted output from another system.

Terminal window
bun run audit-comments # full-tree audit, plus the new-comment rules on your uncommitted work
bun run audit-comments --docs [paths] # prose audit of Markdown, default docs/en/contributing
bun test ./scripts/checks/commentPolicy.test.ts

scripts/checks/checkCommentPolicy.ts scans TypeScript (src/, scripts/, tests/, apps/), Markdown (docs/), standalone # comments in Dockerfiles, YAML, shell, PowerShell, Python, and TOML files (Dockerfile, docker-compose.yaml, bunfig.toml, docker/, servers/, .github/), and -- comments in SQL (src/db/, docker/), including comments inside $$ bodies. It runs deterministic rules that fail in any mode, and heuristic rules that warn. --staged or --base <ref> also judges changed lines: --base covers everything since the merge base with <ref>, including uncommitted and untracked files, and audit-comments passes --base HEAD. On changed lines, obvious-narration becomes an error and the new-comment rules apply. Every changed block up to 2 lines is checked, while an older block over 2 lines counts as new only when most of its lines changed; expounded-comment still warns whenever an edit grows an existing block. Errors fail bun run vl (under Documentation); the checker is not part of the test runner.

RuleKindCatches
prose-dashErrorDashes in comments, docs/ Markdown, and src/locales/ string literals. Skips code, URLs, link targets, CLI flags, -# , and empty table cells
disallowed-jsdocErrorJSDoc blocks except single-line Knip directives and license headers
numbered-narrationErrorStep numbers on line comments, including Step 1: and Phase 2 prefixes
rule-scaffoldingErrorRule N: and Rule #N prefixes
obvious-narrationWarning (error on changed lines)Action-verb narration and section banners
redundant-headingAudit warningA file header that repeats the file name (including a SQL Migration 042: title), or a standalone label that repeats the next object key or a prefix shared by the next two keys
tombstone-commentWarning on changed lines and in auditsA note about removed code: “was here”, “dropped by migration 011”, or an uppercase DEPRECATED marker
meta-dateWarning on changed lines and in auditsA parenthesized date recording when work happened, such as (March 2026)
orphaned-commentErrorCleanup damage: a continuation with no opening line, a bare divider, an empty JSDoc block
duplicate-commentAudit warningTypeScript blocks with the same text after normalizing markers, whitespace, and case. Floor: 12 words and 60 characters
long-comment-blockAudit warningA non-JSDoc block of 8 lines or more outside tests/
long-new-commentError on changed linesA new comment over 2 lines
new-multi-sentenceError on changed linesA changed line-comment block with multiple sentences or semicolon-joined clauses
expounded-commentWarning on changed linesAn edited comment block that grew by 5 or more words instead of staying put, shrinking, or being removed
unexplained-commentWarning on changed linesA new line-comment block with no reason, condition, or unit signal (because, so, when, a colon, and similar)

Each duplicate warning lists every copy, including copies across locale files. The audit skips suppression comments because each one is a separate lint decision.

COMMENT_AUDIT_DUPLICATE_MIN_WORDS, COMMENT_AUDIT_DUPLICATE_MIN_CHARS, and COMMENT_AUDIT_LONG_BLOCK_LINES override the limits for recalibration only. The bot never reads them, and a value that is not a whole positive number falls back to the default. The limits were chosen so that the full report stays readable in one sitting. When it stops being readable, re-measure; never delete rationale to shrink the count.

Treat a finding as a prompt to review. For each one, read the whole block and the code it describes. Remove it only when the code already says everything in it; otherwise rewrite it down to the constraint. Then re-read the surrounding lines for orphaned continuations and empty JSDoc, and review the diff as prose. Clear most duplicate groups by extracting the code path the copies share; deleting the copies loses the rationale.

To pick files for manual review, rank standalone // lines by file.

Terminal window
git grep -c -E '^[[:space:]]*//' -- 'src/*.ts' | sort -t: -k2,2nr | head -20

scripts/checks/comment-policy-exceptions.json records current, intentional matches only, such as literal syntax that resembles a violation, each with the exact comment and a reason. The checker reports an exception as stale once its comment is gone. It is not a log of past cleanups.