Usage Examples That Make a README Worth Reading
Most people decide whether to use a package within a minute of opening its README, and what they look for is a code block. Not the badges, not the feature list, not the logo. This post is about that code block: what the first example should show, how many examples are enough, and how to stop them from quietly going stale.
The first example is a promise
The first snippet in a README tells a reader what the package feels like to use. If it needs fifteen lines of setup before anything happens, readers assume every task will feel like that. I try to make the first example the smallest complete program that does something real: an import, one call, and the visible result.
import { slugify } from "tiny-slug";
slugify("Hello, World!");
// => "hello-world"
That illustrative example is three lines, shows the import path, the function name and the output. A reader can copy it, run it and know within seconds whether the package does what they need.
What a good set of examples covers
After the first example, a handful of targeted snippets usually does more than a long tutorial. I aim for these:
- The common case with options. Show the two or three options people actually change, not every option that exists.
- The error case. What does a failure look like? Show the thrown error type or returned value so people can handle it.
- The other runtime or module format. If you support both
requireandimport, show both once. - One integration. A short snippet using the package inside a typical setting, without endorsing any particular framework.
Everything beyond that belongs in separate documentation pages, linked from the README.
Keeping examples from rotting
The biggest problem with README examples is not that they start out bad but that they drift. A function is renamed, an option changes default, and the README keeps advertising the old behaviour for months. A few approaches help:
- Run the examples in CI. Some ecosystems make this easy. Rust runs code blocks in doc comments as tests, and Python's
doctestmodule can check interactive examples. For JavaScript, extracting README code blocks into a test file with a small script works well. - Review the README in every pull request that changes the public API. Add a checkbox to the pull request template.
- Show output, not just input. An example that prints its result is easier to verify by eye, by a reviewer and by a test.
Things I leave out
I avoid claims nobody can check, such as "blazing fast" or "the best way to". I avoid listing every configuration flag in the README, because a reference table there becomes outdated faster than any other section. And I avoid examples that depend on network services that might not exist next year.
If you want a broader checklist for the rest of the README, such as installation, compatibility and licence, the resources page points to official packaging documentation for each ecosystem. The examples, though, are the part people read first, so they deserve the most care.