What gets written, language by language
Each language has its own comment syntax and its own rules about what belongs inside. The drafts follow the convention read by the documentation tool of each language, so the syntax works with that tool as it stands. Your job is the wording.
-
JavaScript JSDoc /** */
Types in braces, guessed from default values when nothing else says. Optional parameters get the
[name=default]form. Destructured arguments becomeoptions.fieldentries, generators get@yields, and async functions return a Promise. -
TypeScript TSDoc /** */
No types in braces, since the compiler reads them from the signature. Defaults move into the description. Thrown errors are written as
{@link Error}references. -
PHP PHPDoc /** */
Types come from the type hints, with
mixedwhere a parameter has none. Variadics print as...$name, promoted constructor properties count as parameters, and@returnfollows the declared return type. -
Python docstring """
Written inside the body under the def line, in Google, NumPy or reST layout. Annotations become types, defaults mark a parameter optional, raise statements fill Raises, and a yield turns Returns into Yields.
-
Java Javadoc /** */
Summary in the third person, the form the Oracle Javadoc guide asks for. No
@returnon void methods or constructors.@throwsmerges the throws clause with everythrow newin the body. -
C# XML ///
Summary, param, returns and exception elements. A plain
Taskgets no returns element. Members with no access modifier are private by compiler default, so Public API only leaves them out. -
Go doc comment //
One sentence opening with the name, the form
go docand pkg.go.dev expect. Go has no parameter tags. Lowercase names are unexported and count as private.
One Python function, three docstring styles
The docstring setting changes layout, never content. Here is a finished docstring for one retry helper in each style, written by hand so you see the target rather than the draft.
def retry_delay(attempt: int, base: float = 0.5) -> float:
"""Return the wait before the next retry.
Args:
attempt (int): Retry number, from 1.
base (float, optional): First delay in
seconds. Defaults to 0.5.
Returns:
float: Seconds to sleep, capped at 30.
Raises:
ValueError: If attempt is below 1.
"""
def retry_delay(attempt: int, base: float = 0.5) -> float:
"""Return the wait before the next retry.
Parameters
----------
attempt : int
Retry number, from 1.
base : float, default 0.5
First delay in seconds.
Returns
-------
float
Seconds to sleep, capped at 30.
Raises
------
ValueError
If attempt is below 1.
"""
def retry_delay(attempt: int, base: float = 0.5) -> float:
"""Return the wait before the next retry.
:param attempt: Retry number, from 1.
:type attempt: int
:param base: First delay in seconds.
:type base: float
:returns: Seconds to sleep, capped at 30.
:rtype: float
:raises ValueError: If attempt is below 1.
"""
Match whatever the project uses already. With no convention in place, Google style reads best in plain source and suits most application code. NumPy style gives long parameter lists room to breathe and is the standard in SciPy, pandas and scikit-learn. reST field lists render in stock Sphinx with no extension, at the price of being the hardest of the three to scan in an editor. Sphinx reads Google and NumPy through its napoleon extension, so the choice comes down to who reads the source, not to tooling.
Facts from the code, sentences from you
Half of a good docblock sits in the source already. The other half lives in the head of whoever wrote the function, and no parser reaches it.
Read from the source
- Parameter names, order, and rest or variadic markers
- Types from annotations and type hints, or from a default value when no annotation exists
- Default values, copied as written
- Whether a value comes back, from return statements and declared return types
- Error classes, from throw and raise statements and Java throws clauses
- Generators and async functions
Only you know
- Units: seconds or milliseconds, cents or dollars
- What null, an empty list or minus one means as a result
- Side effects: a database write, a network call, a mutated argument
- Valid ranges, and what happens outside them
- Who owns a returned handle and closes it
- Why this function exists next to the obvious alternative
The summary line comes from the name, so parseRetryAfter turns into "Parse retry after." The sentence restates the name and adds nothing. Below is the draft for the JavaScript sample next to a version worth committing.
/**
* Parse retry after.
*
* @param {*} header - TODO.
* @param {number} [now=Date.now()] - TODO.
* @returns {*} TODO.
*/
/**
* Turn a Retry-After header into an epoch time.
*
* @param {string} header - Seconds ("120") or an
* HTTP date, as sent by the upstream API.
* @param {number} [now=Date.now()] - Clock in epoch
* ms, injectable for tests.
* @returns {?number} Epoch ms to wait until, or
* null when the header is unreadable.
*/
Three things changed. Every description now says something the signature hides: the two header formats, the unit, the null case. The type of header went from a star to string, since a parameter with no default and no annotation gives the parser nothing to go on. The return type gained a question mark for the null branch, which inference missed because the other branch returns an expression with no literal type.
Reading the ledger
- Documented
- A docblock sits directly above the symbol, or a docstring opens its body, and every parameter has an entry. A function returning a value also needs a return entry.
- Gaps
- A docblock exists but skips something, and the note names each missing parameter or return entry. In Go, the note flags a comment opening with a word other than the function name, the rule linters such as revive enforce. The generator never edits these. Rewriting a half-finished docblock would throw away the half someone wrote.
- Missing
- Nothing above the symbol, or only a plain
/* */comment. Documentation tools skip plain comments, so those count as missing as well. Every Missing row gets a draft in the source view.
The percentage counts symbols with any docblock, gaps included. Use it to track a file between commits, not as a quality score. A file at 100% where every description reads TODO documents nothing.
The ledger expects a docblock on the line directly above the declaration, with decorators, annotations and attributes allowed in between. A blank line between comment and code reads as Missing, so close the gap before trusting the count.
From docblocks to a published reference
The Markdown view suits a README section or a wiki page. For a full docs site, run the generator your language ships with over the commented files.
| Language | Tool | Starting command |
|---|---|---|
| JavaScript | JSDoc | npx jsdoc src -r -d docs |
| TypeScript | TypeDoc | npx typedoc src/index.ts |
| PHP | phpDocumentor | phpDocumentor -d src -t docs |
| Python | Sphinx with autodoc and napoleon | sphinx-apidoc -o docs src |
| Java | javadoc | javadoc -d docs -sourcepath src -subpackages com.toolexe |
| C# | Compiler XML output, then DocFX | <GenerateDocumentationFile>true</GenerateDocumentationFile> |
| Go | go doc, pkg.go.dev | go doc -all ./redirects |
Each tool reads only the formats it supports. Sphinx without napoleon prints Google and NumPy sections as plain text, and TypeDoc ignores types written in JSDoc braces, so pick the style setting for the tool at the end of the pipeline.
When another tool fits better
- One function in an open editor. VS Code drafts a JSDoc block when you type
/**above a function, PhpStorm and IntelliJ do the same for PHPDoc and Javadoc, and Visual Studio expands///into an XML block. This page earns its place on a whole file, through the ledger and the Markdown reference. - Regex reading, not a compiler. Decorators spread over several lines, TypeScript overload signatures, C# properties and Python lambdas assigned to names get skipped or read imperfectly. Compare the ledger with the file before committing.
- Nested functions are skipped on purpose. A helper defined inside a function body belongs to nobody's API.
- Return and throw detection reads the whole body. Callbacks are included, so a return inside an inner arrow function counts toward the outer one.
- Types from defaults are guesses.
limit = 10says number, a default of null says nothing, and a parameter with neither gets a star ormixed. - Rust, Kotlin, Swift, Ruby, C and C++ are not parsed. Rust doc comments with Markdown sections and Doxygen blocks for C and C++ need their own tooling.
- Pastes over 400 KB are refused. Split a large file by class and run the parts one at a time.
Nothing you paste leaves the browser. Parsing, drafting and the Markdown export run in JavaScript on this page, with no request to a server after load, so private repositories and files holding credentials stay on your machine.