Naming - Geisonszo/TecProg-Emergo GitHub Wiki
Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores. Thus each valid identifier name is matched by the regular expression \w+ .
In Google Style special prefixes or suffixes, like those seen in the examples name_
, mName
, s_name
and kName, are not used.
Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace
, not com.example.deepSpace
or com.example.deep_space
.
Class names are written in UpperCamelCase.
Class names are typically nouns or noun phrases. For example, Character
or ImmutableList
. Interface names may also be nouns or noun phrases (for example, List
), but may sometimes be adjectives or adjective phrases instead (for example, Readable
).
There are no specific rules or even well-established conventions for naming annotation types.
Test classes are named starting with the name of the class they are testing, and ending with Test
. For example, HashTest
or HashIntegrationTest
Method names are written in lowerCamelCase.
Method names are typically verbs or verb phrases. For example, sendMessage
or stop
.
Underscores may appear in JUnit 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.
Constant names use CONSTANT_CASE
: all uppercase letters, with words separated by underscores. But what is a constant, exactly?
Every constant is a static final field, but not all static final fields are constants. Before choosing constant case, consider whether the field really feels like a 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
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }
// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};
These names are typically nouns or noun phrases.
Non-constant field names (static or otherwise) are written in lowerCamelCase.
These names are typically nouns or noun phrases. For example, computedValues
or index
.
Parameter names are written in lowerCamelCase.
One-character parameter names in public methods should be avoided.
Local variable names are written in lowerCamelCase.
Even when final and immutable, local variables are not considered to be constants, and should not be styled as constants.
Each type variable is named in one of two styles:
-
A single capital letter, optionally followed by a single numeral (such as
E
,T
,X
,T2
) -
A name in the form used for classes (see Section 5.2.2, Class names), followed by the capital letter
T
(examples:RequestT
,FooBarT
).
Sometimes there is more than one reasonable way to convert an English phrase into camel case, such as when acronyms or unusual constructs like "IPv6" or "iOS" are present. To improve predictability, Google Style specifies the following (nearly) deterministic scheme.
Beginning with the prose form of the name:
-
Convert the phrase to plain ASCII and remove any apostrophes. For example, "Müller's algorithm" might become "Muellers algorithm".
-
Divide this result into words, splitting on spaces and any remaining punctuation (typically hyphens).
- Recommended: if any word already has a conventional camel-case appearance in common usage, split this into its constituent parts (e.g., "AdWords" becomes "ad words"). Note that a word such as "iOS" is not really in camel case per se; it defies any convention, so this recommendation does not apply.
-
Now lowercase everything (including acronyms), then uppercase only the first character of:
-
... each word, to yield upper camel case, or
-
... each word except the first, to yield lower camel case
-
-
Finally, join all the words into a single identifier.
Note that the casing of the original words is almost entirely disregarded. Examples:
Prose form | Correct | Incorrect |
---|---|---|
"XML HTTP request" | XmlHttpRequest |
XMLHTTPRequest |
"new customer ID" | newCustomerId |
newCustomerID |
"inner stopwatch" | innerStopwatch |
innerStopWatch |
"supports IPv6 on iOS?" | supportsIpv6OnIos |
supportsIPv6OnIOS |
"YouTube importer" |
YouTubeImporter or YoutubeImporter*
|
*Acceptable, but not recommended.
Note: Some words are ambiguously hyphenated in the English language: for example "nonempty" and "non-empty" are both correct, so the method names checkNonempty and checkNonEmpty are likewise both correct.