6.2 Rules by identifier type

6.2.1 Package names

Package names are all lowerCamelCase. For example,my.exampleCode.deepSpace, but not my.examplecode.deepspace or my.example_code.deep_space.

6.2.2 Class names

Class, interface, record, and typedef names are written in UpperCamelCase.Unexported classes are simply locals: they are not marked @private andtherefore are not named with a trailing underscore.

Type names are typically nouns or noun phrases. For example, Request,ImmutableList, or VisibilityMode. Additionally, interface names maysometimes be adjectives or adjective phrases instead (for example, Readable).

6.2.3 Method names

Method names are written in lowerCamelCase. Names for @private methods mustend with a trailing underscore.

Method names are typically verbs or verb phrases. For example, sendMessage orstop_. Getter and setter methods for properties are never required, but ifthey are used they should be named getFoo (or optionally isFoo or hasFoofor booleans), or setFoo(value) for setters.

Underscores may also appear in JsUnit test method names to separate logicalcomponents of the name. One typical pattern istest<MethodUnderTest><state><expectedOutcome>, for exampletestPop_emptyStack_throws. There is no One Correct Way to name test methods.

6.2.4 Enum names

Enum names are written in UpperCamelCase, similar to classes, and shouldgenerally be singular nouns. Individual items within the enum are named inCONSTANT_CASE.

6.2.5 Constant names

Constant names use CONSTANT_CASE: all uppercase letters, with words separatedby underscores. There is no reason for a constant to be named with a trailingunderscore, since private static properties can be replaced by (implicitlyprivate) module locals.

6.2.5.1 Definition of “constant”

Every constant is a @const static property or a module-local constdeclaration, but not all @const static properties and module-local constsare constants. Before choosing constant case, consider whether the field reallyfeels like a deeply immutable constant. For example, if any of that instance'sobservable state can change, it is almost certainly not a constant. Merelyintending to never mutate the object is generally not enough.

Examples:

  1. // Constants
  2. const NUMBER = 5;
  3. /** @const */ exports.NAMES = ImmutableList.of('Ed', 'Ann');
  4. /** @enum */ exports.SomeEnum = { ENUM_CONSTANT: 'value' };
  5. // Not constants
  6. let letVariable = 'non-const';
  7. class MyClass { constructor() { /** @const {string} */ this.nonStatic = 'non-static'; } };
  8. /** @type {string} */ MyClass.staticButMutable = 'not @const, can be reassigned';
  9. const /** Set<string> */ mutableCollection = new Set();
  10. const /** ImmutableSet<SomeMutableType> */ mutableElements = ImmutableSet.of(mutable);
  11. const Foo = goog.require('my.Foo'); // mirrors imported name
  12. const logger = log.getLogger('loggers.are.not.immutable');

Constants’ names are typically nouns or noun phrases.

6.2.5.2 Local aliases

Local aliases should be used whenever they improve readability overfully-qualified names. Follow the same rules as goog.requires(??), maintaining the last part of the aliased name.Aliases may also be used within functions. Aliases must be const.

Examples:

  1. const staticHelper = importedNamespace.staticHelper;
  2. const CONSTANT_NAME = ImportedClass.CONSTANT_NAME;
  3. const {assert, assertInstanceof} = asserts;

6.2.6 Non-constant field names

Non-constant field names (static or otherwise) are written in lowerCamelCase,with a trailing underscore for private fields.

These names are typically nouns or noun phrases. For example, computedValuesor index_.

6.2.7 Parameter names

Parameter names are written in lowerCamelCase. Note that this applies even ifthe parameter expects a constructor.

One-character parameter names should not be used in public methods.

Exception: When required by a third-party framework, parameter names maybegin with a $. This exception does not apply to any other identifiers(e.g. local variables or properties).

6.2.8 Local variable names

Local variable names are written in lowerCamelCase, except for module-local(top-level) constants, as described above. Constants in function scopes arestill named in lowerCamelCase. Note that lowerCamelCase is usedeven if the variable holds a constructor.

6.2.9 Template parameter names

Template parameter names should be concise, single-word or single-letteridentifiers, and must be all-caps, such as TYPE or THIS.

6.2.10 Module-local names

Module-local names that are not exported are implicitly private. They are notmarked @private and do not end in an underscore. This applies to classes,functions, variables, constants, enums, and other module-local identifiers.