Get AI summaries of any video or article — Sign up free
Making Your Code Examples Shine - Larry Ullman - Write the Docs Portland 2018 thumbnail

Making Your Code Examples Shine - Larry Ullman - Write the Docs Portland 2018

Write the Docs·
6 min read

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.

TL;DR

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?

The research starts by identifying which programming languages users use most (including relative proportions), then assessing experience level so examples match the audience without defaulting to the lowest common denominator. It also considers users’ goals—what they’re trying to do when they open the docs—and whether they’re developers only or also decision-makers who may prefer guided paths. Stripe expands this beyond language names to include language versions (e.g., Python 2 vs Python 3) and frameworks (e.g., Flask for Python web examples). It also accounts for library and API versions: examples are shown in the most current versions, while the API reference flags when older versions may differ.

What enhancements make code examples easier to trust and copy/paste?

Stripe makes examples runnable with the right data. Logged-in users see examples containing their own test API keys and test customer IDs; logged-out users see demo-account equivalents, so copy/paste still returns real objects. For interactive docs, RunKit provides a complete “create a charge” example with a Run button that executes a request and shows results; users can edit parameters like amount, currency, and Node version. When real values can’t be prefilled (such as Connect connected account IDs), Stripe uses placeholder templates (double curly braces) plus guidance (mouse-over instructions) telling users to replace the placeholder before running.

Why does a style guide matter for code examples, and what does it include?

A style guide prevents inconsistency across many writers and languages. Stripe’s guide covers general rules like naming conventions, parameter order, how API keys and placeholders are referenced, and when comments should be used. Comments are primarily for non-obvious cases or when code won’t copy/paste perfectly. It also includes language-specific rules: variable/function naming, syntax choices (like Ruby hash rocket vs Ruby JSON parentheses), quotation mark preferences, and indentation rules. The guide even points writers to established references (e.g., Chicago Manual of style for cases not covered, and PEP 8 for Python).

How does Stripe maintain hundreds of code examples without drowning in manual work?

Stripe uses automation in two layers. One approach auto-generates code examples on the fly from the API reference structure, producing syntactically valid snippets for each method and language. When auto-generation can’t capture special cases, Stripe overrides with hard-coded files. A second approach is a manual utility written in Go that generates examples from templates (API keys, token references, variable creation, object/method, and parameters). This tool outputs examples that already adhere to the style guide and are ready to paste into docs, and it can be regenerated if templates change or if new languages are added.

How does Stripe verify that generated code examples actually work?

Stripe tests generated libraries against Stripe Mock, a GitHub project that uses the OpenAPI spec to model Stripe’s API and can run locally. The team is also starting to test code examples themselves. Beyond runtime testing, linting and spell-checking catch common mistakes; Stripe notes linting exists in parts of the API ref but has room to grow. The goal is to confirm that examples return valid results and to catch small errors before users do.

What pitfalls threaten the accuracy of code examples over time?

A major risk is doc/code drift: documentation text may come from one source while code examples come from another, and renaming or changing values can leave mismatches. Stripe recommends simple safeguards like adding comments that warn maintainers to update related doc content when code changes. Another pitfall is over-automation—making systems too clever can create hidden inconsistencies—so automation should be applied “within reason.”

Review Questions

  1. What specific user attributes (language, experience level, goals, versions) should be researched before writing code examples, and why?
  2. How do placeholders and interactive “Run” examples reduce confusion when real credentials or IDs can’t be prefilled?
  3. What mechanisms help prevent doc/code drift and ensure generated examples remain correct across languages?

Key Points

  1. 1

    Treat code examples as part of the documentation’s user experience: map solutions to user problems rather than optimizing for snippet aesthetics.

  2. 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. 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. 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. 5

    Use a combination of auto-generation and template-based generation to scale across many languages while still handling special cases accurately.

  6. 6

    Validate correctness with testing against Stripe Mock and supplement with linting/spell-checking to catch small errors early.

  7. 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.

Highlights

Stripe tailors code examples to the user’s context: logged-in users see their own test API keys and test customer IDs, while logged-out users get demo-account equivalents—both are copy/paste runnable.
RunKit turns static snippets into interactive examples where users can edit inputs (amount, currency, Node version) and immediately see API results.
A dedicated code example style guide standardizes everything from placeholder conventions to indentation and quotation rules across languages, and it doubles as a feedback channel for engineers.
Stripe uses two generation strategies: on-the-fly auto-generation from API reference structure and a Go-based template generator that outputs style-compliant, syntactically valid examples.
Stripe Mock (OpenAPI-spec-driven) supports local testing of libraries, and the team is expanding testing to code examples themselves.

Topics

Mentioned

  • Stripe
  • RunKit
  • Stripe Mock
  • Larry Ullman
  • API
  • OpenAPI