Android Coding Styles

Handling Exception

  1. Handle specific exception rather than generic exception.

  2. You can choose to throw the exception up to the caller of your method

For example :

void setServerPort(String value) throws NumberFormatException {      
    serverPort = Integer.parse(value);      
}
  1. Handle the error gracefully and substitute an appropriate value in the catch {} block.

  2. Don’t throw RuntimeException. -> cause application to crash

  3. Catch each exception separately as separate catch blocks after a single try.

Fully Qualify Imports

Do this :

import foo.Bar;

instead of :

import foo.*;

Java Style Rules

Use Javadoc Standard Comments

/**
 *  For example
 */
  1. Seperate each block by a blank line.

  2. Describe what the class or interface does.

Quoting from official website

Every class and nontrivial public method you write must contain a Javadoc comment with at least one sentence describing what the class or method does. This sentence should start with a 3rd person descriptive verb.

  1. Don’t need to write Javadoc for trivial get and set method..

Use Spaces for Indentation

We use 4 space indents for blocks. We never use tabs. When in doubt, be consistent with code around you.

We use 8 space indents for line wraps, including function calls and assignments. For example, this is correct:

Limit Variable Scope

  1. The scope of local variables should be kept to a minimum.
  2. Each variables should be declared in the innermost block that enclosed all uses of the variable.
  3. Local variables should be declared at the point they are first used.

Take a look at the below source code

Set createSet(Class cl) {
    // Instantiate class cl, which represents some sort of Set 
    try {
        return (Set) cl.newInstance();
    } catch(IllegalAccessException e) {
        throw new IllegalArgumentException(cl + " not accessible");
    } catch(InstantiationException e) {
        throw new IllegalArgumentException(cl + " not instantiable");
    }
}

...

// Exercise the set 
Set s = createSet(cl);
s.addAll(Arrays.asList(args));

With this techniques, we don’t have to declare a variable and initialize it before try block.

  1. Loop variables should be declared in the for statement.

Following Field Name 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.

Example:

public class MyClass {
    public static final int SOME_CONSTANT = 10;
    public int publicField;
    private static MyClass sSintleton;
    int mPackagePrivate;
    private int mPrivate;
    protected int mProtected;
}

Limit Line Length

  1. Each line of text in your code should be at most 100 characters long.

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.

and

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

If your TODO is of the form “At a future date do something” make sure that you either include a very specific date (“Fix by November 2005”) or a very specific event (“Remove this code after all production mixers understand protocol V7.”).

Reference

[Coding Style Guidelines for Contributors] [https://source.android.com/source/code-style.html]