Building it · lesson 8 of 10
Ideas in this lesson
AREADME.mdthe front page of a repository
BDocstringa string at the top of a function saying what it does, for its users
C# why, not whata comment for the reader of the code

Documentation

Last updated

Jun joins Pila on a Monday. By lunchtime he should have it running on his laptop and know where the queue rules live, without interrupting Marco five times. That's what documentation is for. In this lesson you'll write Pila's README, add docstrings Python can show, and learn which comments help and which only repeat the code.

By the end of this lesson you can

  • Say who reads documentation, and what each reader needs
  • Write a README that gets a newcomer from nothing to running
  • Write docstrings, and comments that say why rather than what

Who reads it

Documentation has different readers, who need different things:

Pila’s readers
ReaderNeedsFinds it in
A new teammate, like JunRun it, find their way around, know how to contributeThe README
Someone calling a functionWhat it does, what it takes, what it returns, what can go wrongIts docstring
Someone changing the codeWhy it’s written this wayComments, commit messages, decision records (4.9)
Ate Cora and the counter staffHow to use PilaA one-page guide at the counter (lesson 8.5)
You, in six monthsAll of the above, because you’ll have forgottenEverywhere

Write Pila’s README

The README is the front page of a repository: hosting sites show it first. A good one answers, in order: what is this, how do I run it, how do I test it, where are things, and how do I help. Write Pila's; each part says what a newcomer can now do. It's kept in this browser and can be downloaded.

README.mdKept in this browser only
  • Says what it does (not yet)
  • Says who it’s for (not yet)
  • Makes a virtual environment (not yet)
  • Installs the dependencies (not yet)
  • Says the command that starts it (done)
  • Gives the test command (not yet)
  • Names at least three files or folders (not yet)
  • Mentions branches or pull requests (not yet)
  • Says where to ask or report problems (not yet)
Write each part until its checks pass.

The best test of a README is a newcomer following it word for word on a fresh computer. Every place Jun gets stuck is a missing line.

Docstrings

A docstring is a string as the first statement of a module, class or function. PEP 257 describes the conventions: one line saying what it does, as a command; then, if needed, a blank line and the details a caller needs: arguments, what's returned, what can go wrong. Python keeps docstrings, and help() shows them. Run this:

Step 3 code main.py
Edit the code, then run it again

Editors show the same text when you hover over a call to take_number, so callers rarely need to open the file.

Comments: why, not what

Code says what happens. A comment earns its place by saying what the code can't: why it's written this way, or what would break if someone changed it. Sort these comments from Pila's code.

0 of 5 placed right

  1. # add 1 to number above number += 1

  2. # Numbers restart at 1 each day; ids never repeat (see 4.5)

  3. # limit is 50 above DAILY_LIMIT = 60

  4. # The gateway retries for up to 24 hours, so never delete message ids sooner

  5. # loop over the tickets above for ticket in tickets:

Documentation lives in the repository, with the code, so a change to the code and to its documentation happen in the same commit and the same review. A reviewer who sees a behaviour change with no README change should ask about it (lesson 5.5).

Spot the trap: the comment that repeats the code

Spot the trapMarco

Marco’s pull request has a comment on nearly every line: # set the limit, # check the hours, # return the number. The one strange line, the hours check that uses < rather than <=, has none.

What happens if…

Check what you learned

What should a README let a newcomer do first?
What belongs in a docstring?
Which comment is worth keeping?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development