1 <html devsite><head> 2 <title></title> 3 <meta name="project_path" value="/_project.yaml"/> 4 <meta name="book_path" value="/_book.yaml"/> 5 </head> 6 <body> 7 <!-- 8 Copyright 2017 The Android Open Source Project 9 10 Licensed under the Apache License, Version 2.0 (the "License"); 11 you may not use this file except in compliance with the License. 12 You may obtain a copy of the License at 13 14 http://www.apache.org/licenses/LICENSE-2.0 15 16 Unless required by applicable law or agreed to in writing, software 17 distributed under the License is distributed on an "AS IS" BASIS, 18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 See the License for the specific language governing permissions and 20 limitations under the License. 21 --> 22 23 <p> Android <em></em></p> 24 25 <h2 id="java-language-rules">Java </h2> 26 <p>Android Java </p> 27 28 <h3 id="dont-ignore-exceptions"></h3> 29 <p></p> 30 <pre><code>void setServerPort(String value) { 31 try { 32 serverPort = Integer.parseInt(value); 33 } catch (NumberFormatException e) { } 34 } 35 </code></pre> 36 <p></p> 37 <p><em> catch catch Java </em>-<a href="http://www.artima.com/intv/solid4.html">James Gosling</a></p> 38 <p></p> 39 <ul> 40 <li> 41 <pre><code>void setServerPort(String value) throws NumberFormatException { 42 serverPort = Integer.parseInt(value); 43 } 44 </code></pre> 45 </li> 46 <li> 47 <pre><code>void setServerPort(String value) throws ConfigurationException { 48 try { 49 serverPort = Integer.parseInt(value); 50 } catch (NumberFormatException e) { 51 throw new ConfigurationException("Port " + value + " is not valid."); 52 } 53 } 54 </code></pre> 55 </li> 56 <li> catch {} 57 <pre><code>/** Set port. If value is not a valid number, 80 is substituted. */ 58 59 void setServerPort(String value) { 60 try { 61 serverPort = Integer.parseInt(value); 62 } catch (NumberFormatException e) { 63 serverPort = 80; // default port for server 64 } 65 } 66 </code></pre> 67 </li> 68 <li> <code>RuntimeException</code> 69 <pre><code>/** Set port. If value is not a valid number, die. */ 70 71 void setServerPort(String value) { 72 try { 73 serverPort = Integer.parseInt(value); 74 } catch (NumberFormatException e) { 75 throw new RuntimeException("port " + value " is invalid, ", e); 76 } 77 } 78 </code></pre> 79 <p class="note"><strong></strong> RuntimeException Java 1.3 </p> 80 </li> 81 <li><pre><code>/** If value is not a valid number, original port number is used. */ 82 void setServerPort(String value) { 83 try { 84 serverPort = Integer.parseInt(value); 85 } catch (NumberFormatException e) { 86 // Method is documented to just ignore invalid user input. 87 // serverPort will just be unchanged. 88 } 89 } 90 </code></pre> 91 </li> 92 </ul> 93 94 <h3 id="dont-catch-generic-exception"></h3> 95 <p></p> 96 <pre><code>try { 97 someComplicatedIOFunction(); // may throw IOException 98 someComplicatedParsingFunction(); // may throw ParsingException 99 someComplicatedSecurityFunction(); // may throw SecurityException 100 // phew, made it all the way 101 } catch (Exception e) { // I'll just catch all exceptions 102 handleError(); // with one generic handler! 103 } 104 </code></pre> 105 <p> Throwable Throwable Error ClassCastException RuntimeException</p> 106 <p> Throwable</p> 107 <p></p> 108 <ul> 109 <li> 110 <p> try catch catch </p></li><p></p> 111 112 <li> 113 <p> try IO</p> 114 </li> 115 <li> 116 <p></p> 117 </li> 118 </ul> 119 <p></p> 120 <h3 id="dont-use-finalizers"></h3> 121 <p></p> 122 <p>Android close() InputStream</p> 123 124 <h3 id="fully-qualify-imports"></h3> 125 <p> foo Bar </p> 126 <ul> 127 <li><code>import foo.*;</code> 128 <p> import </p></li> 129 <li><code>import foo.Bar;</code> 130 <p></p></li></ul> 131 <p> <code>import foo.Bar;</code> Android Java <code>java.util.*</code><code>java.io.*</code> (<code>junit.framework.*</code>) </p> 132 133 <h2 id="java-library-rules">Java </h2> 134 <p> Android Java </p> 135 136 <h2 id="java-style-rules">Java </h2> 137 138 <h3 id="use-javadoc-standard-comments"> Javadoc </h3> 139 <p> package import Javadoc </p> 140 <pre><code>/* 141 * Copyright (C) 2015 The Android Open Source Project 142 * 143 * Licensed under the Apache License, Version 2.0 (the "License"); 144 * you may not use this file except in compliance with the License. 145 * You may obtain a copy of the License at 146 * 147 * http://www.apache.org/licenses/LICENSE-2.0 148 * 149 * Unless required by applicable law or agreed to in writing, software 150 * distributed under the License is distributed on an "AS IS" BASIS, 151 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 152 * See the License for the specific language governing permissions and 153 * limitations under the License. 154 */ 155 156 package com.android.internal.foo; 157 158 import android.os.Blah; 159 import android.view.Yada; 160 161 import java.sql.ResultSet; 162 import java.sql.SQLException; 163 164 /** 165 * Does X and Y and provides an abstraction for Z. 166 */ 167 168 public class Foo { 169 ... 170 } 171 </code></pre> 172 <p><em></em> Javadoc </p> 173 <p></p> 174 <pre><code>/** Returns the correctly rounded positive square root of a double value. */ 175 static double sqrt(double a) { 176 ... 177 } 178 </code></pre> 179 <p></p> 180 <pre><code>/** 181 * Constructs a new String by converting the specified array of 182 * bytes using the platform's default character encoding. 183 */ 184 public String(byte[] bytes) { 185 ... 186 } 187 </code></pre> 188 <p> get set <code>setFoo()</code> Javadoc FooFoo 189 </p><p> Javadoc API JavadocAndroid Javadoc <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html"> Javadoc </a></p> 190 191 <h3 id="write-short-methods"></h3> 192 <p> 40 </p> 193 194 <h3 id="define-fields-in-standard-places"></h3> 195 <p></p> 196 197 <h3 id="limit-variable-scope"></h3> 198 <p></p> 199 <p></p> 200 <p>try-catch try try try try </p> 201 <pre><code>// Instantiate class cl, which represents some sort of Set 202 Set s = null; 203 try { 204 s = (Set) cl.newInstance(); 205 } catch(IllegalAccessException e) { 206 throw new IllegalArgumentException(cl + " not accessible"); 207 } catch(InstantiationException e) { 208 throw new IllegalArgumentException(cl + " not instantiable"); 209 } 210 211 // Exercise the set 212 s.addAll(Arrays.asList(args)); 213 </code></pre> 214 <p> try-catch </p> 215 <pre><code>Set createSet(Class cl) { 216 // Instantiate class cl, which represents some sort of Set 217 try { 218 return (Set) cl.newInstance(); 219 } catch(IllegalAccessException e) { 220 throw new IllegalArgumentException(cl + " not accessible"); 221 } catch(InstantiationException e) { 222 throw new IllegalArgumentException(cl + " not instantiable"); 223 } 224 } 225 226 ... 227 228 // Exercise the set 229 Set s = createSet(cl); 230 s.addAll(Arrays.asList(args)); 231 </code></pre> 232 <p> for </p> 233 <pre><code>for (int i = 0; i < n; i++) { 234 doSomething(i); 235 } 236 </code></pre> 237 <p></p> 238 <pre><code>for (Iterator i = c.iterator(); i.hasNext(); ) { 239 doSomethingElse(i.next()); 240 } 241 </code></pre> 242 243 <h3 id="order-import-statements"> import </h3> 244 <p>import </p> 245 <ol> 246 <li> 247 <p> Android </p> 248 </li> 249 <li> 250 <p><code>com</code><code>junit</code><code>net</code><code>org</code></p> 251 </li> 252 <li> 253 <p><code>java</code> <code>javax</code></p> 254 </li> 255 </ol> 256 <p> IDE </p> 257 <ul> 258 <li> 259 <p> Z a </p> 260 </li> 261 <li> 262 <p><code>android</code><code>com</code><code>junit</code><code>net</code><code>org</code><code>java</code><code>javax</code></p> 263 </li> 264 </ul> 265 <p> IDE IDE Java Android IDE IDE </p> 266 <p></p> 267 <ul> 268 <li> 269 <p> (<code>android</code>)</p> 270 </li> 271 <li> 272 <p> (<code>java</code>)</p> 273 </li> 274 <li> 275 <p></p> 276 </li> 277 <li> 278 <p>IDE </p> 279 </li> 280 </ul> 281 <p> IDE </p> 282 283 <h3 id="use-spaces-for-indentation"></h3> 284 <p> (4) </p> 285 <p> (8) </p> 286 <pre><code>Instrument i = 287 someLongExpression(that, wouldNotFit, on, one, line); 288 </code></pre> 289 <p></p> 290 <pre><code>Instrument i = 291 someLongExpression(that, wouldNotFit, on, one, line); 292 </code></pre> 293 294 <h3 id="follow-field-naming-conventions"></h3> 295 <ul> 296 <li> 297 <p> m </p> 298 </li> 299 <li> 300 <p> s </p> 301 </li> 302 <li> 303 <p></p> 304 </li> 305 <li> 306 <p> final (ALL_CAPS_WITH_UNDERSCORES)</p> 307 </li> 308 </ul> 309 <p></p> 310 <pre><code>public class MyClass { 311 public static final int SOME_CONSTANT = 42; 312 public int publicField; 313 private static MyClass sSingleton; 314 int mPackagePrivate; 315 private int mPrivate; 316 protected int mProtected; 317 } 318 </code></pre> 319 <h3 id="use-standard-brace-style"></h3> 320 <p></p> 321 <pre><code>class MyClass { 322 int func() { 323 if (something) { 324 // ... 325 } else if (somethingElse) { 326 // ... 327 } else { 328 // ... 329 } 330 } 331 } 332 </code></pre> 333 <p></p> 334 <pre><code>if (condition) { 335 body(); 336 } 337 </code></pre> 338 <p></p> 339 <pre><code>if (condition) body(); 340 </code></pre> 341 <p></p> 342 <pre><code>if (condition) 343 body(); // bad! 344 </code></pre> 345 346 <h3 id="limit-line-length"></h3> 347 <p> 100 <em></em> 100 </p> 348 <ul> 349 <li> 100 100 </li> 350 <li></li> 351 </ul> 352 353 <h3 id="use-standard-java-annotations"> Java </h3> 354 <p> @Override</p> 355 <p>Java 3 Android </p> 356 <ul> 357 <li><code>@Deprecated</code> @Deprecated @Deprecated @deprecated Javadoc @Deprecated <em></em> @deprecated Javadoc @Deprecated 358 </li> 359 <li><code>@Override</code> @Override @inheritdocs Javadoc @Override </li> 360 <li><code>@SuppressWarnings</code>@SuppressWarnings <em></em> @SuppressWarnings 361 <p> @SuppressWarnings TODO </p> 362 <pre><code>// TODO: The third-party class com.third.useful.Utility.rotate() needs generics 363 @SuppressWarnings("generic-cast") 364 List<String> blix = Utility.rotate(blax); 365 </code></pre> 366 <p> @SuppressWarnings </p> 367 </li> 368 </ul> 369 370 <h3 id="treat-acronyms-as-words"></h3> 371 <p></p> 372 <table> 373 <thead> 374 <tr> 375 <th></th> 376 <th></th> 377 </tr> 378 </thead> 379 <tbody> 380 <tr> 381 <td>XmlHttpRequest</td> 382 <td>XMLHTTPRequest</td> 383 </tr> 384 <tr> 385 <td>getCustomerId</td> 386 <td>getCustomerID</td> 387 </tr> 388 <tr> 389 <td>class Html</td> 390 <td>class HTML</td> 391 </tr> 392 <tr> 393 <td>String url</td> 394 <td>String URL</td> 395 </tr> 396 <tr> 397 <td>long id</td> 398 <td>long ID</td> 399 </tr> 400 </tbody> 401 </table> 402 <p> JDK Android </p> 403 404 <h3 id="use-todo-comments"> TODO </h3> 405 <p> TODO TODO TODO </p> 406 <pre><code>// TODO: Remove this code after the UrlTable2 has been checked in. 407 </code></pre> 408 <p></p> 409 <pre><code>// TODO: Change this to use a flag instead of a constant. 410 </code></pre> 411 <p> TODO 2005 11 V7 </p> 412 413 <h3 id="log-sparingly"></h3> 414 <p> 5 </p> 415 <ul> 416 <li><code>ERROR</code> ERROR </li> 417 <li><code>WARNING</code> WARNING </li> 418 <li><code>INFORMATIVE:</code> 419 </li> 420 <li><code>DEBUG</code> VERBOSE 421 <p> <code>if (LOCAL_LOG)</code> <code>if (LOCAL_LOGD)</code> <code>LOCAL_LOG[D]</code> <code>if (LOCAL_LOG)</code> <code>if 422 (LOCAL_LOG)</code> <code>if (LOCAL_LOG)</code> </p> 423 <p> <code>if (localLOGV)</code></p> 424 </li> 425 <li><code>VERBOSE</code> <code>if (LOCAL_LOGV)</code> <code>if (LOCAL_LOGV)</code> 426 </li> 427 </ul> 428 <p><em></em> </p> 429 <ul> 430 <li> VERBOSE </li> 431 <li> VERBOSE DEBUG DEBUG INFORMATIVE </li> 432 <li></li> 433 <li> DEBUG VERBOSE </li> 434 <li> INFORMATIVE </li> 435 <li> DEBUG </li> 436 <li> String <code>+</code> 16 <code>StringBuilder</code> String StringBuilder + <code>Log.v()</code> </li> 437 <li> DEBUG </li> 438 <li> 80 100 130 160 </li> 439 <li> VERBOSE </li> 440 <li> DEBUG VERBOSE if </li> 441 <li></li> 442 <li> <code>System.out.println()</code> <code>printf()</code>System.out System.err /dev/null print </li> 443 <li><em></em></li> 444 </ul> 445 446 <h3 id="be-consistent"></h3> 447 <p> if </p> 448 <p></p> 449 450 <h2 id="javatests-style-rules">Javatests </h2> 451 <p></p> 452 <pre><code>testMethod_specificCase1 testMethod_specificCase2 453 454 void testIsDistinguishable_protanopia() { 455 ColorMatcher colorMatcher = new ColorMatcher(PROTANOPIA) 456 assertFalse(colorMatcher.isDistinguishable(Color.RED, Color.BLACK)) 457 assertTrue(colorMatcher.isDistinguishable(Color.X, Color.Y)) 458 } 459 </code></pre> 460 461 </body></html>