-
Notifications
You must be signed in to change notification settings - Fork 1
Coding Standards
raleesound edited this page Jan 27, 2015
·
1 revision
#Coding Standards#
##General:##
- 4 space tabs (Android Studio should do this by default -- you can still just press 'Tab' but your IDE should insert 4 '\s' characters rather than a single '\t' character.)
- Readability > everything else (No hacker crap!)
- Next line braces
- White space between all tokens
- Specify operator precedence with parentheses
- Defensive programming -- use assertions
- Descriptive names -- someone else should be able to figure out immediately what something is/does by reading its name
- Comments -- Javadoc comments before each class/method, and then comments inside for anything which would not be immediately obvious to someone else
- Capitalization:
- camelCase for variables/methods
- TitleCase for class names
- ALL_CAPS for constants
##Examples:##
###Next Line Braces:###
if(something)
{
//multiple lines of code
}
not:
if(something) {
//multiple lines of code
}
###White space between all tokens:###
a = ( b + c ) * d;
not:
a=(b+c)*d;
###Operator Precedence:###
if (a > c || c < d && d != a) //WTF is this
if (((a > c) || (c < d)) && (d != a)) //Much easier to see what's happening
While operator precedence will make these two bits of code functionally identical, the parentheses in the second one make it much easier for someone else to read, especially with context highlighting in an IDE. That being said, we probably ought to try to avoid cramming a whole bunch of different boolean logic into a single conditional (combining 'or' and 'and' logic is usually a good sign that something can be simplified), but there are cases where it is the best/most readable way to do things.