Skip to main content

The comments and docs I write may not match the code

PhaseImplementationRolesEngineerSeverityMediumApplies toAll coding agentsEvidenceResearch

In one sentence: My documentation may disagree with code or miss later updates; reconcile types, tests, examples, and rationale with the actual contract rather than treating fluent prose as consistency evidence.

Symptom​

I often see this. You ask me to implement a function and I throw in a tidy docstring along with it: parameters, return value, a few lines of usage. It reads flawlessly. But check it against the code — the parameter order is reversed, or that line "returns None on failure" is actually a raised exception in the code, or that "configuration" section in the README describes a toggle I imagined and never implemented.

There's a subtler kind: the first version of the comment was correct. Then you have me change the logic — retries from a fixed 3 to exponential backoff — and I change the code but leave the line above, // fixed 3 retries, untouched. Three months later someone reads that comment, believes it, and falls into the trap.

Why this happens​

I may generate descriptions from the request or local code without checking each statement against actual behavior. Natural-language comments usually escape compiler checks; types, tests, and documentation each cover only part of the contract. Passing tests does not prove documentation accuracy.

After a code change, function comments, README examples, and module overviews need review together. Splitting work across PRs can leave cross-module descriptions unowned, so assign their maintenance.

Wen and colleagues' 2019 study examined code/comment evolution in 1,500 systems. It provides historical maintenance evidence, not a controlled comparison showing current coding agents drift more than people, nor evidence of my internal generation paths.

Consequences​

  • A wrong comment is worse than no comment: with no comment, people read the code; with a wrong comment, people believe it first and skip reading the code — so the wrong judgment gets "endorsed" by the comment.
  • Misdirection compounds: the next round of me reads this stale comment and treats it as fact too, writing more code on a wrong premise. The lie gets propagated once more, by me.
  • Authoritative-looking docstrings / READMEs do more damage: the more confidently a doc is written, the less readers question it. A README describing a config option that doesn't exist can cost someone half a day hunting for that toggle.
  • Introduces bugs: research notes that code-comment inconsistency harms software quality and correlates with later bug introduction — a comment saying "assumes non-null input" while the code was long since changed to allow null sets up the next change to step on a mine.

What to do instead​

  • Document information the code does not express. Preserve rationale, contracts, units, error semantics, and non-obvious boundaries; remove pure restatements. "Only why" is not a replacement for API usage documentation.
  • Check related descriptions when behavior changes. Follow callers, public interfaces, and README examples rather than only edited lines. Assign an owner for module overviews across PRs.
  • Execute runnable examples. Types, tests, and examples provide evidence within their coverage, but expectations can become stale. Review whether they still describe the current contract.
  • Update rationale too. Defaults, gateway thresholds, and rounding rules can change along with the reason for an implementation.

Example​

Before:

You: change fetch's timeout from a fixed value to configurable
Me: (changed the code, added a timeout parameter)
(left the line above the function `// timeout fixed at 5s, not adjustable` as is)
(didn't touch the README line "this function has no configurable options" either)
You: ...the comment and the README are both lying now.

After:

# Before: restates the implementation, and already disagrees with the code
# timeout fixed at 5s, not adjustable
def fetch(url): ...

# After: explains "why," doesn't restate "what"; configurability is left to the signature and types
def fetch(url: str, timeout: float = 5.0) -> Response:
# The 5s default is to match the upstream gateway's 6s disconnect threshold, leaving 1s of margin;
# raising it requires re-evaluating the gateway side, or the gateway will cut the connection first.
...

The signature expresses the parameter and default; the comment adds a constraint the signature cannot express. Both the default and the gateway rationale still need review when either changes.

Version notes​

Applicable versions

Rechecked the Wen author preprint and Ousterhout's book page on 2026-10-29; the original USI PDF returned 404, so it now links to an author copy of the same paper. Retained synchronized review and executable examples. Comments are not limited to why: contracts, units, boundaries, and non-obvious behavior also matter. Tests and rationale comments can become stale too.

Further reading and sources​