Code is Science is back: the journal code policy tracker now covers 24 journals.See the tracker

How to document research software: a practical guide beyond the README

The four kinds of documentation a research tool needs, how to write docstrings, tutorials and methods pages, which tools build them and how to keep them accurate.

PracticeBy the Code is Science editorsDec 2, 202613 min read
Line illustration of an open notebook with a bookmark ribbon and a stack of tabbed manual pages beside a laptop showing code lines and a laboratory flask
Key takeaways
  • A README is the front door, not the whole house: research tools also need a tutorial, how-to guides, a reference and an explanation of the method.
  • Docstrings with units, assumptions and paper references are the highest-value documentation a researcher can write, because tools turn them into the API reference.
  • Keep docs in the same repository as the code, build them with Sphinx, MkDocs or your language's standard tool, and publish a version per release.
  • Docs stay accurate only if they change in the same pull request as the code and are built and tested in continuous integration.

Documenting research software means writing down, in the places people will look, what the code is for, how to install and run it, what each function does, and why the method works the way it does. In short: keep a README as the front door, add a short tutorial and a few task-focused how-to guides, generate the API reference from docstrings, write one page that explains the method and its assumptions, keep all of it in the same repository as the code, and build it automatically on every change so it cannot drift.

Most research groups stop at the README, and for a script used by one person that is often enough. The trouble starts when the code gains users: a collaborator in another lab, a reviewer who has to run it, a new PhD student who inherits it, or a community that builds on it. At that point a README cannot answer every question, and the same questions start arriving by email. Good documentation is the cheapest way to make research code reusable, reviewable and citable.

Software teams outside academia have worked out this problem in detail, and their overview of the types of software documentation used in commercial projects (requirements, architecture, API, user and process documents) is a useful map for a lab tool that is growing into shared infrastructure. This guide takes the research side: which kinds of documentation a research tool actually needs, in what order to write them, which tools do the heavy lifting, and how to keep the result accurate as the code changes.

Why does research software need more than a README?

Research software needs more than a README because its users ask questions that a single page cannot hold. A README answers one question well: what is this, and how do I get started? The others look like this:

  • "How do I do X with it?" A user with a specific task needs a short recipe, not the full overview.
  • "What does this parameter mean?" Someone writing their own analysis needs every function, argument, unit and return value described precisely.
  • "Why does it compute it this way?" A reviewer or a skeptical colleague needs the method, the assumptions and the known limitations.
  • "How do I contribute or report a bug?" A community needs contribution rules and a support route.

When all of that is squeezed into the README, it becomes too long to read and still incomplete. When it is missing, users guess, and in research a wrong guess about a unit or a default can change a published result. A 2022 study in Scientific Reports on documenting research software in engineering science makes the same point: documentation is what lets other researchers judge whether software can be trusted and reused at all.

What types of documentation does a research tool need?

A research tool needs four kinds of documentation, each answering a different need. The split comes from Diátaxis, a framework created by Daniele Procida and now used by many open-source and scientific projects. Its central idea is that tutorials, how-to guides, reference and explanation serve different moments, and mixing them makes each one worse.

TypeQuestion it answersResearch exampleWhere it usually lives
Tutorial"Teach me to use this."Load the bundled sample dataset, run the pipeline end to end, inspect one figuredocs/tutorial or a notebook in examples/
How-to guide"How do I do this specific task?"Run the model on a cluster with Slurm; import data from a new instrument formatShort pages in docs/how-to/
Reference"What exactly does this do?"Every public function, argument, unit, default and output file formatGenerated from docstrings
Explanation"Why does it work this way?"The algorithm, numerical tolerances, assumptions, validation against published resultsdocs/explanation/ or a methods page

Alongside these four sit the project files that every repository needs regardless of size: README, LICENSE, CITATION.cff, CONTRIBUTING and a changelog. Our guide to writing a README for research code covers the front door in detail; this article is about everything behind it.

What is the minimum documentation for research code?

The minimum depends on who will use the code. Match the effort to the audience instead of writing a full manual for a script that only you will run.

LevelWho uses the codeWhat to write
MinimumYou, and a reviewer of one paperREADME with purpose, install, one worked example and expected output; licence; citation file; docstrings on public functions
RecommendedYour group and close collaboratorsPlus a tutorial, two or three how-to guides, a generated API reference and a changelog
Shared toolOther labs, a consortium or the publicPlus an explanation of the method, contribution and support guidelines, versioned docs online and a code of conduct

The minimum level is also what most journals now expect at submission. A code availability statement points to the repository; the repository then has to be usable by someone who has never met you. Our note on writing a code availability statement explains how the two fit together.

How should you document the code itself?

Use comments to explain why, and docstrings to describe what a function promises. Comments that restate the code ("add 1 to i") add noise; comments that record a reason ("offset by one because the instrument counts channels from 1") save the next reader an afternoon.

Docstrings are the most valuable documentation a researcher can write, because tools turn them into the reference automatically and editors show them on hover. For scientific Python, the numpydoc format is the common convention:

