Sub topic 12
What to Document and Keeping It Accurate
This chapter assumes you have read the earlier chapters. You should already know the core JSDoc tags, // @ts-check with checkJs, and the basic eslint-plugin-jsdoc setup. Here the question changes from how to write JSDoc to when to write it, and how to keep it true after the code changes.
Document the contract
A good comment states the contract: what callers can rely on and what they must provide. The code already shows how the function works. The comment should cover what the code can't show:
- Units and ranges, such as milliseconds or "must be >= 1".
- Side effects, such as network calls, mutation of arguments, or writes to disk.
- Failure behavior, meaning what is thrown or rejected and when.
- Edge cases, such as empty input,
null, or the last retry.
A comment that only repeats the signature is noise:
/**
* Retries.
* @param {Function} fn - The fn.
* @param {number} attempts - The attempts.
*/Compare it with a comment that records the contract:
// @ts-check
/**
* Calls `fn` until it resolves or the attempts run out.
* Waits `delayMs` between attempts, but not after the final failure.
* @template T
* @param {() => Promise<T>} fn - Operation to try. Called at most `attempts` times.
* @param {number} [attempts=3] - Total tries, including the first. Must be >= 1.
* @param {number} [delayMs=100] - Pause between tries, in milliseconds.
* @returns {Promise<T>} The first successful result.
* @throws {Error} Rejects with the error from the final attempt if every attempt fails.
*/
export async function retry(fn, attempts = 3, delayMs = 100) {
let lastError;
for (let i = 0; i < attempts; i++) {
try {
return await fn();
} catch (err) {
lastError = err;
if (i < attempts - 1) {
await new Promise((resolve) => setTimeout(resolve, delayMs));
}
}
}
throw lastError;
}Each line of the second comment tells the caller something they would otherwise have to work out by reading the loop.
Where to spend effort
| Code | Document? | Why |
|---|---|---|
| Exported functions and classes | Always | Other modules and the generated docs depend on them |
| Shared `@typedef` shapes | Always | They become the vocabulary of your codebase |
| Non-obvious private helpers | Briefly | Explain the reason, not the steps |
| Trivial private code (`isEmpty`) | Usually not | The name already says it |
| Workarounds and odd choices | Yes, with `@see` | A link to the issue stops someone "fixing" it later |
Why comments go stale
Comments drift because nothing runs them. Suppose someone renames a parameter and doesn't touch the comment:
/**
* @param {number} delayMs - Pause between tries, in milliseconds.
*/
function wait(timeoutMs) {
return new Promise((resolve) => setTimeout(resolve, timeoutMs));
}The code still works, but the docs now describe a parameter that doesn't exist. Prose can drift too. If someone changes the delay to seconds and leaves "in milliseconds" in the comment, no tool will notice.
Make the tools guard the structure
Use automation to catch every class of error that a tool can detect. That leaves human review free for the prose.
// eslint.config.js
import jsdoc from 'eslint-plugin-jsdoc';
export default [
jsdoc.configs['flat/recommended'],
{
files: ['src/**/*.js'],
plugins: { jsdoc },
rules: {
'jsdoc/require-jsdoc': ['warn', { publicOnly: true }],
'jsdoc/check-param-names': 'error',
'jsdoc/require-param': 'error',
'jsdoc/require-returns': 'error',
},
},
];With this config, ESLint reports the wait mismatch above: the comment documents delayMs, but the function's parameter is timeoutMs. Running npx tsc with checkJs catches the other half of the problem, where the types in the comment no longer match how the code is called. Run both in CI so that drift fails the build and doesn't reach users.
Habits that keep docs honest
- Edit the comment in the same commit as the code. A reviewer should see both in one diff.
- Keep
@examplesnippets small enough to copy into a test, and copy them into one so the example breaks when the behavior changes. - Prefer deleting a wrong comment to keeping it. No comment is better than a misleading one.
- Mark replaced APIs with
@deprecatedand point to the new one with{@link}. Don't delete them silently. - During review, ask one question: "If I only read the comment, would I call this correctly?"
Exercise
A teammate changes retry so that attempts counts retries after the first call, which means attempts = 3 now makes 4 calls. They leave the comment unchanged. Which check catches this: eslint-plugin-jsdoc, tsc with checkJs, or code review? What should the updated @param line say?
Show answer
Only code review catches it. The parameter name and type are unchanged, so ESLint and tsc both pass, and only the meaning in the prose is now wrong. The line should read something like: @param {number} [attempts=3] - Retries after the first call. Total calls = attempts + 1. Must be >= 0. The summary line also needs to stay accurate.
Exercise
You have an exported function formatDate(date) and a private helper pad2(n) that left-pads a number to two digits. Which needs a full JSDoc block, and what should the formatDate comment cover beyond the type?
Show answer
formatDate needs a full block because it is exported. Its comment should state the output format (for example YYYY-MM-DD), whether it uses local time or UTC, and what happens with an invalid Date. pad2 is trivial and private, so its name is enough documentation.