Foundations to Frontier
Tests
C. Programming, Tools and HPC › C2 Software Engineering for Research

Documentation

Code is read far more often than it is written, and documentation is how its intent, units, assumptions and usage survive personnel changes. This topic covers the documentation layers of a research project (README, docstrings, API reference, tutorials and how-tos, design notes and decision records, changelogs), docstring conventions (NumPy/Google style, with units and shapes), executable documentation (doctests, notebooks in CI), generated reference docs (Sphinx autodoc, mkdocstrings), and keeping docs honest automatically. In the exercises you parse NumPy-style docstrings, check them against function signatures and run doctests, then audit a small library for documentation drift (coverage, renamed and missing parameters, stale examples), fix it, and generate its API reference.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Instructions that come with the machine

A brilliant tool with no instructions is almost useless to the next person, including you in a year. Documentation answers the questions code can’t: what is this for, how do I use it, what units are these numbers in, why was it built this way? The best documentation lives right next to the code (in docstrings), includes small examples that are run automatically so they can’t silently go out of date, and is turned into a browsable website by tools.

Documentation drift: code moves on, docs stay behind
release:
v1.0: initial version
def slew_time(angle_deg, rate_deg_s=1.0):
    """Time [s] to slew by an angle at a constant rate.

    Parameters
    ----------
    angle_deg : float
    rate_deg_s : float

    Examples
    --------
    >>> slew_time(90)
    90.0
    """
    return abs(angle_deg) / rate_deg_s
✅ every parameter documented
✅ no stale parameters
✅ doctest passes
Comments and docs that aren't checked silently go stale: v1.1 added settling time (the documented example is now wrong) and v2.0 switched to radians (the documented units are now dangerous). Keeping docstrings next to the code, writing executable examples (doctests), checking signature-versus-docs in CI (numpydoc validation, darglint/pydoclint) and building API docs from the code (Sphinx autodoc, mkdocstrings) keep docs honest.
Why engineers care

Mission software outlives its authors by decades (Voyager’s teams have turned over many times). Undocumented units and assumptions cause real failures, and good documentation is also a requirement in software-assurance standards and a precondition for others reusing your research code.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A docstring is…
  2. 2. Why run documentation examples automatically?
  3. 3. The most important thing to document for a physical quantity is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Layers of documentation

  • README: what it is, how to install, a minimal example, how to cite, license.
  • Docstrings: every public module, class and function.
  • API reference: generated from docstrings.
  • Tutorials and how-to guides: task-oriented.
  • Explanation: design notes, architecture decision records (ADRs), theory.
  • CHANGELOG: what changed, what breaks.

(The “Diátaxis” framework separates tutorials, how-tos, reference and explanation.)

A NumPy-style docstring

def orbital_period(a_km, mu_km3_s2=398600.4418):
    """Orbital period of a Keplerian orbit.

    Parameters
    ----------
    a_km : float
        Semi-major axis [km].
    mu_km3_s2 : float, optional
        Gravitational parameter [km^3/s^2]; Earth by default.

    Returns
    -------
    period_s : float
        Period [s].

    Examples
    --------
    >>> round(orbital_period(6778.0) / 60, 1)
    92.6
    """
📝 Quick check (Level 1)
0/3 answered
  1. 1. The first line of a docstring should be…
  2. 2. Which section lists exceptions a function can raise?
  3. 3. Where do installation instructions belong?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Executable documentation

  • doctest: python -m doctest module.py or pytest --doctest-modules runs every >>> example and compares the output. Use ELLIPSIS and NORMALIZE_WHITESPACE for robust comparisons, and round floats in examples.
  • Notebook tutorials executed in CI (C1.9).
  • Example scripts in examples/ run as tests.

Generated reference

Sphinx with autodoc, napoleon (NumPy/Google parsing) and intersphinx, or mkdocs with mkdocstrings, build HTML API references from the docstrings and type hints. Host them on Read the Docs or GitHub Pages, rebuilt on every release.

Checking docs automatically

numpydoc’s validation or pydoclint compare signatures with documented parameters (missing, extra, order), interrogate measures docstring coverage, and doctests catch stale examples, all in CI next to the linters (C2.3).

🐍 Exercise 1: docstrings as data
Parse NumPy-style docstrings (summary, Parameters with type and description, Returns, Raises), compare documented parameters with the function signature, and run doctests in source code, counting tries and failures.
📝 Quick check (Level 2)
0/3 answered
  1. 1. pytest --doctest-modules…
  2. 2. Sphinx autodoc builds…
  3. 3. A signature/doc mismatch checker reports…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Why docs drift

Docs that aren’t executed or checked have no feedback loop: renaming a parameter, adding a term or changing units breaks nothing, so nobody notices. Drift is predictable at the moment code changes, which is exactly when CI can catch it, by co-locating docs with code (docstrings, not wikis), executing examples, and validating signatures.

Writing for engineers

State units, frames and shapes explicitly ((N, 3) array, ECI frame, [km]), preconditions and failure behaviour, numerical accuracy and validity range, and references for the algorithm. Prefer examples to prose. Document why in design notes; the what should be clear from good names.

Versioning docs

Docs should match releases: build per version, record breaking changes in the CHANGELOG, and use deprecation warnings (warnings.warn(..., DeprecationWarning)) for a release cycle before removing an API.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The root cause of documentation drift is…
  2. 2. Before removing a public function you should…
  3. 3. Which belongs in a docstring for a state vector argument?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Documentation, measured

Exercise 1. The parser extracts the summary, 2 parameters (with types and descriptions including units), 1 return and 1 raise from orbital_period. The signature check reports nothing missing; a planted function with a renamed parameter reports (["y"], ["z"]). Doctests catch one stale example out of two. orbital_period(6778 km) = 92.6 min, matching its own example.

Exercise 2: auditing a small library. In version 1, docstring coverage is 75% (quaternion_norm undocumented). wrap_angle documents a parameter angle that was renamed to a and omits lo. slew_time omits the newly added settle_s, and its doctest still expects 90.0 although the function now returns 95.0 (2 of 3 doctests pass). After fixing the docstrings, coverage is 100%, there are no mismatches and all doctests pass. The generated markdown API reference lists all 4 functions with signatures and summaries.

🐍 Exercise 2 (research): audit and repair documentation drift
For a small module given as source, compute docstring coverage of public functions, find parameters that are undocumented or documented-but-gone, run the doctests, then fix the docstrings and confirm a clean audit; finally generate a markdown API reference from signatures and summaries.

Documentation in AI, robotics and aerospace

  • Interface control documents (ICDs) for telemetry and commands, generated from the same definitions as the code.
  • Model cards and dataset datasheets for ML components.
  • ROS package docs (rosdoc2) and Doxygen for C++ flight software.
  • Research software: a README with install, example and citation (CITATION.cff) makes results reusable.

References

  1. PEP 257 (docstring conventions); numpydoc docstring guide (numpydoc.readthedocs.io).
  2. D. Procida. Diátaxis: a systematic framework for technical documentation (diataxis.fr).
  3. Sphinx documentation (sphinx-doc.org): autodoc, napoleon; mkdocstrings documentation.
  4. Python documentation: doctest.
  5. G. Wilson et al. (2014). “Best practices for scientific computing.” PLOS Biology, 12(1).