135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypage.title=Code Style Guidelines for Contributors
235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly@jd:body
335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<!--
5768b82a9dfbdd8504eae2736d283a60a37c7a547Clay Murphy    Copyright 2013 The Android Open Source Project
635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    Licensed under the Apache License, Version 2.0 (the "License");
835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    you may not use this file except in compliance with the License.
935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    You may obtain a copy of the License at
1035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
1135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        http://www.apache.org/licenses/LICENSE-2.0
1235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
1335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    Unless required by applicable law or agreed to in writing, software
1435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    distributed under the License is distributed on an "AS IS" BASIS,
1535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    See the License for the specific language governing permissions and
1735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    limitations under the License.
1835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly-->
1935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<div id="qv-wrapper">
2035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  <div id="qv">
2135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    <h2>In this document</h2>
2235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    <ol id="auto-toc">
2335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    </ol>
2435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  </div>
2535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</div>
2635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
2735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The rules below are not guidelines or recommendations, but strict rules.
2835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyContributions to Android generally <em>will not be accepted</em> if they do not
2935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyadhere to these rules.</p>
3035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
3135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Not all existing code follows these rules, but all new code is expected to.</p>
3235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
3335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="java-language-rules">Java Language Rules</h2>
3435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>We follow standard Java coding conventions. We add a few rules:</p>
3535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="dont-ignore-exceptions">Don't Ignore Exceptions</h3>
3635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Sometimes it is tempting to write code that completely ignores an exception
3735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylike this:</p>
3835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>void setServerPort(String value) {
3935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
4035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = Integer.parseInt(value);
4135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch (NumberFormatException e) { }
4235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
4335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
4435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>You must never do this. While you may think that your code will never
4535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyencounter this error condition or that it is not important to handle it,
4635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyignoring exceptions like above creates mines in your code for someone else to
4735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lytrip over some day. You must handle every Exception in your code in some
4835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyprincipled way. The specific handling varies depending on the case.</p>
4935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><em>Anytime somebody has an empty catch clause they should have a
5035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycreepy feeling. There are definitely times when it is actually the correct
5135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lything to do, but at least you have to think about it. In Java you can't escape
5235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe creepy feeling.</em> -<a href="http://www.artima.com/intv/solid4.html">James Gosling</a></p>
5335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Acceptable alternatives (in order of preference) are:</p>
5435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
5535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
5635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Throw the exception up to the caller of your method.</p>
5735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>void setServerPort(String value) throws NumberFormatException {
5835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    serverPort = Integer.parseInt(value);
5935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
6035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
6135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
6235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
6335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Throw a new exception that's appropriate to your level of abstraction.</p>
6435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>void setServerPort(String value) throws ConfigurationException {
6535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
6635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = Integer.parseInt(value);
6735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch (NumberFormatException e) {
6835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        throw new ConfigurationException("Port " + value + " is not valid.");
6935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
7035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
7135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
7235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
7335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
7435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Handle the error gracefully and substitute an appropriate value in the
7535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycatch {} block.</p>
7635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/** Set port. If value is not a valid number, 80 is substituted. */
7735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
7835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvoid setServerPort(String value) {
7935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
8035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = Integer.parseInt(value);
8135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch (NumberFormatException e) {
8235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = 80;  // default port for server 
8335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
8435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
8535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
8635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
8735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
8835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Catch the Exception and throw a new <code>RuntimeException</code>. This is dangerous:
8935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyonly do it if you are positive that if this error occurs, the appropriate
9035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lything to do is crash.</p>
9135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/** Set port. If value is not a valid number, die. */
9235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
9335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvoid setServerPort(String value) {
9435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
9535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = Integer.parseInt(value);
9635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch (NumberFormatException e) {
9735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        throw new RuntimeException("port " + value " is invalid, ", e);
9835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
9935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
10035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
10135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Note that the original exception is passed to the constructor for
10235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyRuntimeException.  If your code must compile under Java 1.3, you will need to
10335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyomit the exception that is the cause.</p>
10435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
10535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
10635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Last resort: if you are confident that actually ignoring the exception is
10735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyappropriate then you may ignore it, but you must also comment why with a good
10835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyreason:</p>
10935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/** If value is not a valid number, original port number is used. */
11035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvoid setServerPort(String value) {
11135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
11235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        serverPort = Integer.parseInt(value);
11335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch (NumberFormatException e) {
11435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        // Method is documented to just ignore invalid user input.
11535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        // serverPort will just be unchanged.
11635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
11735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
11835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
11935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
12035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
12135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="dont-catch-generic-exception">Don't Catch Generic Exception</h3>
12235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Sometimes it is tempting to be lazy when catching exceptions and do
12335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysomething like this:</p>
12435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>try {
12535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    someComplicatedIOFunction();        // may throw IOException 
12635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    someComplicatedParsingFunction();   // may throw ParsingException 
12735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    someComplicatedSecurityFunction();  // may throw SecurityException 
12835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    // phew, made it all the way 
12935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly} catch (Exception e) {                 // I'll just catch all exceptions 
13035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    handleError();                      // with one generic handler!
13135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
13235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
13335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>You should not do this. In almost all cases it is inappropriate to catch
13435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lygeneric Exception or Throwable, preferably not Throwable, because it includes
13535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyError exceptions as well. It is very dangerous. It means that Exceptions you
13635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lynever expected (including RuntimeExceptions like ClassCastException) end up
13735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lygetting caught in application-level error handling. It obscures the failure
13835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhandling properties of your code. It means if someone adds a new type of
13935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyException in the code you're calling, the compiler won't help you realize you
14035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyneed to handle that error differently. And in most cases you shouldn't be
14135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhandling different types of exception the same way, anyway.</p>
14235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>There are rare exceptions to this rule: certain test code and top-level
14335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycode where you want to catch all kinds of errors (to prevent them from showing
14435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyup in a UI, or to keep a batch job running). In that case you may catch
14535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lygeneric Exception (or Throwable) and handle the error appropriately. You
14635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyshould think very carefully before doing this, though, and put in comments
14735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyexplaining why it is safe in this place.</p>
14835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Alternatives to catching generic Exception:</p>
14935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
15035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
15135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Catch each exception separately as separate catch blocks after a single
15235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lytry. This can be awkward but is still preferable to catching all Exceptions.
15335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyBeware repeating too much code in the catch blocks.</li></p>
15435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
15535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
15635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Refactor your code to have more fine-grained error handling, with multiple
15735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lytry blocks. Split up the IO from the parsing, handle errors separately in each
15835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycase.</p>
15935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
16035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
16135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Rethrow the exception. Many times you don't need to catch the exception at
16235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythis level anyway, just let the method throw it.</p>
16335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
16435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
16535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Remember: exceptions are your friend! When the compiler complains you're
16635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lynot catching an exception, don't scowl. Smile: the compiler just made it
16735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyeasier for you to catch runtime problems in your code.</p>
16835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="dont-use-finalizers">Don't Use Finalizers</h3>
16935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Finalizers are a way to have a chunk of code executed
17035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywhen an object is garbage collected.</p>
17135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Pros: can be handy for doing cleanup, particularly of external resources.</p>
17235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Cons: there are no guarantees as to when a finalizer will be called,
17335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyor even that it will be called at all.</p>
17435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Decision: we don't use finalizers. In most cases, you can do what
17535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyyou need from a finalizer with good exception handling. If you absolutely need
17635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyit, define a close() method (or the like) and document exactly when that
17735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymethod needs to be called. See InputStream for an example. In this case it is
17835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyappropriate but not required to print a short log message from the finalizer,
17935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyas long as it is not expected to flood the logs.</p>
18035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="fully-qualify-imports">Fully Qualify Imports</h3>
18135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When you want to use class Bar from package foo,there
18235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyare two possible ways to import it:</p>
18335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ol>
18435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li><code>import foo.*;</code></li>
18535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ol>
18635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Pros: Potentially reduces the number of import statements.</p>
18735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ol>
18835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li><code>import foo.Bar;</code></li>
18935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ol>
19035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Pros: Makes it obvious what classes are actually used. Makes
19135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycode more readable for maintainers. </p>
19235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Decision: Use the latter for importing all Android code. An explicit
19335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyexception is made for java standard libraries (<code>java.util.*</code>, <code>java.io.*</code>, etc.)
19435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyand unit test code (<code>junit.framework.*</code>)</p>
19535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="java-library-rules">Java Library Rules</h2>
19635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>There are conventions for using Android's Java libraries and tools. In some
19735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycases, the convention has changed in important ways and older code might use a
19835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydeprecated pattern or library. When working with such code, it's okay to
199b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphycontinue the existing style. When creating new components never use deprecated
200b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphylibraries.</p>
201b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphy
20235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="java-style-rules">Java Style Rules</h2>
203b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphy
20435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="use-javadoc-standard-comments">Use Javadoc Standard Comments</h3>
20535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Every file should have a copyright statement at the top. Then a package
20635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lystatement and import statements should follow, each block separated by a blank
20735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyline. And then there is the class or interface declaration. In the Javadoc
20835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycomments, describe what the class or interface does.</p>
20935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/*
210768b82a9dfbdd8504eae2736d283a60a37c7a547Clay Murphy * Copyright (C) 2013 The Android Open Source Project 
21135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly *
21235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * Licensed under the Apache License, Version 2.0 (the "License");
21335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * you may not use this file except in compliance with the License.
21435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * You may obtain a copy of the License at 
21535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly *
21635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly *      http://www.apache.org/licenses/LICENSE-2.0
21735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly *
21835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * Unless required by applicable law or agreed to in writing, software 
21935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * distributed under the License is distributed on an "AS IS" BASIS,
22035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * See the License for the specific language governing permissions and 
22235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * limitations under the License.
22335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly */
22435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
22535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypackage com.android.internal.foo;
22635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
22735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimport android.os.Blah;
22835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimport android.view.Yada;
22935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
23035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimport java.sql.ResultSet;
23135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimport java.sql.SQLException;
23235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
23335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly/**
23435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * Does X and Y and provides an abstraction for Z.
23535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly */
23635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
23735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypublic class Foo {
23835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    ...
23935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
24035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
24135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Every class and nontrivial public method you write <em>must</em> contain a
24235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyJavadoc comment with at least one sentence describing what the class or method
24335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydoes. This sentence should start with a 3rd person descriptive verb.</p>
24435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Examples:</p>
24535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/** Returns the correctly rounded positive square root of a double value. */
24635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lystatic double sqrt(double a) {
24735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    ...
24835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
24935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
25035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>or</p>
25135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>/**
25235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * Constructs a new String by converting the specified array of 
25335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly * bytes using the platform's default character encoding.
25435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly */
25535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypublic String(byte[] bytes) {
25635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    ...
25735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
25835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
25935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>You do not need to write Javadoc for trivial get and set methods such as
26035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<code>setFoo()</code> if all your Javadoc would say is "sets Foo". If the method does
26135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysomething more complex (such as enforcing a constraint or having an important
26235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyside effect), then you must document it. And if it's not obvious what the
26335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyproperty "Foo" means, you should document it.</p>
26435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Every method you write, whether public or otherwise, would benefit from
26535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyJavadoc. Public methods are part of an API and therefore require Javadoc.</p>
26635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Android does not currently enforce a specific style for writing Javadoc
267b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphycomments, but you should follow the instructions <a
268b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphyhref="http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html">How
269b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphyto Write Doc Comments for the Javadoc Tool</a>.</p>
270b61f06cd20569ce1291181c8d997c354ecfc1ecfClay Murphy
27135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="write-short-methods">Write Short Methods</h3>
27235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>To the extent that it is feasible, methods should be kept small and
27335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyfocused. It is, however, recognized that long methods are sometimes
27435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyappropriate, so no hard limit is placed on method length. If a method exceeds
27535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly40 lines or so, think about whether it can be broken up without harming the
27635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lystructure of the program.</p>
27735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="define-fields-in-standard-places">Define Fields in Standard Places</h3>
27835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Fields should be defined either at the top of the file, or immediately before the methods that use them.</p>
27935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="limit-variable-scope">Limit Variable Scope</h3>
280291670d0f6dae2c19a0e38b8d3491bd87bac1a41Clay Murphy<p>The scope of local variables should be kept to a minimum. By doing so, you increase the readability and
28135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymaintainability of your code and reduce the likelihood of error. Each variable
28235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyshould be declared in the innermost block that encloses all uses of the
28335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvariable.</p>
28435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Local variables should be declared at the point they are first used. Nearly
28535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyevery local variable declaration should contain an initializer. If you don't
28635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyyet have enough information to initialize a variable sensibly, you should
28735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypostpone the declaration until you do.</p>
28835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>One exception to this rule concerns try-catch statements. If a variable is
28935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyinitialized with the return value of a method that throws a checked exception,
29035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyit must be initialized inside a try block. If the value must be used outside
29135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyof the try block, then it must be declared before the try block, where it
29235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycannot yet be sensibly initialized:</p>
29335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>// Instantiate class cl, which represents some sort of Set 
29435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LySet s = null;
29535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lytry {
29635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    s = (Set) cl.newInstance();
29735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly} catch(IllegalAccessException e) {
29835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    throw new IllegalArgumentException(cl + " not accessible");
29935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly} catch(InstantiationException e) {
30035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    throw new IllegalArgumentException(cl + " not instantiable");
30135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
30235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
30335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly// Exercise the set 
30435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lys.addAll(Arrays.asList(args));
30535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
30635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>But even this case can be avoided by encapsulating the try-catch block in a method:</p>
30735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>Set createSet(Class cl) {
30835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    // Instantiate class cl, which represents some sort of Set 
30935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    try {
31035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        return (Set) cl.newInstance();
31135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch(IllegalAccessException e) {
31235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        throw new IllegalArgumentException(cl + " not accessible");
31335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    } catch(InstantiationException e) {
31435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        throw new IllegalArgumentException(cl + " not instantiable");
31535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
31635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
31735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
31835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly...
31935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
32035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly// Exercise the set 
32135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LySet s = createSet(cl);
32235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lys.addAll(Arrays.asList(args));
32335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
32435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Loop variables should be declared in the for statement itself unless there
32535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyis a compelling reason to do otherwise:</p>
32607c3d6417043babe736c2e933a0640d895de7ddcClay Murphy<pre><code>for (int i = 0; i < n; i++) {
32735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    doSomething(i);
32835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
32935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
33035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>and</p>
33135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>for (Iterator i = c.iterator(); i.hasNext(); ) {
33235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    doSomethingElse(i.next());
33335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
33435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
33535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="order-import-statements">Order Import Statements</h3>
33635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The ordering of import statements is:</p>
33735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ol>
33835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
33935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Android imports</p>
34035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
34135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
34235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Imports from third parties (<code>com</code>, <code>junit</code>, <code>net</code>, <code>org</code>)</p>
34335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
34435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
34535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>java</code> and <code>javax</code></p>
34635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
34735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ol>
34835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>To exactly match the IDE settings, the imports should be:</p>
34935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
35035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
35135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Alphabetical within each grouping, with capital letters before lower case letters (e.g. Z before a).</p>
35235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
35335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
35435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>There should be a blank line between each major grouping (<code>android</code>, <code>com</code>, <code>junit</code>, <code>net</code>, <code>org</code>, <code>java</code>, <code>javax</code>).</p>
35535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
35635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
35735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Originally there was no style requirement on the ordering. This meant that
35835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe IDE's were either always changing the ordering, or IDE developers had to
35935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydisable the automatic import management features and maintain the imports by
36035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhand. This was deemed bad. When java-style was asked, the preferred styles
36135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywere all over the map. It pretty much came down to our needing to "pick an
36235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyordering and be consistent." So we chose a style, updated the style guide, and
36335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymade the IDEs obey it. We expect that as IDE users work on the code, the
36435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimports in all of the packages will end up matching this pattern without any
36535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyextra engineering effort.</p>
36635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>This style was chosen such that:</p>
36735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
36835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
36935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The imports people want to look at first tend to be at the top (<code>android</code>)</p>
37035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
37135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
37235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The imports people want to look at least tend to be at the bottom (<code>java</code>)</p>
37335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
37435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
37535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Humans can easily follow the style</p>
37635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
37735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
37835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>IDEs can follow the style</p>
37935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
38035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
38135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The use and location of static imports have been mildly controversial
38235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyissues. Some people would prefer static imports to be interspersed with the
38335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyremaining imports, some would prefer them reside above or below all other
384788d101d6ffaaf5891eed56603da4fe6c2f99021John Spurlockimports. Additionally, we have not yet come up with a way to make all IDEs use
38535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe same ordering.</p>
38635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Since most people consider this a low priority issue, just use your
38735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyjudgement and please be consistent.</p>
38835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="use-spaces-for-indentation">Use Spaces for Indentation</h3>
38935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>We use 4 space indents for blocks. We never use tabs. When in doubt, be
39035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyconsistent with code around you.</p>
39135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>We use 8 space indents for line wraps, including function calls and
39235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyassignments. For example, this is correct:</p>
39335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>Instrument i =
39435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        someLongExpression(that, wouldNotFit, on, one, line);
39535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
39635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>and this is not correct:</p>
39735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>Instrument i =
39835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    someLongExpression(that, wouldNotFit, on, one, line);
39935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
40035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="follow-field-naming-conventions">Follow Field Naming Conventions</h3>
40135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
40235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
40335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Non-public, non-static field names start with m.</p>
40435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
40535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
40635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Static field names start with s.</p>
40735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
40835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
40935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Other fields start with a lower case letter.</p>
41035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
41135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
41235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.</p>
41335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
41435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
41535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>For example:</p>
41635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>public class MyClass {
41735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    public static final int SOME_CONSTANT = 42;
41835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    public int publicField;
41935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    private static MyClass sSingleton;
42035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    int mPackagePrivate;
42135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    private int mPrivate;
42235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    protected int mProtected;
42335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
42435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
42535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="use-standard-brace-style">Use Standard Brace Style</h3>
42635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Braces do not go on their own line; they go on the same line as the code
42735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybefore them. So:</p>
42835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>class MyClass {
42935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    int func() {
43035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        if (something) {
43135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly            // ...
43235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        } else if (somethingElse) {
43335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly            // ...
43435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        } else {
43535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly            // ...
43635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        }
43735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    }
43835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
43935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
44035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>We require braces around the statements for a conditional. Except, if the
44135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyentire conditional (the condition and the body) fit on one line, you may (but
44235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyare not obligated to) put it all on one line. That is, this is legal:</p>
44335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>if (condition) {
44435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    body(); 
44535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
44635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
44735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>and this is legal:</p>
44835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>if (condition) body();
44935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
45035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>but this is still illegal:</p>
45135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>if (condition)
45235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    body();  // bad!
45335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
45435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="limit-line-length">Limit Line Length</h3>
45535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Each line of text in your code should be at most 100 characters long.</p>
45635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>There has been lots of discussion about this rule and the decision remains
45735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythat 100 characters is the maximum.</p>
45835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Exception: if a comment line contains an example command or a literal URL
45935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylonger than 100 characters, that line may be longer than 100 characters for
46035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyease of cut and paste.</p>
46135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Exception: import lines can go over the limit because humans rarely see
46235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythem. This also simplifies tool writing.</p>
46335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="use-standard-java-annotations">Use Standard Java Annotations</h3>
46435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Annotations should precede other modifiers for the same language element.
46535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LySimple marker annotations (e.g. @Override) can be listed on the same line with
46635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe language element. If there are multiple annotations, or parameterized
46735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyannotations, they should each be listed one-per-line in alphabetical
46835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyorder.&lt;</p>
46935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Android standard practices for the three predefined annotations in Java are:</p>
47035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
47135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
47235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>@Deprecated</code>: The @Deprecated annotation must be used whenever the use of the annotated
47335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyelement is discouraged. If you use the @Deprecated annotation, you must also
47435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhave a @deprecated Javadoc tag and it should name an alternate implementation.
47535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyIn addition, remember that a @Deprecated method is <em>still supposed to
47635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywork.</em></p>
47735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If you see old code that has a @deprecated Javadoc tag, please add the @Deprecated annotation.</p>
47835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
47935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
48035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>@Override</code>: The @Override annotation must be used whenever a method overrides the
48135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydeclaration or implementation from a super-class.</p>
48235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>For example, if you use the @inheritdocs Javadoc tag, and derive from a
48335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyclass (not an interface), you must also annotate that the method @Overrides
48435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe parent class's method.</p>
48535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
48635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
48735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>@SuppressWarnings</code>: The @SuppressWarnings annotation should only be used under circumstances
48835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywhere it is impossible to eliminate a warning. If a warning passes this
48935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly"impossible to eliminate" test, the @SuppressWarnings annotation <em>must</em> be
49035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyused, so as to ensure that all warnings reflect actual problems in the
49135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycode.</p>
49235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When a @SuppressWarnings annotation is necessary, it must be prefixed with
49335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lya TODO comment that explains the "impossible to eliminate" condition. This
49435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywill normally identify an offending class that has an awkward interface. For
49535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyexample:</p>
49635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>// TODO: The third-party class com.third.useful.Utility.rotate() needs generics 
49735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly&#64;SuppressWarnings("generic-cast")
49835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyList&lt;String&gt; blix = Utility.rotate(blax);
49935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
50035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When a @SuppressWarnings annotation is required, the code should be
50135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrefactored to isolate the software elements where the annotation applies.</p>
50235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
50335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
50435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="treat-acronyms-as-words">Treat Acronyms as Words</h3>
50535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Treat acronyms and abbreviations as words in naming variables, methods, and classes. The names are much more readable:</p>
50635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<table>
50735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<thead>
50835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
50935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<th>Good</th>
51035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<th>Bad</th>
51135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
51235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</thead>
51335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tbody>
51435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
51535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>XmlHttpRequest</td>
51635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>XMLHTTPRequest</td>
51735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
51835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
51935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>getCustomerId</td>
52035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>getCustomerID</td>
52135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
52235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
52335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>class Html</td>
52435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>class HTML</td>
52535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
52635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
52735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>String url</td>
52835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>String URL</td>
52935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
53035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<tr>
53135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>long id</td>
53235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<td>long ID</td>
53335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tr>
53435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</tbody>
53535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</table>
53635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Both the JDK and the Android code bases are very inconsistent with regards
53735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyto acronyms, therefore, it is virtually impossible to be consistent with the
53835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycode around you. Bite the bullet, and treat acronyms as words.</p>
539291670d0f6dae2c19a0e38b8d3491bd87bac1a41Clay Murphy
54035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="use-todo-comments">Use TODO Comments</h3>
54135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Use TODO comments for code that is temporary, a short-term solution, or
54235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lygood-enough but not perfect.</p>
54335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>TODOs should include the string TODO in all caps, followed by a colon:</p>
54435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>// TODO: Remove this code after the UrlTable2 has been checked in.
54535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
54635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>and</p>
54735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>// TODO: Change this to use a flag instead of a constant.
54835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</code></pre>
54935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If your TODO is of the form "At a future date do something" make sure that
55035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyyou either include a very specific date ("Fix by November 2005") or a very
55135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyspecific event ("Remove this code after all production mixers understand
55235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyprotocol V7.").</p>
55335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="log-sparingly">Log Sparingly</h3>
554291670d0f6dae2c19a0e38b8d3491bd87bac1a41Clay Murphy<p>While logging is necessary, it has a significantly negative impact on
55535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyperformance and quickly loses its usefulness if it's not kept reasonably
556291670d0f6dae2c19a0e38b8d3491bd87bac1a41Clay Murphyterse. The logging facilities provides five different levels of logging:</p>
55735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
55835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
55935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>ERROR</code>: 
56035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyThis level of logging should be used when something fatal has happened,
56135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyi.e. something that will have user-visible consequences and won't be
56235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrecoverable without explicitly deleting some data, uninstalling applications,
56335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywiping the data partitions or reflashing the entire phone (or worse). This
56435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylevel is always logged. Issues that justify some logging at the ERROR level
56535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyare typically good candidates to be reported to a statistics-gathering
56635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyserver.</p>
56735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
56835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
56935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>WARNING</code>: 
57035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyThis level of logging should used when something serious and unexpected
57135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhappened, i.e. something that will have user-visible consequences but is
57235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylikely to be recoverable without data loss by performing some explicit action,
57335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyranging from waiting or restarting an app all the way to re-downloading a new
57435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyversion of an application or rebooting the device. This level is always
57535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylogged. Issues that justify some logging at the WARNING level might also be
57635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyconsidered for reporting to a statistics-gathering server.</p>
57735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
57835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
57935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>INFORMATIVE:</code>
58035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyThis level of logging should used be to note that something interesting to
58135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymost people happened, i.e. when a situation is detected that is likely to have
58235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywidespread impact, though isn't necessarily an error. Such a condition should
58335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyonly be logged by a module that reasonably believes that it is the most
58435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyauthoritative in that domain (to avoid duplicate logging by non-authoritative
58535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycomponents). This level is always logged.</p>
58635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
58735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
58835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>DEBUG</code>:
58935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyThis level of logging should be used to further note what is happening on the
59035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydevice that could be relevant to investigate and debug unexpected behaviors.
59135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyYou should log only what is needed to gather enough information about what is
59235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lygoing on about your component. If your debug logs are dominating the log then
59335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyyou probably should be using verbose logging. </p>
59435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>This level will be logged, even
59535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyon release builds, and is required to be surrounded by an <code>if (LOCAL_LOG)</code> or <code>if
59635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly(LOCAL_LOGD)</code> block, where <code>LOCAL_LOG[D]</code> is defined in your class or
59735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysubcomponent, so that there can exist a possibility to disable all such
59835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylogging. There must therefore be no active logic in an <code>if (LOCAL_LOG)</code> block.
59935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyAll the string building for the log also needs to be placed inside the <code>if
60035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly(LOCAL_LOG)</code> block. The logging call should not be re-factored out into a
60135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymethod call if it is going to cause the string building to take place outside
60235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyof the <code>if (LOCAL_LOG)</code> block. </p>
60335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>There is some code that still says <code>if
60435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly(localLOGV)</code>. This is considered acceptable as well, although the name is
60535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lynonstandard.</p>
60635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
60735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
60835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>VERBOSE</code>:
60935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyThis level of logging should be used for everything else. This level will only
61035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybe logged on debug builds and should be surrounded by an <code>if (LOCAL_LOGV)</code> block
61135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly(or equivalent) so that it can be compiled out by default. Any string building
61235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywill be stripped out of release builds and needs to appear inside the <code>if (LOCAL_LOGV)</code> block.</p>
61335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
61435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
61535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><em>Notes:</em> </p>
61635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
61735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
61835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Within a given module, other than at the VERBOSE level, an
61935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyerror should only be reported once if possible: within a single chain of
62035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyfunction calls within a module, only the innermost function should return the
62135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyerror, and callers in the same module should only add some logging if that
62235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysignificantly helps to isolate the issue.</p>
62335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
62435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
62535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>In a chain of modules, other than at the VERBOSE level, when a
62635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylower-level module detects invalid data coming from a higher-level module, the
62735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylower-level module should only log this situation to the DEBUG log, and only
62835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyif logging provides information that is not otherwise available to the caller.
62935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LySpecifically, there is no need to log situations where an exception is thrown
63035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly(the exception should contain all the relevant information), or where the only
63135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyinformation being logged is contained in an error code. This is especially
63235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimportant in the interaction between the framework and applications, and
63335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyconditions caused by third-party applications that are properly handled by the
63435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyframework should not trigger logging higher than the DEBUG level. The only
63535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysituations that should trigger logging at the INFORMATIVE level or higher is
63635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywhen a module or application detects an error at its own level or coming from
63735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lya lower level.</p>
63835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
63935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
64035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When a condition that would normally justify some logging is
64135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylikely to occur many times, it can be a good idea to implement some
64235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrate-limiting mechanism to prevent overflowing the logs with many duplicate
64335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycopies of the same (or very similar) information.</p>
64435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
64535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
64635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Losses of network connectivity are considered common and fully
64735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyexpected and should not be logged gratuitously. A loss of network connectivity
64835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythat has consequences within an app should be logged at the DEBUG or VERBOSE
64935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylevel (depending on whether the consequences are serious enough and unexpected
65035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyenough to be logged in a release build).</p>
65135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
65235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
65335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>A full filesystem on a filesystem that is acceessible to or on
65435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybehalf of third-party applications should not be logged at a level higher than
65535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyINFORMATIVE.</p>
65635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
65735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
65835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Invalid data coming from any untrusted source (including any
65935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyfile on shared storage, or data coming through just about any network
66035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyconnections) is considered expected and should not trigger any logging at a
66135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylevel higher then DEBUG when it's detected to be invalid (and even then
66235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylogging should be as limited as possible).</p>
66335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
66435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
66535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Keep in mind that the <code>+</code> operator, when used on Strings,
66635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimplicitly creates a <code>StringBuilder</code> with the default buffer size (16
66735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycharacters) and potentially quite a few other temporary String objects, i.e.
66835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythat explicitly creating StringBuilders isn't more expensive than relying on
66935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe default '+' operator (and can be a lot more efficient in fact). Also keep
67035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyin mind that code that calls <code>Log.v()</code> is compiled and executed on release
67135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybuilds, including building the strings, even if the logs aren't being
67235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyread.</p>
67335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
67435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
67535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Any logging that is meant to be read by other people and to be
67635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyavailable in release builds should be terse without being cryptic, and should
67735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybe reasonably understandable. This includes all logging up to the DEBUG
67835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylevel.</p>
67935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
68035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
68135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When possible, logging should be kept on a single line if it
68235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymakes sense. Line lengths up to 80 or 100 characters are perfectly acceptable,
68335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lywhile lengths longer than about 130 or 160 characters (including the length of
68435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe tag) should be avoided if possible.</p>
68535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
68635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
68735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Logging that reports successes should never be used at levels
68835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhigher than VERBOSE.</p>
68935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
69035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
69135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Temporary logging that is used to diagnose an issue that's
69235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyhard to reproduce should be kept at the DEBUG or VERBOSE level, and should be
69335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyenclosed by if blocks that allow to disable it entirely at compile-time.</p>
69435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
69535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
69635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Be careful about security leaks through the log. Private
69735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyinformation should be avoided. Information about protected content must
69835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydefinitely be avoided. This is especially important when writing framework
69935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycode as it's not easy to know in advance what will and will not be private
70035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyinformation or protected content.</p>
70135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
70235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
70335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><code>System.out.println()</code> (or <code>printf()</code> for native code) should
70435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lynever be used. System.out and System.err get redirected to /dev/null, so your
70535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyprint statements will have no visible effects. However, all the string
70635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybuilding that happens for these calls still gets executed.</p>
70735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
70835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>
70935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><em>The golden rule of logging is that your logs may not
71035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyunnecessarily push other logs out of the buffer, just as others may not push
71135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyout yours.</em></p>
71235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</li>
71335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
71435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="be-consistent">Be Consistent</h3>
71535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Our parting thought: BE CONSISTENT. If you're editing code, take a few
71635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyminutes to look at the code around you and determine its style. If they use
71735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyspaces around their if clauses, you should too. If their comments have little
71835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyboxes of stars around them, make your comments have little boxes of stars
71935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyaround them too.</p>
72035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The point of having style guidelines is to have a common vocabulary of
72135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycoding, so people can concentrate on what you're saying, rather than on how
72235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyyou're saying it. We present global style rules here so people know the
72335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvocabulary. But local style is also important. If code you add to a a file
72435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lylooks drastically different from the existing code around it, it throws
72535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyreaders out of their rhythm when they go to read it. Try to avoid this.</p></p>
72635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="javatests-style-rules">Javatests Style Rules</h2>
72735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h3 id="follow-test-method-naming-conventions">Follow Test Method Naming Conventions</h3>
72835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When naming test methods, you can use an underscore to seperate what is
72935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybeing tested from the specific case being tested. This style makes it easier
73035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyto see exactly what cases are being tested.</p>
73135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>For example:</p>
73235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre><code>testMethod_specificCase1 testMethod_specificCase2
73335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
73435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvoid testIsDistinguishable_protanopia() {
73535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    ColorMatcher colorMatcher = new ColorMatcher(PROTANOPIA)
73635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    assertFalse(colorMatcher.isDistinguishable(Color.RED, Color.BLACK))
73735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    assertTrue(colorMatcher.isDistinguishable(Color.X, Color.Y))
73835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly}
73907c3d6417043babe736c2e933a0640d895de7ddcClay Murphy</code></pre>
740