Google employs thousands of Java programmers that collectively maintain many millions of lines of Java code. So they need a comprehensive style guide to quickly resolve any dispute. It's a brilliant document that succinctly and unambiguously describes how to write stylish Java code .
Highlight: UTF-8 encoding
Googlers aren't limited to ASCII in their source files. It's 2014 after all.
String unitAbbrev = "μs";
Highlight: But what is a constant, exactly?
The guide explains the difference between static fields and constants, ending the LOGGER
vs. logger
lumberjack fight.
Highlight: Expect the exceptional
Testing what happens when your code throws? No need for much ceremony; just catch expected and carry on.
try {
emptyStack.pop();
fail();
} catch (NoSuchElementException expected) {
}
Lowlight: Mandatory braces
You must equip all of your control flow with curly braces. The following is taboo:
if (empty() == 0) throw new EOFException();
if (byteCount == 0) return 0;
At Google, Java programmers must spread each check over three lines:
if (empty() == 0) {
throw new EOFException();
}
if (byteCount == 0) {
return 0;
}
All that extra scrolling will make your wrists hurt!
Overall it's a great doc. Satisfying this guide is certainly sufficient for the open source projects I maintain at Square.