Stay organized with collections
Save and categorize content based on your preferences.
This page explains how to format code samples. For more information about formatting and
explaining code that appears in text, command-line syntax, and placeholders, see the following
resources:
Follow these guidelines when formatting code samples:
Follow the indentation guidelines in the relevant
code style guide. For most programming languages,
that means using spaces instead of tabs and using two spaces for each indentation level.
However, some contexts use four spaces for each indentation level, and some contexts use tabs.
This guidance applies to formatting code samples, not to
formatting commands.
Wrap lines at 80 characters.
If you expect readers to have a relatively narrow browser window or to print out your
document, consider wrapping at a smaller number of characters for readability.
Mark code blocks as preformatted text. In HTML, use a pre element;
in Markdown, indent every line of the code block by four spaces.
Indicate omitted code by using a comment in the syntax of the language of your code
sample. Don't use three dots or the ellipsis character (…). If a code
block contains an omission, don't format the block as click-to-copy.
Recommended:
<pre>
function helloWorld() {
alert('Hello, world! This sentence is so long that it wraps onto a second
line.');
}
</pre>
This renders the following code block:
function helloWorld() {
alert('Hello, world! This sentence is so long that it wraps onto a second
line.');
}
Recommended:
apiVersion:serving.knative.dev/v1kind:Service# Several lines of code are omitted here.spec:template:spec:containers:-image:IMAGE_URLports:-name:h2ccontainerPort:8080
Introductory statements
In most cases, precede a code sample with an introductory sentence or
paragraph. The introduction can end with a colon or a period; usually a colon if it
immediately precedes the sample, usually a period if there's more material (such
as a note paragraph) between the introduction and the sample, or if the
introduction paragraph ends in a sentence that isn't directly related to the
sample.
Recommended (ending with a period): The
following code sample shows how to use the get method. For
information about other methods, see [link]. [sample]
Also recommended: The following code
sample shows how to use the get method: [sample] For information about
other methods, see [link].
Not recommended (ending with a colon): The
following code sample shows how to use the get method. For
information about other methods, see [link]: [sample]
Some open source projects have their own overriding style guides. For
example, Java code in the Android Open Source Project follows the AOSP Java Code
Style for Contributors guide.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-22 UTC."],[[["\u003cp\u003eCode samples should follow indentation guidelines, typically using spaces instead of tabs and two spaces per indentation level.\u003c/p\u003e\n"],["\u003cp\u003eWrap code lines at 80 characters for better readability, especially in narrow browser windows or when printed.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003e<pre>\u003c/code\u003e element in HTML or four-space indentation in Markdown to format code blocks as preformatted text and indicate omissions with comments specific to the code's language.\u003c/p\u003e\n"],["\u003cp\u003eIntroduce code samples with a sentence or paragraph, ending with a colon if directly preceding the sample, or a period if there's intervening material.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the relevant code style guides, such as Google's or project-specific ones, for language-specific formatting conventions.\u003c/p\u003e\n"]]],["Code samples should adhere to language-specific indentation guidelines, typically using spaces (two per level), though some contexts use four or tabs. Wrap lines at 80 characters or fewer, especially for narrower displays. Format code blocks as preformatted text using `\u003cpre\u003e` in HTML or four-space indentation in Markdown. Indicate omitted code with language-specific comments, not ellipses. Introduce samples with a sentence ending in a colon or period. Use public Google coding style guides if there is not an existing style guide.\n"],null,["This page explains how to format code samples. For more information about formatting and\nexplaining code that appears in text, command-line syntax, and placeholders, see the following\nresources:\n\n- [Code in text](/style/code-in-text)\n- [Documenting command-line syntax](/style/code-syntax)\n- [Formatting placeholders](/style/placeholders)\n\nBasic guidelines\n\nFollow these guidelines when formatting code samples:\n\n- **Follow the indentation guidelines in the relevant\n [code style guide](#coding)**. For most programming languages,\n that means using spaces instead of tabs and using two spaces for each indentation level.\n However, some contexts use four spaces for each indentation level, and some contexts use tabs.\n\n This guidance applies to formatting code samples, not to\n [formatting commands](/style/code-syntax#formatting-a-command).\n- **Wrap lines** at 80 characters.\n\n If you expect readers to have a relatively narrow browser window or to print out your\n document, consider wrapping at a smaller number of characters for readability.\n- **Mark code blocks as preformatted text** . In HTML, use a `pre` element; in Markdown, indent every line of the code block by four spaces.\n- **Indicate omitted code by using a comment** in the syntax of the language of your code sample. Don't use three dots or the ellipsis character (`...`). If a code block contains an omission, don't format the block as click-to-copy.\n\nRecommended: \n\n```\n\u003cpre\u003e\nfunction helloWorld() {\n alert('Hello, world! This sentence is so long that it wraps onto a second\n line.');\n}\n\u003c/pre\u003e\n```\n\nThis renders the following code block: \n\n```\nfunction helloWorld() {\n alert('Hello, world! This sentence is so long that it wraps onto a second\n line.');\n}\n```\n\nRecommended: \n\n```yaml\napiVersion: serving.knative.dev/v1\nkind: Service\n# Several lines of code are omitted here.\nspec:\n template:\n spec:\n containers:\n - image: IMAGE_URL\n ports:\n - name: h2c\n containerPort: 8080\n```\n\nIntroductory statements\n\nIn most cases, precede a code sample with an introductory sentence or\nparagraph. The introduction can end with a colon or a period; usually a colon if it\nimmediately precedes the sample, usually a period if there's more material (such\nas a note paragraph) between the introduction and the sample, or if the\nintroduction paragraph ends in a sentence that isn't directly related to the\nsample. \nRecommended (ending with a period): The\nfollowing code sample shows how to use the `get` method. For\ninformation about other methods, see \\[link\\]. \\[sample\\]\n\nAlso recommended: The following code\nsample shows how to use the `get` method: \\[sample\\] For information about\nother methods, see \\[link\\].\n\nNot recommended (ending with a colon): The\nfollowing code sample shows how to use the `get` method. For\ninformation about other methods, see \\[link\\]: \\[sample\\]\n\nFor more information about how to introduce code samples, see\n[Document command-line syntax](/style/code-syntax).\n\nCode style guides\n\nThe following public Google coding-style guides are available on GitHub:\n\n- [C++ style guide](https://google.github.io/styleguide/cppguide.html).\n- [HTML/CSS style guide](https://google.github.io/styleguide/htmlcssguide.html).\n- [Java style guide](https://google.github.io/styleguide/javaguide.html).\n- [JavaScript style guide](https://google.github.io/styleguide/javascriptguide.xml).\n- [Python style guide](https://google.github.io/styleguide/pyguide)\n- [Full list of Google's programming style guides](https://google.github.io/styleguide/)\n\nSome open source projects have their own overriding style guides. For\nexample, Java code in the Android Open Source Project follows the [AOSP Java Code\nStyle for Contributors](https://source.android.com/setup/contribute/code-style) guide."]]