Coding Styles in Android Development

The code styles below are rules, not guidelines or recommendations. Contributions to Android should follow the below.

Don’t ignore exceptions

It can be tempting to write code that completely ignores an exception, such as:

Do not do this. While you may think your code will never encounter this error condition or that it is not important to handle it, it might trigger some day and then you wouldn’t know.

Write Short Methods

If a method exceeds 40 lines or so, think about whether it can be broken up without harming the structure of the program.

Define Fields in Standard Places

Define fields either at the top of the file or immediately before the methods that use them.

Limit Variable Scope

Each variable should be declared in the innermost block that encloses all uses of the variable.

Follow 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:

Use 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 the UrlTable2 has been checked in.


// TODO: Change this to use a flag instead of a constant.

Log Sparingly

While logging is necessary, it has a significantly negative impact on performance and quickly loses its usefulness if not kept reasonably terse.

Thats all for this post. Stay tuned for Part 2! :)