Code Style Guidelines of Android Development

The rules below are not guidelines or recommendations, but strict rules. Contributions to Android generally will not be accepted if they do not adhere to these rules.

Not all existing code follows these rules, but all new code is expected to.

Java Language Rules

We follow standard Java coding conventions. We add a few rules:

  1. Exceptions: Never catch and ignore them without explanation.
  2. Exceptions: do not catch generic Exception, except in library code at the root of the stack.
  3. Finalizers: generally don’t use them.
  4. Imports: Fully qualify imports

Java Library Rules

There are conventions for using Android’s Java libraries and tools. In some cases, the convention has changed in important ways and older code might use a deprecated pattern or library. When working with such code, it’s okay to continue the existing style (see Consistency). When creating new components never use deprecated libraries.

Java Style Rules

Programs are much easier to maintain when all files have a consistent style. We follow the standard Java coding style, as defined by Sun in their Code Conventions for the Java Programming Language, with a few exceptions and additions. This style guide is comprehensive and detailed and is in common usage in the Java community.

In addition, we enforce the following style rules:

  1. Comments/Javadoc: write it; use standard style
  2. Short methods: don’t write giant methods
  3. Fields: should either be at the top of the file, or immediately before the methods that use them
  4. Local variables: limit the scope
  5. Imports: android; third party alphabetical; java(x)
  6. Indentation: 4 spaces, no tabs.
  7. Line length: 100 columns
  8. Field names: Non-public, non-static fields start with m.
  9. Braces: Opening braces don’t go on their own line.
  10. Annotations: Use the standard annotations.
  11. Acronyms are words: Treat acronyms as words in names, yielding XmlHttpRequest, getUrl(), etc.
  12. TODO style: “TODO: write this description”
  13. Consistency: Look at what’s around you!
  14. Logging: Be careful with logging. It’s expensive.

Javatests Style Rules

  1. Naming test methods: testMethod_specificCase is ok

References

zemna

Programmer/Web/Mobile/Desktop

You may also like...

Leave a Reply