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:
| Reader | Needs | Finds it in |
|---|---|---|
| A new teammate, like Jun | Run it, find their way around, know how to contribute | The README |
| Someone calling a function | What it does, what it takes, what it returns, what can go wrong | Its docstring |
| Someone changing the code | Why it’s written this way | Comments, commit messages, decision records (4.9) |
| Ate Cora and the counter staff | How to use Pila | A one-page guide at the counter (lesson 8.5) |
| You, in six months | All of the above, because you’ll have forgotten | Everywhere |
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.
- 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)
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:
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
# add 1 to numberabovenumber += 1# Numbers restart at 1 each day; ids never repeat (see 4.5)# limit is 50aboveDAILY_LIMIT = 60# The gateway retries for up to 24 hours, so never delete message ids sooner# loop over the ticketsabovefor 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
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
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.
- PEP 257: Docstring Conventions: what a docstring says, and how it’s laid out
- PEP 8: Comments: comments that contradict the code are worse than none
- Python documentation: help(): Python shows a function’s docstring
- Google Engineering Practices: What to look for in a code review: comments should explain why, and documentation should change with the code