Est.
Reading CodeLong read

Understanding Code Comments and Inline Documentation

Good documentation tells AI why code works, not just what it does.

Features Editor · · 9 min read
Cover illustration for “Understanding Code Comments and Inline Documentation”
Reading Code · September 22, 2026 · 9 min read · 2,039 words

Code comments do not exist to explain syntax. Anyone reading Python knows what a for loop does; a comment that says "loop through the list" wastes a line. What a comment or docstring is actually for is transferring the things the code itself cannot say: why a decision got made, what edge cases got considered and thrown out, and what the caller is on the hook for guaranteeing before it calls the function. Code tells you what happens. Documentation is supposed to tell you why, and it's the "why" that AI coding tools and human engineers alike are starving for.

It helps to separate three layers that get lumped together under "documentation." Inline comments clarify logic at the line or block level, usually a sentence explaining a non-obvious choice inside a function body. Docstrings describe a function or class contract: what goes in, what comes out, what breaks. Architecture-level documentation explains how files and modules relate to each other; it lives in a wiki or a top-level doc rather than next to any single function. Conflating these three is where a lot of documentation efforts go wrong before they even start.

What good inline documentation contains

A docstring must include parameter explanations, return types, a description of what the function actually does, and the exceptions it can raise, and that bar is not high. That docstring baseline of parameter explanations, return types, a description of what the function actually does, and the exceptions it can raise is the complete requirement. If any one of those is missing, the docstring is incomplete, whether the reader is a human skimming for context or a tool parsing the file automatically.

The conventions that enforce this shape, JSDoc for JavaScript and TypeScript, Python's docstring format under PEP 257, JavaDoc for Java, are not stylistic preferences somebody made up to keep code tidy. They're contracts. Editors parse them to show inline hints. Linters flag when they're missing or malformed. AI assistants parse them to figure out how a function is supposed to be called without reading its full implementation. Break the convention and every tool downstream of it loses a bit of its grip on the code.

It gets interesting for anyone using an AI model to draft these. A model will get the structure right almost every time: it will enumerate parameters, infer return types from the code, and format the docstring correctly for the language. What it can't reliably get right is whether the described behavior matches the actual intent behind the code. A retry guard might have a docstring claiming it handles "transient gateway errors," and the model will happily reproduce that framing if it's plausible-looking, without any way of confirming whether that's actually the condition the original author meant to catch or just the condition the code happens to catch today. That distinction, intent versus incidental behavior, is exactly the kind of thing a model can't verify from the code alone. A human reviewer can. That review step isn't bureaucratic overhead sitting between a developer and shipped code; it's the only mechanism that catches a docstring that sounds right and is wrong.

How documentation degrades and why stale docs are worse than no docs

Code changes on every commit. Documentation changes on no particular schedule at all, because nothing forces it to move when the code does. That mismatch is the entire mechanism behind documentation rot, and it compounds silently: nobody notices a docstring going stale the way they'd notice a failing test, because a stale docstring doesn't fail anything. It just sits there, quietly wrong.

Running a documentation generator once against a repository solves the easy 20% of this problem, the initial coverage gap, and leaves the hard 80% completely untouched: keeping that documentation aligned with a codebase that keeps changing under it. And stale documentation is arguably worse than having none at all, because it lies with authority. A missing comment tells a reader, correctly, to go check the code themselves. A wrong comment tells them they already know the answer. They trust it, they act on it, and the error it planted propagates into whatever they build next.

The moments where the divergence actually happens are specific and recognizable. A function gets renamed in a refactor, and its docstring keeps referring to the old name. A parameter gets added or dropped from a signature, and nobody updates the parameter list describing it. Behavior shifts, a retry count goes from three attempts to five, an exception type changes from a generic error to something more specific, and the prose sitting above the function never catches up. Architecture-level docs are the slowest to rot and the hardest to notice rotting: a comment describing a module boundary that was refactored away months earlier can sit in a codebase indefinitely, actively misleading anyone new who reads it first.

The AGENTS.md file and agent-specific context as a documentation layer

A README answers a different question than an AI coding agent needs answered. READMEs explain what a project does, aimed at a human deciding whether to use or contribute to it. An agent about to make a code change needs something more operational: the exact build command and its flags, how tests actually get run, style rules that diverge from language defaults, architectural constraints that aren't visible from reading the code, and a list of files that should never be touched no matter how reasonable the change looks.

AGENTS.md exists to hold exactly that. It's a plain Markdown file placed at the root of a repository, with no required schema and nothing to install to make it work: any agent capable of reading a file in a repo benefits from it immediately. That simplicity is the point. It was originally developed at OpenAI and released as an open standard in August 2025, and it's now governed under the Agentic AI Foundation, a Linux Foundation project, under an MIT license. Adoption has moved fast: Claude Code, OpenAI Codex, Cursor, and VS Code all support it, along with, reportedly, more than thirty other tools.

