I recently learned that Google publishes coding conventions (Style Guide) for various languages. I took a quick look at Java Coding Standards and summarized some interesting points.
2 Source files basics
Whether to Unicode escape when writing non-ASCII characters in code depends on which one is easier to understand. (2.3.3 Non-ASCII characters)
String unitAbbrev = "μs"; // "\u03bcs"Since it is difficult to understand, it is described as Unicode.
3 Source file structure
ʻImport with the wildcard
* is not used regardless of whether it is
static` or not. (3.3.1 No wildcard imports)
4 Formatting
The brace {}
used in ʻifand
for` is not omitted even if the content is empty. (4.1.1 Braces are used where optional)
Non-empty blocks are described in K & R style as shown below. (4.1.2 Nonempty blocks: K & R style)
if (condition()) {
method();
} else if (condition()) {
method();
}
An empty block can be abbreviated as {}
unless it is composed of multiple blocks such as ʻif and
try`. (4.1.3 Empty Blocks: may be concise)
Two spaces are used for indentation. (4.2 Block indentation: +2 spaces)
Make sure that one line fits within 100 characters. (4.4 Colum limit: 100)
If one line is long, insert a space of 4 characters or more when starting a new line. (4.5.2 Indent continuation lines at least +4 spaces)
Insert a space between the reserved word (such as for
) and(between
,}
and the reserved word (such as ʻelse) before the
{`. (4.6.2 Horizontal white spaces)
5 Naming
Do not use prefixes or suffixes that have a special meaning. Example: name_
, mName
(5.1 Rules common to all identifiers)
Words that are generally composed of uppercase letters, such as abbreviations, are also made into camel case. Example: XmlHttpRequest
(5.3 Camel case: defined)
The name that has already spread in camel case may be left as it is. Example: YouTubeImporter
(5.3 Camel case: defined)