def growth_rate(counts, times, window=5):
    """Estimate the exponential growth rate from cell counts.

    Parameters
    ----------
    counts : array of float
        Cell counts per well, background already subtracted.
    times : array of float
        Measurement times in hours, same length as counts.
    window : int, optional
        Number of points in the sliding fit. Default 5.

    Returns
    -------
    float
        Maximum growth rate in 1/hour.

    Examples
    --------
    >>> growth_rate([10, 20, 40, 80, 160], [0, 1, 2, 3, 4])
    0.693...
    """

Three details matter more in research code than elsewhere:

  • Units. State them for every physical quantity, input and output. A unit mix-up is easy to make and hard to spot, because the code still runs and still returns a plausible number.
  • Assumptions. Say what the function expects about its input: sorted, normalised, background-subtracted, no missing values.
  • Sources. If a function implements an equation from a paper, cite the paper and the equation number in the docstring.

R users get the same result with roxygen2 comments, which generate the package help pages; Julia users write docstrings that Documenter.jl collects. The language changes, the habit does not.

How do you write tutorials and how-to guides for researchers?

Write the tutorial for a newcomer and the how-to guides for someone who already knows the basics. They look similar, but they fail in different ways when mixed.

A good tutorial for research software:

  1. Starts from a clean install and says which version it was tested with.
  2. Uses a small sample dataset shipped with the package or downloadable in one command, so nobody needs access to your lab's data.
  3. Walks through one complete analysis, from loading data to a result the reader can check against a printed value or a figure.
  4. Avoids options and side paths; those belong in how-to guides.
  5. Takes under an hour, ideally under twenty minutes.

How-to guides are short recipes for real tasks your users ask about: "process a batch of files", "run on a GPU", "export results to NetCDF", "reproduce Figure 3 of the paper". The questions in your inbox and issue tracker are the best list of guides to write. If you answer the same question twice, turn the answer into a page.

Notebooks work well for tutorials as long as they run from top to bottom without hidden state. Our guide to reproducible Jupyter notebooks covers how to keep them that way, and tools such as Jupyter Book or the Sphinx gallery extensions can render executed notebooks straight into the documentation site.

How do you produce an API reference without writing it by hand?

Generate it from the docstrings. A hand-written reference is out of date the week after it is written; a generated one changes whenever the code does.

  • Python: Sphinx with the autodoc and napoleon extensions reads numpydoc or Google-style docstrings; MkDocs users get the same with the mkdocstrings plugin.
  • R: roxygen2 builds the help files, and pkgdown turns a package into a documentation website with reference, articles and news pages.
  • Julia: Documenter.jl collects docstrings and can run the examples in them as tests.
  • C and C++: Doxygen parses specially formatted comments; many scientific projects pair it with Sphinx through the Breathe extension.
  • Command-line tools: make sure --help is complete, then include its output in the docs automatically rather than copying it by hand.

Document only the public interface in the reference. Internal helper functions can keep short docstrings for developers, but users should see the functions you promise to keep stable. If the code is a Python package, the layout from our guide to packaging Python research code makes that public/private split easy to express.

Where do the method and the design decisions go?

Put them in explanation pages: the part of the documentation most research projects skip and reviewers most often want. Explanation is where you say why the software computes what it computes.

A useful methods page covers:

  • The scientific model or algorithm, with the equations and the papers they come from.
  • Assumptions and the conditions under which results are not valid.
  • Numerical choices: solvers, tolerances, random seeds, precision, and how they affect results.
  • How the software was validated: against analytical solutions, published results or reference datasets, and how close the agreement is.
  • Known limitations and open issues.

For developers, record design decisions as they are made: one short note per decision with the context, the options considered and the choice. Teams call these architecture decision records; in a lab they can simply be dated Markdown files in docs/decisions/. Two years later, when someone asks why the data format is HDF5 and not Parquet, the answer is there instead of in a departed postdoc's memory.

Which documentation tools should you choose?

Choose the tool your language community already uses, because that is where your users and contributors will expect to find things.

ToolBest forSource formatNotes
SphinxPython and mixed-language projectsreStructuredText, Markdown via MySTMost powerful cross-referencing; the standard in scientific Python
MkDocsSmaller projects, Markdown-first teamsMarkdownEasy to start; mkdocstrings adds API reference
pkgdownR packagesroxygen2, R MarkdownBuilds a full site from an existing package
Documenter.jlJulia packagesMarkdown, docstringsRuns code examples as doctests
DoxygenC, C++, FortranAnnotated commentsOften combined with Sphinx
Jupyter BookTutorial-heavy and teaching materialNotebooks, MyST MarkdownExecutes notebooks during the build

For a new Python project with no strong preference, MkDocs is the quicker start and Sphinx the safer long-term bet if the project will grow. Either is fine; the expensive mistake is having no build at all.

Where should research software documentation live?

