Customers & growth · Developer marketing

Code sample framework

Best for Early Startup to Startup

SAMPLESEXAMPLESPRACTICESPATTERNS

Use it when

Developers copy your example code to start using an API or SDK, and you need samples that run, read clearly and stay correct as the product changes.

Skip it when

  • Readers need a guided path from setup to a first working result. Use the Tutorial design framework, which calls on these samples.
  • You will write the code live in front of an audience. Use the Live coding framework.
  • You are planning a blog that publishes deep dives built around code. Use Technical content strategy.
  • You need the full set of pages around the code, from concepts to reference. Use Documentation framework.

What to apply first and what to use instead are listed below.

Approaches

Inline snippets

Code is written straight into the docs page as fenced code blocks.

  • Pro It is quick to write and suits short commands, payloads and queries.
  • Con It is hard to test automatically, so it goes stale as libraries change, and it can drop context the reader needs.

Snippets pulled from a tested repository

Docs pages pull named regions from code files in a repository that compiles and runs in automated checks.

  • Pro Every build checks the code, and readers can open the full file for context.
  • Con It needs a samples repository, snippet markers and a build that runs them, plus an agreement with the repository's owners.

Inputs

  • Use cases
    The tasks developers most often attempt with the API, ranked, from support tickets, search queries and API logs over the last 90 days.
  • Languages and versions
    The languages and SDK versions to support, with each language's code style guide.
  • Recommended approach
    The way your team judges best for each task, agreed with engineering where several ways exist.
  • Setup requirements
    Libraries, environment variables, keys and permissions needed to run each sample.
  • Test harness
    A build job that compiles and runs every sample against the current release.

Steps

  1. Pick the use cases to cover, and plan a set per language that runs from a minimal hello world sample through moderate to complex ones.
  2. Write each sample with descriptive names, only the code the task needs, and the team's recommended approach, following the language's style guide.
  3. Comment the lines that are not obvious with why they are there, and note any shortcut taken against production readiness.
  4. Write the setup a reader needs before running and the expected output after it. Where it prevents a common error, add a marked anti-example with the reason it fails.
  5. Put every sample under automated tests that run on each release, and reference them from the docs by named regions instead of line numbers.
  6. Assign an owner per language and log each sample's last passing run.

Output

A tested sample set for each language, with a register that tracks where each sample lives and when it last passed.

  • Use case
  • Language and SDK version
  • Complexity level
  • Setup needed
  • Expected output
  • Location, as repository path and docs page
  • Last passing test date
  • Owner

Done when

  • Each ranked use case has at least one sample in every target language.
  • Every sample builds and passes its test on the current release.
  • Each sample lists its setup and its expected output.
  • The set for each language includes a simple, a moderate and a complex sample.
  • No sample contains credentials or a flaw your security review has flagged.

Measure

Track the share of samples passing tests at every release. For 90 days after publishing, track copies of code blocks, time from new API key to first successful call, and support tickets that quote sample code. Compare with the previous 90 days. It worked when tickets quoting samples fall and time to first call drops against your own baseline.

Hypothetical example

Larkpay's payments API had samples pasted into its pages, and support kept seeing a deprecated call copied from them. The team ranked the top five tasks from tickets and logs, then wrote samples in Python and JavaScript, from creating a test charge up to refunds with webhooks. Each sample listed its keys and packages and showed the expected response. The code moved into a samples repository that ran on every SDK release, and pages pulled named regions from it. The first run caught two samples broken by the last SDK update. Tickets quoting sample code fell over the next quarter.

Common mistakes

  • Reusing unit tests as samples. Write samples whose only job is to show the reader how to do the task.
  • Shortening code with tricks or bad practice. Keep correctness first and cut only what the task does not need.
  • Commenting the obvious lines and skipping the confusing ones. Comment why the tricky lines exist.
  • Jumping from hello world straight to a full app. Add moderate samples in between.
  • Referencing snippets by line numbers. Use named regions so code changes never show the wrong lines.

Sources

By company stage

  1. Pre-IdeaNot applicableThere is no API or SDK for developers to copy from.
  2. IdeaUsefulA few working samples help first developers, but a full test set is heavy.
  3. Early StartupCriticalRunnable samples speed activation and are central to a developer channel.
  4. StartupCriticalTested samples scale developer onboarding and cut support load as usage grows.
  5. GrowthUsefulSamples remain valuable but are maintained by documentation teams.
  6. ScaleUsefulSamples are maintained across products, adding value without being the main concern.
  7. EnterpriseUsefulSample upkeep is routine work inside documentation programs and still needs tested, current code.

Apply first

Nothing. You can start here.

Use instead

  • Tutorial design framework

    Readers need a guided path from setup to a first working result. Use the Tutorial design framework, which calls on these samples.

  • Live coding framework

    You will write the code live in front of an audience. Use the Live coding framework.

  • Technical content strategy

    You are planning a blog that publishes deep dives built around code. Use Technical content strategy.

  • Documentation framework

    You need the full set of pages around the code, from concepts to reference. Use Documentation framework.

Recommended resources

No sponsored resources on this framework yet. Sponsored listings would be labelled here, never in search.

Progress is not saved until you have an account. Create a free account