-
Notifications
You must be signed in to change notification settings - Fork 22
Code Style
alexwalterbos edited this page Nov 25, 2014
·
1 revision
-
Class (static) variables: First the public class variables, then the protected, and then the private.
-
Instance variables: First public, then protected, and then private.
-
Constructors
-
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.
- Descriptive naming; type and name should make clear what the object contains.
- No verbs.
- Try to use private scope combined with getters and setters.
- 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. - Use an 'm' prefix for object members, and an 's' prefix for static fields. Local scope variables and method arguments have no prefix.
- Camel case with lower case start.
- Use verbs.
- Should make clear what the method does, at least roughly. This includes side-effects (which should be avoided in general).
- If
3
cannot be abided, add javadoc
- Opening brackets at the end of the line, not on a new line
- Closing brackets on a new line
- 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;
}
}