Keep the documentation sources in the same repository as the code, in a docs/ folder, and publish a built version online. The CodeRefinery documentation lesson recommends exactly this: versioned with the code, written in a plain-text markup, built by a tool.

  • Read the Docs builds Sphinx and MkDocs projects from a Git repository on every push and keeps a separate version of the docs for each release, which matters when a paper used version 1.2 and the current release is 3.0.
  • GitHub Pages or GitLab Pages host any static site; a continuous integration job builds the docs and publishes them.
  • The archived release should include the docs sources too. When you deposit a version with a DOI, as described in our guide to archiving code with a DOI, the documentation for that exact version travels with it even if the website disappears.

Link the documentation from the README, the repository description and the CITATION.cff file, whose format is documented at the Citation File Format site. Our guide on citing software in papers shows how that file feeds citations on GitHub and Zenodo.

How do you keep documentation from going stale?

Treat documentation as part of the code: change it in the same pull request, build it in continuous integration and test the examples. Stale documentation is worse than none, because readers trust it.

  1. Same pull request rule. A change that alters behaviour, arguments or outputs is not finished until the docs are updated. Add a checkbox to the pull request template.
  2. Build docs in CI. Fail the build on warnings such as broken cross-references or missing docstrings. Sphinx supports this with its warnings-as-errors option.
  3. Run the examples. Python's doctest, Documenter.jl and executed notebooks check that the code in the docs still produces the output shown.
  4. Check links. A link checker run weekly catches dead references to papers, datasets and external docs.
  5. Keep a changelog. One line per user-visible change, grouped by release. It is the page users read before upgrading, and it tells them which version of the docs to open.

Testing the docs and testing the code reinforce each other. Our guide to testing research code covers the test suite these documentation checks plug into.

What do journals and reviewers check in documentation?

Reviewers check whether a stranger can install the software, run it and confirm that it works. The clearest public example is the Journal of Open Source Software review checklist, whose documentation section asks for:

  • A statement of need: what problem the software solves and for whom.
  • Installation instructions with a clear list of dependencies.
  • Example usage, ideally on a real-world analysis problem.
  • Functionality documentation, such as API method documentation.
  • Automated tests or described manual steps to verify the software works.
  • Community guidelines for contributing, reporting issues and seeking support.

That list is a good self-check even if you never submit to JOSS. Our article on publishing research software in JOSS walks through the rest of that review, and the research code review checklist shows what a careful reviewer looks for in the code behind the docs.

When does a lab tool need product-grade documentation?

A lab tool needs product-grade documentation once people outside the group depend on it to keep working. A shared data portal, a public web service or a study app used by participants brings documents that a research script never needed:

  • Requirements: what the system must do for each kind of user, agreed with the people who fund and use it.
  • Architecture: components, data flows, where data is stored and who can access it.
  • Operations: how to deploy, back up, restore and monitor the service, and what to do when it is down.
  • Security and data protection: what an ethics board, a hospital or an institutional security review will ask to see.
  • User support: help pages written for non-programmers, not for collaborators who can read the source.

This is often the point where a group brings in a research software engineer or an outside development team, and the documentation becomes part of the handover. Whoever builds the system, insist that these documents live in the institution's repository next to the code, in the same plain-text formats as the rest of the docs, so the lab can keep maintaining them after the contract ends.

What to do this week: a documentation sprint for one tool

  1. Pick the one tool your group shares most, and list the last ten questions people asked about it.
  2. Check the README against the minimum level above; fix installation and the worked example first.
  3. Add docstrings with units and assumptions to every public function, starting with the most used.
  4. Set up Sphinx or MkDocs in docs/, generate the reference and publish it on Read the Docs or Pages.
  5. Turn the three most frequent questions into how-to guides, and write one page explaining the method.
  6. Add a docs build and a doctest run to continuous integration, and a "docs updated" checkbox to the pull request template.

Questions about documenting research software

What should research software documentation include?

At minimum a README with purpose, installation and a worked example, a licence, a citation file and docstrings on public functions. Shared tools add a tutorial, how-to guides, a generated API reference, a methods page and contribution guidelines.

What are the four types of software documentation?

Following the Diátaxis framework: tutorials that teach a newcomer, how-to guides for specific tasks, reference that describes every function precisely, and explanation that covers why the software works the way it does.

Should I use Sphinx or MkDocs for research code?

Use what your language community uses. Sphinx is the standard in scientific Python and handles large, mixed-language projects; MkDocs is quicker to set up for smaller Markdown-first projects. Both can generate an API reference from docstrings.

Where should I host documentation for research software?

Keep the sources in a docs folder in the code repository and publish a built site on Read the Docs or GitHub or GitLab Pages, with a separate version per release. Include the docs sources in each archived release with a DOI.

How do I stop documentation going out of date?

Update docs in the same pull request as the code, build them in continuous integration with warnings treated as errors, run the examples as doctests and publish versioned docs for each release.

Do journals require software documentation?

Many journals with code policies expect a usable repository, and software journals review documentation directly. The JOSS checklist asks for a statement of need, installation instructions, example usage, API documentation, tests and community guidelines.

Keep reading. More from the desk.