Google Java Programming Style guide-naming conventions

Source: Internet
Author: User
Tags uppercase letter

Naming conventions
5.1 rules that are common to all identifiers
Identifiers can only use ASCII letters and numbers, so each valid identifier name can match the regular expression \w+.
Special prefixes or suffixes used in other Google programming language styles, such as name_, Mname, S_name, and Kname, are in the Java programming style
No longer used.
5.2 Rules for identifier types
5.2.1 Package Name
The package name is all lowercase, and successive words are simply concatenated, without underlining.
5.2.2 Class Name
Class names are written in uppercamelcase style.
The class name is usually a noun or noun phrase, and the interface name can sometimes be an adjective or an adjective phrase. There are no specific rules or effective
Convention to name the annotation type.
The test class is named to start with the name of the class it is testing, ending with test. For example, Hashtest or hashintegrationtest.
5.2.3 Method Name
Method names are written in lowercamelcase style.
The method name is usually a verb or a verb phrase.
The underscore may appear in the JUnit test method name to separate the logical components of the name. A typical pattern.
Yes: test<methodundertest>_<state>, such as Testpop_emptystack. There is no single right way to name a test method.
5.2.4 Constant Name
The constant name mode is constant_case, all letters are capitalized, and the words are separated by underscores. So, what exactly is a constant?
Each constant is a static final field, but not all static final fields are constants. In determining whether a field is a constant
, consider whether it really feels like a constant. For example, if any one of the observed states of the instance is mutable, it is almost certainly not
will be a constant. Just never plan to change the object is generally not enough, it must be really unchanged in order to show it as a constant.
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 usually nouns or noun phrases.
5.2.5 Number of field names
The Lowercamelcase field name is written in the style.
These names are usually nouns or noun phrases.
5.2.6 Name of the parameter
The name of the parameter is written in lowercamelcase style.
Parameters should avoid naming with a single character.
5.2.7 local variable name
Local variable names are written in lowercamelcase style, and local variable names can have looser abbreviations than other types of names.
Although the abbreviations are looser, you should avoid naming them with single characters, except for temporary and cyclic variables.
Even if the local variable is final and immutable, it should not be shown as a constant, nor can it be named with constant rules.
5.2.8 type variable Name
Type variables can be named in one of the following two styles:
A single uppercase letter, followed by a number (such as: E, T, X, T2).
Named by Class (5.2.2), followed by an uppercase T (for example: Requestt, Foobart).
5.3 Camel-named method (CamelCase)
The camel-named method is divided into large hump-type nomenclature (uppercamelcase) and small hump-type nomenclature (lowercamelcase). Sometimes, we have more than one
A reasonable way to convert an English phrase into a hump form, such as an abbreviation or an unusual structure (such as "IPv6" or "IOS"). Google Specify
The following conversion scenarios.
The name begins with the prose form (prose form):
1. Convert the phrase to a pure ASCII code and remove any single quotes. For example: "Müller ' s Algorithm" will become "muellers
Algorithm ".
2. Divide the result into words, separated by spaces or other punctuation (often hyphens).
Recommendation: If a word already has a common hump representation, divide it by its composition (e.g. "AdWords" splits
"Ad words"). It is important to note that "IOS" is not a real hump representation, so the recommendation does not
Use.
3. Now all the letters are lowercase (including abbreviations), then capitalize the first letter of the word:
The first letter of each word is capitalized, and comes to a large hump-type name.
In addition to the first word, the first letter of each word is capitalized to get a small hump-type name.
4. Finally connect all the words together to get an identifier.
Example:
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
youtubeimporter*
Asterisking indicates yes, but not recommended.
Note: in English, some word forms with hyphens are not unique. For example: "NonEmpty" and "non-
Empty "is correct, so the method names Checknonempty and Checknonempty are also correct.

Google Java Programming Style guide-naming conventions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.