agentleFS
gg-mogg-mo/repo-hygiene/skills/writing-docstrings/SKILL.md

writing-docstrings

Skill3 starsChanged 7 months agoMITView on GitHub

Use when writing or modifying any function, method, class, or source file in a repo using repo-hygiene — public API, internal helpers, and new files all require docstrings; only one-line lambdas and trivial accessors are excluded

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every source file gets a header explaining its role.

    line 60

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Writing Docstrings

Overview

In a repo using repo-hygiene, docstrings on functions/classes and headers on source files are required. This OVERRIDES the default "no comments unless WHY is non-obvious" stance. The override exists because docstrings ARE the API contract — a future reader (human or agent) shouldn't have to read the body to know what to expect.

Core principle: A docstring earns its place by telling the reader something the signature does NOT tell them.

When To Use

  • Writing a new function, method, or class
  • Modifying a function's signature, return type, or contract
  • Creating a new source file (write a header)
  • Reviewing your own code before commit

What Earns a Docstring Its Place

A good docstring covers what the signature CAN'T:

  1. Contract — one-sentence promise, present tense
  2. Parameters — only those whose meaning isn't obvious from type/name
  3. Returns — only if non-obvious from the return type
  4. Raises / errors — failure modes the caller must handle
  5. Caveats — non-obvious behavior, side effects, performance, thread-safety

Skip anything the signature already conveys. The goal is information, not ceremony.

Before / After

❌ Restates the signature (worthless):

def parse_date(raw: str) -> datetime:
    """Parse a string and return a datetime."""
    return datetime.strptime(raw, "%Y-%m-%d")

❌ Empty ceremony:

def parse_date(raw: str) -> datetime:
    """
    :param raw: the raw string
    :returns: the parsed datetime
    """

✅ Pins what the signature can't:

def parse_date(raw: str) -> datetime:
    """Parse a YYYY-MM-DD date. Raises ValueError on any other format."""
    return datetime.strptime(raw, "%Y-%m-%d")

File Headers

Every source file gets a header explaining its role. A header that just paraphrases the filename is worse than no header.

❌ Paraphrases filename:

"""user_repository.py — the user repository."""

✅ Explains role + boundary:

"""Persistence layer for User aggregates.

Owns reads/writes to the `users` table. Callers should never use raw SQL
against this table — go through this module so audit logs stay consistent.
Domain type lives in domain/user.py.
"""

A good header answers: what does this file own, what's the boundary, who calls it, what's a closely related file?

Narrow Exceptions

These don't need a docstring (the signature IS the spec):

  • One-line lambdas / arrow functions
  • Trivial accessors: def name(self): return self._name
  • Single-expression private helpers under 3 lines

For everything else: if you can't think of a useful one-liner, the function probably does too much or is misnamed. Fix the function before skipping the docstring.

Quick Reference

LanguagePattern
Python"""...""" after def/class; module header at top of file
TypeScript / JavaScriptJSDoc /** ... */ above function/class; top-of-file block
Go// FuncName ... above declaration; // Package x ... at top
Rust/// for items, //! for module-level
JavaJavadoc /** ... */

Mirror the repo's existing convention. If unclear, copy a well-documented file in the same language.

Common Mistakes

MistakeFix
Restating the signature in proseCut. If the signature says it, the docstring is noise.
Documenting every param regardless of clarityOnly the ones whose meaning isn't obvious
File header paraphrases filenameExplain role + boundary + collaborators
"My default is no-comments"This skill OVERRIDES that default. Intentional.
Skipping docs on "internal" helpersInternal becomes public. Document the contract anyway.

Red Flags — STOP

  • "My default is no comments unless WHY is non-obvious" — that default does not apply in repo-hygiene repos
  • About to ship a function with no docstring (outside the narrow exceptions)
  • About to create a new file with no header
  • Writing a docstring that just rephrases the function name — rewrite or you're adding noise

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.