At Khan Academy, we only use Java for Android and, as such, follow the Android Code Style Guidelines for Contributors. Note that those rules themselves extend the standard Java language style guide as of 1999. The rest of this document describes additions and clarifications to those rules that we follow at Khan Academy.
To import these settings for Android Studio, copy the config file into $HOME/Library/Preferences/AndroidStudio/codestyles/
.
Import statements are divided into the following groups, in this order, with each group separated by a blank line:
- All static imports in a single group
- org.khanacademy imports
- All other non-Android third-party imports, in ASCII sort order (for example: com, junit, org, sun)
- android imports
- java imports
- javax imports
Imports are not subject to the 100 column limit rule; they should never be wrapped.
We prefer using static imports when using the following:
com.google.common.base.Preconditions.*
(e.g.checkNotNull
)org.junit.Assert.*
(e.g.assertEquals
)
We do not include copyright or @author
or Created by
boilerplate at the top of files. The first line of the file is typically the package
statement.
Package statements are not subject to the 100 column limit rule; they should never be wrapped.
We don't strictly require Javadoc for all public methods, though it's strongly encouraged for non-trivial ones.
In addition to requiring the @Override
tag when a class overrides the declration or implementation from a superclass, we also require it to be used when classes implement a method from an implemented interface.
Bad:
if (foo) {bar();} // No single-line if statements
// ...and braces are always required
if (foo)
bar();
Good:
if (foo) {
bar();
}
The square brackets form part of the type, not the variable.
Good: String[] args
Bad: String args[]
Extending AOSP style guide, TODOs should include the string TODO in all caps, the author's name in parentheses, and a colon:
// TODO(author): ...
Mark variables that should only be initialized once as final
. This includes function arguments.