Code samples that work when pasted

Readers skip your prose and copy your code. Eight rules for samples that run the first time, and a way to stop them breaking between releases.

Sahil Gadimbayli

Founder & CTO

Writing docs4 min read

Watch a developer read docs and you'll see the same pattern. Their eyes skip the paragraphs and land on the first code block. They copy it, paste it, run it, and only go back to the prose when it fails.

That makes your code samples the most-read part of your docs, and the part with the least room for error. A sentence with a typo still makes sense. A sample with a typo is a stack trace, and the reader's trust in every other page drops with it.

1. Make it complete

The most common broken sample is one that works only in the writer's head:

const payment = await client.payments.create({ amount: 1000 })

Where does client come from? Which package? Is amount in cents? The writer knows. The reader pastes it and gets client is not defined.

Include the imports, the setup and the call, even if it makes the block longer:

import Northwind from '@northwind/pay'

const client = new Northwind(process.env.NORTHWIND_API_KEY)

// amount is in cents: this is 10.00 EUR
const payment = await client.payments.create({ amount: 1000, currency: 'eur' })
console.log(payment.status)

Six lines instead of one, and every one of them runs.

2. Mark what the reader must change

Placeholders should be impossible to miss and impossible to run by accident. YOUR_API_KEY in capitals, or an environment variable with a clear name, both work. A realistic-looking fake key doesn't, because the reader can't tell whether it's an example or a working test key.

Say where to find the real value, right next to the sample: "Your test key is under Settings → API keys."

3. Show the output

After the sample, show what a successful run prints:

succeeded

It's one line, and it answers the reader's next question: did that work? Without it, they can't tell a silent success from a silent failure.

4. Keep commands and output apart

A shell block that mixes commands and their output can't be pasted:

$ npm install @northwind/pay
added 1 package in 2s
$ node pay.js

Put the commands in one block, without the $, and the output in a second block. Readers who copy the first get exactly what they need to run.

5. One language per block, and say which

Label every block with its language so it gets highlighted, and so a reader skimming for Python doesn't paste JavaScript. If you support several languages, give each its own block or tab rather than one sample with comments like # or in Ruby:.

6. No ellipses in code people copy

... in the middle of a sample means "you fill this in" to the writer and is a syntax error to everyone else. If part of the sample doesn't matter, leave it out and say so in the sentence before. If it does matter, write it.

7. Pin the versions

"Install the library" works until the next major version renames a method. Name the version the sample was written against, at least in the install command. Readers on an older or newer version then know why the sample doesn't match.

8. Keep samples short enough to read

Complete isn't the same as long. If a sample needs 60 lines, it's usually two ideas: setting up and doing the thing. Split them, or link to a full example project and show only the lines that matter on the page.

Stop samples from rotting

Every rule above helps on the day you publish. The harder problem is month six, when the API has changed and nobody remembered the docs.

The fix is to treat samples as code. Keep them in files that your docs include, or extract them from the Markdown in CI, and run them against a test account. Many languages have a tool for this; Python's doctest and Rust's documentation tests are the best-known.

If that's too much to set up today, start smaller. Put a link to each sample's source file in a comment, and add "run the quickstart samples" to your release checklist. A sample someone ran last week is worth 10 that looked right when they were written.

The test

Copy every code block on your quickstart, in order, into a fresh project on a clean machine. Change only what the page tells you to change. If you get to the end without opening another tab, your samples work. If you don't, you've found the exact line where your readers give up.

Writing docs4 min read

Every docs page is somebody's first page

Readers land in the middle of your docs from search, assistants and support links. Six habits that make any page work as the first one they see.

Sahil Gadimbayli

Writing docs4 min read

Document the task, not the screen

Docs that walk through every field on a settings page answer questions nobody asks. How to find the tasks your readers came for and write pages around them.

Sahil Gadimbayli