Skip to content

Latest commit

 

History

History
81 lines (45 loc) · 2.61 KB

java.md

File metadata and controls

81 lines (45 loc) · 2.61 KB

Java

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/.

Imports

Import statements are divided into the following groups, in this order, with each group separated by a blank line:

  1. All static imports in a single group
  2. org.khanacademy imports
  3. All other non-Android third-party imports, in ASCII sort order (for example: com, junit, org, sun)
  4. android imports
  5. java imports
  6. 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)

No per-file boilerplate

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.

Use your best judgement for Javadoc

We don't strictly require Javadoc for all public methods, though it's strongly encouraged for non-trivial ones.

Use @Override where possible

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.

No single-line if statements.

Bad:

if (foo) {bar();}   // No single-line if statements

// ...and braces are always required
if (foo)
    bar();

Good:

if (foo) {
    bar();
}

No C-style array declarations

The square brackets form part of the type, not the variable.

Good: String[] args Bad: String args[]

Use TODO comments with author name

Extending AOSP style guide, TODOs should include the string TODO in all caps, the author's name in parentheses, and a colon:

// TODO(author): ...

Use final variables when possible

Mark variables that should only be initialized once as final. This includes function arguments.