Naming Conventions
Rules common for all Identifiers
Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores and very rarely (when required by frameworks like Angular) dollar signs.
- Give as descriptive a name as possible, within reason.
- Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader.
- Do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.
priceCountReader // No abbreviation.
numErrors // "num" is a widespread convention.
numDnsConnections // Most people know what "DNS" stands for.
Illegal:
n // Meaningless.
nErr // Ambiguous abbreviation.
nCompConns // Ambiguous abbreviation.
wgcConnections // Only your group knows what this stands for.
pcReader // Lots of things can be abbreviated "pc".
cstmrId // Deletes internal letters.
kSecondsPerDay // Do not use Hungarian notation.
Rules by Identifier Type
Package names
Package names are all lowerCamelCase
. For example, my.exampleCode.deepSpace
, but not my.examplecode.deepspace
or my.example_code.deep_space
.
Class names
Class, interface, record, and typedef names are written in UpperCamelCase
. Unexported classes are simply locals: they are not marked @private
and therefore are not named with a trailing underscore.
Type names are typically nouns or noun phrases. For example, Request
, ImmutableList
, or VisibilityMode
. Additionally, interface names may sometimes be adjectives or adjective phrases instead (for example, Readable
).
Method names
Method names are written in lowerCamelCase
. Private methods’ names must end with a trailing underscore.
Method names are typically verbs or verb phrases. For example, sendMessage
or stop_
. Getter and setter methods for properties are never required, but if they are used they should be named getFoo
(or optionally isFoo
or hasFoo
for booleans), or setFoo(value)
for setters.
Underscores may also appear in JsUnit test method names to separate logical components of the name. One typical pattern is test<MethodUnderTest>_<state>
, for example testPop_emptyStack
. There is no One Correct Way to name test methods.
Enum names
Enum names are written in UpperCamelCase
, similar to classes, and should generally be singular nouns. Individual items within the enum are named in CONSTANT_CASE
.
Constant names
Constant names use CONSTANT_CASE
: all uppercase letters, with words separated by underscores. There is no reason for a constant to be named with a trailing underscore, since private static properties can be replaced by (implicitly private) module locals.
Definition of “constant”
Every constant is a @const
static property or a module-local const
declaration, but not all @const
static properties and module-local consts
are constants. Before choosing constant case, consider whether the field really feels like a deeply immutable constant. For example, if any of that instance’s observable state can change, it is almost certainly not a constant. Merely intending to never mutate the object is generally not enough.
Examples:
// Constants
const NUMBER = 5;
/** @const */ exports.NAMES = ImmutableList.of('Ed', 'Ann');
/** @enum */ exports.SomeEnum = { ENUM_CONSTANT: 'value' };
// Not constants
let letVariable = 'non-const';
class MyClass { constructor() { /** @const */ this.nonStatic = 'non-static'; } };
/** @type {string} */ MyClass.staticButMutable = 'not @const, can be reassigned';
const /** Set<String> */ mutableCollection = new Set();
const /** ImmutableSet<SomeMutableType> */ mutableElements = ImmutableSet.of(mutable);
const Foo = goog.require('my.Foo'); // mirrors imported name
const logger = log.getLogger('loggers.are.not.immutable');
Constants’ names are typically nouns or noun phrases.
Local aliases
Local aliases should be used whenever they improve readability over fully-qualified names. Follow the same rules as goog.requires (3.4 goog.require statements), maintaining the last part of the aliased name. Aliases may also be used within functions. Aliases must be const.
Examples:
const staticHelper = importedNamespace.staticHelper;
const CONSTANT_NAME = ImportedClass.CONSTANT_NAME;
const {assert, assertInstanceof} = asserts;
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, computedValues
or index_
.
Parameter names
Parameter names are written in lowerCamelCase
. Note that this applies even if the 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 may begin with a $. This exception does not apply to any other identifiers (e.g. local variables or properties).
Local variable names
Local variable names are written in lowerCamelCase
, except for module-local (top-level) constants, as described above. Constants in function scopes are still named in lowerCamelCase
. Note that lowerCamelCase applies even if the variable holds a constructor.
Template parameter names
Template parameter names should be concise, single-word or single-letter identifiers, and must be all-caps, such as TYPE
or THIS
.