Skip to content
alexwalterbos edited this page Nov 25, 2014 · 1 revision

Class ordering

  1. Class (static) variables: First the public class variables, then the protected, and then the private.

  2. Instance variables: First public, then protected, and then private.

  3. Constructors

  4. Methods: These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

Naming variables and fields

  1. Descriptive naming; type and name should make clear what the object contains.
  2. No verbs.
  3. Try to use private scope combined with getters and setters.
  4. If it's static final, use capital case and underscore-separated words. If it's not, use camel case starting with a lower case letter.
  5. Use an 'm' prefix for object members, and an 's' prefix for static fields. Local scope variables and method arguments have no prefix.

Naming methods

  1. Camel case with lower case start.
  2. Use verbs.
  3. Should make clear what the method does, at least roughly. This includes side-effects (which should be avoided in general).
  4. If 3 cannot be abided, add javadoc

Nesting and brackets

  1. Opening brackets at the end of the line, not on a new line
  2. Closing brackets on a new line
  3. Nesting always in brackets, even if it's only one line Example:
class Example{
    private static final String APPLICATION_TITLE;

    private static int sSomeInteger; // no inspiration

    private boolean mActive;
    private ArrayList<ContentItem> mSelectedItems; // example, this shouldn't be stored in a field

    public Example(boolean active) {
        this.mActive = active;
    }

    public ArrayList<ContentItem> getSelectedItems() {
        if(this.mSelectedItems != null){
            return this.mSelectedItems;
        }
    }