The Android Open Source Project has very strict rules for code style which we should follow as well to make future merges easier and upstream contribution acceptable.
This page summarizes the most important parts of AOSP guidelines and also the most common errors we see on our code review page.
Clean code style is easily achievable by correctly setting up your IDE – either IntelliJ IDEA or Eclipse.
Not all existing code follows these rules, but all new code is expected to.
Thank you for your contribution!
Line Length Limit
Each line of text in your code should be at most 100 characters long.
Brace style
Braces are required around the statements for a conditional. Except, if the entire conditional (the condition and the body) fit on one line, you may (but are not obligated to) put it all on one line. That is, this is legal:
if (condition) {
body();
}
and this is also legal:
if (condition) body();
But this is illegal:
if (condition)
body();
Spaces for Indentation
- Never use tabs.
- Use 4 space indents for blocks.
- Use 8 space indents for line wraps, including function calls and assignments.
For example, this is correct:
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
and this is not correct:
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
Field Naming Conventions
- Non-public, non-static field names start with m.
- Static field names start with s.
- Other fields start with a lower case letter.
- Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
For example:
public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sSingleton;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}
Treat Acronyms as Words
Treat acronyms and abbreviations as words in naming variables, methods, and classes. The names are much more readable:
Good | Bad |
---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
class Html |
class HTML |
String url |
String URL |
long id |
long ID |
Import Order
- Android imports
- Imports from third parties (com, junit, net, org)
- java and javax
TODO Comments
Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.
TODOs should include the string TODO in all caps, followed by a colon:
// TODO: Remove this code after UrlTable2 has been finished.