Skip to content
The style guide is in beta: content and structure may change.

Code examples

A code example is often the first item a developer reads on a page, and a part they copy. These rules cover the code block itself: how to introduce it, how to format it, and how to keep it runnable when readers copy it. Code elements inside sentences and placeholder values have their own pages.

code-003 should GuidesAPI docsMarketing

Precede every code example with an introductory sentence or paragraph that explains what the example demonstrates and lists any requirements or dependencies.

Examples

  • ✓ The following example authenticates with an API key and lists your projects. It requires version 2.0 or later of the SDK:
  • ✗ Example:

Rationale

An introduction tells the reader what the example shows and what it needs before they read the code.

code-004 should GuidesAPI docsMarketing

End the introductory statement with a colon when it immediately precedes the code example, and with a period when intervening material (such as a note) separates the introduction from the example.

Examples

  • ✓ To create the bucket, run the following command:
  • ✗ To create the bucket, run the following command.
  • ✓ The following example streams the response. (a note separates this sentence from the code example, so it ends with a period)
code-010 should GuidesAPI docsMarketing

Code examples should be concise, start with simple cases, and build toward complexity; reserve complex multi-feature examples for tutorials with step-by-step explanations.

Rationale

Starting simple lets a reader grasp the basic case before complexity is layered on.

Options and replaceable values in code examples

Section titled “Options and replaceable values in code examples”
code-011 should GuidesAPI docsMarketing

In a code example, when a reader must choose a value, use a placeholder for the value. Describe the choices a reader can make in the body text that introduces the example, in the placeholder’s explanation, or in a code comment. Don’t mark choices or optional parts inside the code with brackets, braces, pipes, or ellipses. Use that notation only in a syntax description.

Examples

  • ✓ tool deploy APP_NAME --region=REGION (the list after the code block names the regions)
  • ✗ tool deploy APP_NAME [--region=REGION]
  • ✗ tool deploy APP_NAME --region={us-east1|europe-west1}
  • ✓ # To deploy outside your default region, add --region=REGION. (a comment above tool deploy APP_NAME)

Rationale

Notation such as brackets and pipes isn’t part of a command, so a reader who copies a code example has to find and remove the notation. A placeholder and an explanation tell the reader what to supply without breaking the code.

code-005 should GuidesAPI docsMarketingBlog

Limit the length of lines in code examples to under 80 characters so the examples render completely on mobile viewports, in side-by-side layouts, and in PDF exports. Wider lines are acceptable for long URLs, string literals, error messages, and structured formats such as JSON and YAML.

Examples

  • ✓ client.jobs.create(name="nightly-export", retries=3) (52 characters, renders cleanly everywhere)
  • ✗ A 140-character line chaining client.jobs.create(...).wait().download().save(...) that scrolls horizontally on mobile screens

Rationale

Lines under about 80 characters render cleanly on mobile screens, in side-by-side views, and in PDF, so it is a useful soft target. Long URLs, strings, and structured formats are reasonable exceptions where wrapping would hurt readability.

code-007 should GuidesAPI docsMarketing

Mark code blocks as preformatted text using the appropriate mechanism for the authoring format: the pre element in HTML, a code fence or four-space indent in Markdown.

Examples

  • ✓ <pre><code>npm install example-sdk</code></pre>
  • ✗ <p>npm install example-sdk</p>

Rationale

Marking code as preformatted preserves whitespace and stops the renderer from reflowing it.

code-006 should GuidesAPI docsMarketing

Follow the relevant language style guide for indentation; prefer spaces over tabs and use two spaces per indentation level unless the language convention differs.

Examples

  • ✓ A JavaScript code example indented two spaces per level
  • ✗ A JavaScript code example indented four spaces per level, against the language convention

Rationale

Following the language’s own conventions keeps code examples idiomatic and ready to copy.

code-002 should GuidesAPI docsMarketing

Indent HTML source with two spaces per level; do not use tabs.

Examples

  • ✓ <li> items indented two spaces inside their <ul>
  • ✗ <li> items indented with tab characters
code-001 should GuidesAPI docsMarketing

Limit HTML source line length to 80 characters, with defined exceptions for meta elements and long URLs.

Examples

  • ✓ An <a> tag wrapped after its class attribute so no source line passes 80 characters
  • ✗ <a class="btn btn-primary" href="https://example.com/docs/get-started" title="Get started with the API">Get started</a>

Rationale

Constrained line length keeps HTML source readable and diff-friendly.

code-008 should GuidesAPI docsMarketing

Indicate omitted code within a code example using a comment in the language’s own syntax rather than ellipsis characters or three dots.

Examples

  • ✓ // Omitted: client setup shown in the previous example
  • ✗ …

Rationale

A language-specific code comment is valid and easily identifiable in a code example, but users can misinterpret bare ellipses as syntax.

code-014 should GuidesAPI docsMarketing

In English documentation, write the comments in code examples in English. When you copy code from a project that’s commented in another language, translate its comments, but don’t change its identifiers or string literals, because readers type or match them. If you need to show the exact contents of a file, such as a shipped configuration file, keep its original comments and explain them in the body text of the documentation.

Examples

  • ✓ queue.flush(send) # Send the stored events, oldest first
  • ✗ queue.flush(send) # 古い順に送信する (this comment is in Japanese)
  • ✓ print(失敗メッセージ("ja")) # Print the failure message for Japanese (the function name matches the code)

Rationale

Comments in code examples explain details intended for the readers of the documentation, so they need to be translated. Identifiers and string literals are different because translating them causes errors in the code.

code-012 should GuidesAPI docsMarketing

Capitalize code elements according to the conventions of the programming language or API being documented.

Examples

  • ✓ Call getElementById() to find the element. (JavaScript)
  • ✗ Call GetElementByID() to find the element.

Rationale

Matching the casing of the programming language keeps the code correct, so readers can copy and run it.

code-009 should GuidesAPI docsMarketing

Do not inflect code element names to form plurals or possessives; instead, add a noun after the element name and inflect that noun (Intent objects, not Intents).

Examples

  • ✓ Delete the unused Intent objects.
  • ✗ Delete the unused Intents.

Rationale

Adding an English plural to a code name creates a token that is not the real identifier; following the code name with a noun keeps the name exact.