Making Your Code Examples Shine - Larry Ullman - Write the Docs Portland 2018
Based on Write the Docs's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Treat code examples as part of the documentation’s user experience: map solutions to user problems rather than optimizing for snippet aesthetics.
Briefing
Code examples become genuinely useful—and easier to trust—when they’re built around user needs, enforced through consistent style, and maintained with automation that’s “just enough.” At Stripe, the documentation team treats code snippets as a first-class part of the product experience: they map solutions to what users are trying to accomplish, tailor examples to the languages and skill levels people actually use, and then add enhancements that make copy/paste work reliably.
The process starts with empathy and research, not syntax. Stripe’s documentation strategy begins by identifying which programming languages dominate among customers, then layering in experience level (from junior developers to people using higher-level tools) and the goals behind reading the docs. That research also extends to versioning and frameworks—such as choosing Flask for Python web examples—plus library and API versions. Code examples are shown in the most current versions, while the API reference flags when older versions might differ, because perfectly matching every user’s environment is ideal but expensive.
From there, Stripe focuses on “envisioning enhancements” that reduce friction and prevent confusion. A key improvement is making examples copy/paste-ready with the right credentials and data. When logged in, users see examples containing their own test API keys and test customer IDs; when logged out, they get demo-account equivalents. Stripe also uses interactive “Run” examples (via RunKit) so users can edit parameters like amounts, currency, or even Node version and immediately see results. For cases where real values can’t be prefilled—like Connect platform connected account IDs—placeholders are inserted using a template-style syntax (double curly braces), paired with guidance (mouse-over instructions) explaining what must be replaced before running.
Stripe then applies the same discipline used for written documentation to the code itself. The team maintains a code example style guide covering naming, parameter order, placeholder conventions, comment rules, and language-specific formatting and syntax (including indentation and quotation mark preferences). The style guide also functions as a feedback loop for engineers: code examples communicate what Stripe values—clean, modern best practices—and can reveal inconsistencies across products. Visual presentation matters too; well-formatted snippets can improve readability and break up “walls of text,” making the docs feel simpler and more trustworthy.
Finally, maintaining hundreds of examples across languages requires automation, but not blind cleverness. Stripe uses two complementary approaches: auto-generation on the fly from API reference structure, and a separate manual utility (written in Go) that generates syntactically valid, style-compliant examples from templates. Because correctness can’t be assumed, Stripe tests generated libraries against Stripe Mock (a GitHub project that uses the OpenAPI spec) and is beginning to test code examples as well. Linting and spell-checking help catch small errors, while “pitfalls” to avoid include letting docs and code drift apart and over-automating in ways that create hidden mismatches.
In short: build code examples like user-facing writing, standardize them with a style guide, and keep them accurate through targeted automation and testing—so developers can run what they copy without second-guessing what they’re seeing or whether it will work.
Cornell Notes
Stripe’s documentation team treats code examples as a user-facing product feature, not decorative snippets. The workflow begins with user research: identify the dominant languages, skill levels, goals, and relevant framework/library/API versions, then decide what the docs must support. Next comes “enhancements” that make examples immediately runnable—like showing different test credentials depending on whether a user is logged in, and using RunKit so users can edit inputs and see live results. To keep quality consistent across many languages, Stripe maintains a code example style guide and uses automation (auto-generated API-ref examples plus a Go-based template generator) to produce syntactically valid, style-compliant code. Correctness is supported through testing against Stripe Mock and additional linting, while avoiding doc/code drift and over-automation pitfalls.
How does user research shape what code examples should look like?
What enhancements make code examples easier to trust and copy/paste?
Why does a style guide matter for code examples, and what does it include?
How does Stripe maintain hundreds of code examples without drowning in manual work?
How does Stripe verify that generated code examples actually work?
What pitfalls threaten the accuracy of code examples over time?
Review Questions
- What specific user attributes (language, experience level, goals, versions) should be researched before writing code examples, and why?
- How do placeholders and interactive “Run” examples reduce confusion when real credentials or IDs can’t be prefilled?
- What mechanisms help prevent doc/code drift and ensure generated examples remain correct across languages?
Key Points
- 1
Treat code examples as part of the documentation’s user experience: map solutions to user problems rather than optimizing for snippet aesthetics.
- 2
Start with audience research—dominant languages, experience level, goals, and relevant framework/library/API versions—to decide what examples to support and how to present them.
- 3
Make examples runnable and trustworthy by tailoring credentials/data to logged-in vs logged-out users and by using interactive execution (RunKit) where possible.
- 4
Maintain a code example style guide that standardizes naming, parameter order, placeholders, comments, and language-specific formatting so consistency doesn’t depend on individual writers.
- 5
Use a combination of auto-generation and template-based generation to scale across many languages while still handling special cases accurately.
- 6
Validate correctness with testing against Stripe Mock and supplement with linting/spell-checking to catch small errors early.
- 7
Avoid doc/code drift by ensuring changes in one source trigger updates in the other, and don’t over-automate in ways that hide mismatches.