Code in text

In ordinary text sentences (as opposed to, say, code samples), use code font to mark up most things that have anything to do with code.

  • In HTML, use the <code> element.
  • In Markdown, use backticks (`).

For information about choosing HTML or Markdown, see Markdown versus HTML.

This page explains how to format code in ordinary text sentences. For more information about formatting and explaining placeholders, command-line syntax, and code samples, see the following resources:

Some specific items to put in code font

The following list includes items that should be in code font, but it's not an exhaustive list.

  • Attribute names and values
  • Class names
  • Command output (for example, nameserver 169.254.169.254)
  • Command-line utility names, such as gcloud, gsutil, kubectl, and bq
  • Data types
  • Defined (constant) values for an element or attribute
  • DNS record types
  • Enum (enumerator) names
  • Environment variable names
  • Element names (XML and HTML)

    Place angle brackets (<>) around the element name; you might have to escape the angle brackets to make them appear in the document.

  • Filenames, filename extensions (if used), and paths
  • Folders and directories
  • HTTP verbs
  • HTTP status codes
  • HTTP content-type values
  • IAM role names (for example, roles/storage.admin)

  • Language keywords
  • Method and function names
  • Namespace aliases
  • Placeholder variables
  • Query parameter names and values
  • Strings (such as URLs or domain names) that are used in commands and code:

    Recommended: In IAM, a condition can specify a page that only Human Resources admins can access—for example, https://hr.example.com.

    Recommended: The logID field includes the domain corpaudits.example.com.

  • Text input (for example, my-instance)
  • UI elements that are rendered based on previously entered text input

    For example, if a reader was instructed in a procedure to enter an instance name as my-instance, when you tell them to click the instance name, use code font and bold: Click my-instance.

Generally, don't put quotation marks around code unless the quotation marks are part of the code.

Items to put in ordinary (non-code) font

The following list includes items that should not be in code font, but it's not an exhaustive list.

  • Domain names.
  • URLs that the reader is supposed to follow in a browser.

    However, it's usually best to format a URL as a link and use descriptive link text instead of exposing the URL itself. For more information, see Link text.

  • Names of products, services, and organizations.

Items that are sometimes in code font

The following list includes items that are sometimes in code font, but it's not an exhaustive list.

  • Often, command-line utility names are spelled the same as the software project or product with which they are associated, with only differences in capitalization. In such cases, use code font for the command and ordinary font for the name of the project or product.

    Recommended:

    • Invoke the GCC 8.3 compiler using gcc for C programs or g++ for C++ programs.
    • To send the file over FTP with IPv6, use ftp -6.
    • The options for the curl command are explained on the curl project website.
    • The apt program includes commands from the apt-get and apt-cache programs for working with APT packages.
  • If you want the reader to use the email address as computer input or output, use code font. If you want the reader to treat the email address as a way to contact someone or a reference to someone, use non-code font and hyperlink the email address.

    Recommended:

    • Enter the username, not the full email address. For example, enter alex, not alex@example.com.
    • For help, contact support@example.com.

Method names

When you refer to a method name in text, omit the class name except where including it would prevent ambiguity.

Recommended: To retrieve the zebra's metadata, call its get() method.

Not recommended: To retrieve the zebra's metadata, call its animal.get() method.

Put an empty pair of parentheses after a method name to indicate that it's a method.

HTTP status codes

To refer to a single status code, use the following formatting and phrasing:

an HTTP 400 Bad Request status code

In particular, call it a status code instead of a response code or error code, and put the number and the name in code font. If the HTTP is implicit from context, you can leave it out.

To refer to a range of codes, use the following form:

an HTTP 2xx or 400 status code

In particular, use Nxx (with a specific digit in place of N) to indicate anything in the N00-N99 range, and put the status code number in code font even if you're leaving out the code's name.

If you prefer to specify an exact range, you can do so:

an HTTP status code in the 200-299 range

Here, too, put the numbers in code font.

Code style guides

The following Google coding-style guides are available on GitHub:

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.

Grammatical treatment of code elements

In general, don't use code elements such as keywords and filenames as if they were English verbs or nouns. Don't inflect the name of a code element, such as to make it plural or possessive. Instead, include a noun after the name of the code element, and inflect that noun.

Recommended Not recommended
The ADDRESS constant's value is defined in the settings.h file. ADDRESS's value is defined in settings.h.
To add the data, send a POST request. POST the data.
To retrieve the data, send a GET request. Retrieve information by GETting the data.

You can't close the file before opening it.

You can't call the close() method for a file before you call open().

Close()ing the file requires you to have open()ed it first.

Takes an array of extended ASCII code points (an array of INT64 values) and returns BYTES values.

For STRING arguments, returns the original string with all alphabetic characters in uppercase.

Takes an array of extended ASCII code points (ARRAY of INT64) and returns BYTES.

Exception: When documenting a Java API, it's common to leave out words like object or instance and instead use the class name as a noun: store the MyClass you got from the ClassFactory. If you need to pluralize such nouns, then add object or instance: store the MyClass objects you got from the ClassFactory instances.

Linking API terms in Android

When you're writing code comments that you'll turn into generated reference documentation, link to all elements of Android APIs, such as classes, methods, constants, and XML attributes. Use code font and regular HTML <a> elements to link to this reference material.

Link AndroidManifest.xml elements and attributes to the API guide pages. Link the attribute for a particular widget or layout to its Javadoc in the widget or layout's API reference entry.

Recommended:

<a href="/guide/topics/manifest/data-element.html">data</a>

Very common classes such as Activity and Intent don't need to be linked every time. If you use a term as a concept rather than a class, then don't put it in code font and don't capitalize it. Here are some objects that do not always require Javadoc links or capitalization:

  • activity, activities
  • service
  • fragment
  • view
  • loader
  • action bar
  • intent
  • content provider
  • broadcast receiver
  • app widget

If you use one of these terms in the context of referring to an actual instance, use the formal class name and link to its reference page. Here are two examples:

Recommended: The Activity class is an important part of an application's overall lifecycle...

Recommended: The user interface for an activity is provided by a hierarchy of views—objects derived from the View class.

To link to a class or method:

  • To link to a class, use the class name as link text—for example:

    <a href="/reference/android/widget/TextView">TextView</a>
  • To link to a method, use the method name as a fragment identifier. If you're linking to a static method, also include the class name in the link text. If you need to distinguish between overloaded versions of a particular method, consider showing the full signature—for example:

    <a href="/reference/android/app/Activity.html#onCreate(android.os.Bundle)">onCreate(Bundle)</a>
  • To link the attribute for a particular widget or layout to its Javadoc in the widget or layout's API reference entry, use the URL for the page, and then add the fragment identifier #attr_android:ATTRIBUTE_NAME. For example, to link to the XML attribute android:inputType for the TextView widget, add the following:

    <a href="/reference/android/widget/TextView.html#attr_android:inputType>inputType</a>