The most concrete recent development came on September 18, 2026, when Claude Code version 2.1.277 added support for reading instructions directly from agents.md. If no Claude-specific instruction file exists in a repository, Claude Code now falls back to AGENTS.md by default. For teams running more than one coding agent against the same codebase, that closes a real gap: no more maintaining a Claude-specific file alongside separate instruction files for each other agent, each one drifting from the others the moment somebody updates only one.

How documentation quality affects AI coding agents

Every AI coding tool tested against existing codebases performs noticeably worse than it does on greenfield projects, and the reason isn't mysterious. Greenfield code has no history, no implicit conventions, no undocumented assumptions baked in by a developer who left the company two years ago. Existing codebases have all three, and an agent walking into that environment has to reconstruct context that documentation, if it existed and stayed current, would have handed over for free.

Benchmark numbers make the gap concrete. As of mid-2026, the top-performing agents resolve somewhere around 60 to 70% of SWE-bench Verified tasks; those are curated issues with clear acceptance criteria, a well-specified problem that real production work almost never hands anyone. Real-world tickets arrive vague, underspecified, and dependent on context that lives in someone's head rather than in a ticket description. The distance between benchmark performance and real-world performance is, in large part, a documentation gap: agents do well when the context they need is explicit and structured somewhere they can read it, and they do poorly the moment that context is implicit, tribal, or simply absent.

Treat an AI coding tool the way you'd treat a junior developer on day one: fast, confident, and almost completely without context for the codebase in front of them. A junior developer's output improves as they absorb tribal knowledge over months. An agent doesn't get months. It gets whatever's written down, and win rate on any given task tracks with how much of that context got handed over up front, not with how cleverly the prompt was worded. Context injection, not better prompting, is the lever, and documentation is the primary way that context gets injected.

Practices that keep documentation accurate as code evolves

Telling developers to try harder to update comments has never worked at scale, and there's no reason to expect it to start working now. The fix that actually holds is automation tied to the same events that change the code. If documentation only updates when someone remembers to update it by hand, it will eventually stop updating, because remembering is not a process.

Event-triggered documentation closes that gap by hooking into version control directly. Automations that run on push and pull request events can generate a description the moment a PR opens or refresh documentation the moment code underneath it changes, which removes the dependency on any individual developer's memory. Other approaches couple documentation to specific code symbols rather than to files: when a function gets renamed or a file moves to a new directory, an automated sync process detects the change and flags the documentation that's now out of step with it, whether that's a changed file name, a changed function name, or a changed implementation underneath an unchanged name. This drift happens because documentation tied to symbols breaks the moment the symbol's identity changes, even if the underlying logic hasn't; the sync process catches it because it watches the symbol itself, not just the surrounding file. The shared logic behind both approaches is the same: documentation should regenerate on the same trigger that produced the underlying change, not on a separate schedule that depends on someone remembering.

Validated, current documentation is useful to more than the humans reading it. It's the material that AI tools, coding assistants, internal agents, and MCP servers built on top of a codebase actually need in order to reason about that codebase correctly. A model fed a stale docstring will confidently reproduce the staleness in whatever it generates next.

Enterprise code search and documentation coverage across a large codebase

Individual discipline works at the level of a single file. A conscientious engineer can keep one module's docstrings honest through a dozen refactors. That discipline stops scaling somewhere around a few hundred repositories, or inside one sufficiently large monorepo, because the problem shifts from writing documentation to finding it: where does it exist, where is it missing, and where has it quietly drifted from the code it describes. As a codebase grows sufficiently large, the problem shifts from writing documentation to finding it: where it exists, where it's missing, and where it has quietly drifted from the code it describes, which makes it a search and indexing problem. It's a search and indexing problem.

Code search engines built for this scale let developers locate specific functions, variables, classes, and snippets across a codebase far too large to read through manually, with syntax highlighting, filtering by file type, and support for regular expressions when a search needs to be exact. That's the baseline capability, and it's genuinely useful for finding reusable components buried somewhere in a codebase nobody has fully mapped in their head.

Natural-language code search goes a step further, and it matters specifically for documentation coverage. Asking a plain question like "where is our authentication logic defined?" and getting back a cited, navigable answer beats keyword search because it surfaces not just the matching code but the comments and docstrings attached to it, the exact layer that's easy to lose when searching by literal string match alone.

That capability changes who can get an answer without interrupting someone else's day. A product manager who needs to know how a billing rule works, a support engineer chasing down a bug report, a new hire trying to understand a service before their first pull request: none of them should have to send a message to a senior engineer just to get an answer that's already written down somewhere in the codebase. That only works, though, if the documentation is actually indexed and searchable. Scattered across files that nobody remembers to check, even excellent documentation might as well not exist.

Sources

  1. Best AI Code Documentation Generators in 2026: Ranked and Compared
  2. plus8soft.com
  3. blog.agentailor.com
Filed underReading Code

More in Reading Code