Lesson 5 of 9 · 25m
Code Examples as Contracts
Examples teach the interface you actually support. Make them runnable, complete, secure, and explicit about what can be changed.
A code example is not decoration. Users copy it, adapt it, and then reasonably expect the behavior it demonstrates. That makes examples part of your compatibility surface. A sloppy example creates support load just as surely as a sloppy API — often more, because examples bypass the type checker and the compiler.
Complete enough beats minimal enough
The most common bad example is a fragment with invisible context:
client.create_user(email)What is client? Which import? Which credential? Is the call synchronous? A complete minimal example costs a few lines and removes the guesswork:
import os
import httpx
API_TOKEN = os.environ["EXAMPLE_API_TOKEN"]
response = httpx.post(
"https://api.example.test/v1/users",
headers={"Authorization": f"Bearer {API_TOKEN}"},
json={"email": "operator@example.test", "role": "viewer"},
timeout=10.0,
)
response.raise_for_status()
print(response.json()["id"])Use reserved domains such as example.test for examples that should not be pasted against real systems. Use realistic shapes for everything else: real header names, real status handling, real timeouts.
Mark the variable parts
Readers need to know what is fixed protocol and what is local choice. Naming helps:
export PROJECT_ID="prod-payments-1842"
export REGION="us-east1"
gcloud run deploy billing-api --project "$PROJECT_ID" --region "$REGION" --image "us-east1-docker.pkg.dev/$PROJECT_ID/services/billing-api:2026.11.30" --no-trafficbilling-api is a name the reader may choose; --no-traffic is a safety-critical behavior they should not casually remove. If a flag looks optional but is required in production, annotate it.
Show failure handling when failure is normal
Documentation examples often demonstrate the impossible happy path. Network calls need timeouts, retries need idempotency keys, and file writes need cleanup. Do not turn every example into a production framework, but do not silently teach dangerous defaults either.
response = httpx.post(url, json=payload, timeout=10.0)
if response.status_code == 409:
print("already provisioned; treating as success")
else:
response.raise_for_status()That one branch communicates idempotency better than a paragraph of advice.
Security details are part of the interface
Examples that disable verification become production incidents:
# Do not do this outside a controlled local test harness.
client = httpx.Client(verify=False)If you must show an insecure path for local testing, make the warning adjacent and specific. Better: provide a secure local test path so nobody needs the insecure one. Never use real-looking API keys; obvious placeholders such as EXAMPLE_API_TOKEN signal that substitution is required.
Examples need tests like code
The strongest workflow is to extract examples from tested source or run them in CI. Even when full execution is impossible, syntax-check them. A Python example that cannot parse and a shell example with a broken line continuation erode trust immediately.
Field notes
- The best length is "shortest complete" — not shortest possible.
- One canonical example beats five variants unless the variants represent genuinely different environments.
- Keep comments sparse but meaningful: explain constraints and substitutions, not obvious syntax.
- When the API changes, grep the docs for old field names; examples rarely fail compile checks even when they are semantically obsolete.
Was this lesson helpful?