Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 | <title>Google Java Style Guide</title> |
| 6 | <link rel="stylesheet" type="text/css" href="javaguide.css"> |
| 7 | <script language="javascript" src="include/styleguide.js"></script> |
| 8 | <link rel="shortcut icon" type="image/x-icon" href="https://www.google.com/favicon.ico" /> |
| 9 | <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> |
| 10 | </head> |
| 11 | <body onload="initStyleGuide();"> |
| 12 | <div id="content"> |
| 13 | <h1>Google Java Style Guide</h1> |
| 14 | <div class="vertical_toc" id="tocDiv"></div> |
| 15 | |
| 16 | <div class="main_body"> |
| 17 | |
| 18 | <h2 id="s1-introduction">1 Introduction</h2> |
| 19 | |
| 20 | <p>This document serves as the <strong>complete</strong> definition of Google's coding standards for |
| 21 | source code in the Java™ Programming Language. A Java source file is described as being <em>in |
| 22 | Google Style</em> if and only if it adheres to the rules herein.</p> |
| 23 | |
| 24 | <p>Like other programming style guides, the issues covered span not only aesthetic issues of |
| 25 | formatting, but other types of conventions or coding standards as well. However, this document |
| 26 | focuses primarily on the <strong>hard-and-fast rules</strong> that we follow universally, and |
| 27 | avoids giving <em>advice</em> that isn't clearly enforceable (whether by human or tool). |
| 28 | </p> |
| 29 | |
| 30 | |
| 31 | |
| 32 | <h3 id="s1.1-terminology">1.1 Terminology notes</h3> |
| 33 | |
| 34 | <p>In this document, unless otherwise clarified:</p> |
| 35 | |
| 36 | <ol> |
| 37 | <li>The term <em>class</em> is used inclusively to mean an "ordinary" class, enum class, |
| 38 | interface or annotation type (<code class="prettyprint lang-java">@interface</code>).</li> |
| 39 | |
| 40 | <li>The term <em>member</em> (of a class) is used inclusively to mean a nested class, field, |
| 41 | method, <em>or constructor</em>; that is, all top-level contents of a class except initializers |
| 42 | and comments. |
| 43 | |
| 44 | </li><li>The term <em>comment</em> always refers to <em>implementation</em> comments. We do not |
| 45 | use the phrase "documentation comments", instead using the common term "Javadoc."</li> |
| 46 | </ol> |
| 47 | |
| 48 | <p>Other "terminology notes" will appear occasionally throughout the document.</p> |
| 49 | |
| 50 | <h3 id="s1.2-guide-notes">1.2 Guide notes</h3> |
| 51 | |
| 52 | <p>Example code in this document is <strong>non-normative</strong>. That is, while the examples |
| 53 | are in Google Style, they may not illustrate the <em>only</em> stylish way to represent the |
| 54 | code. Optional formatting choices made in examples should not be enforced as rules.</p> |
| 55 | |
| 56 | |
| 57 | <h2 id="s2-source-file-basics">2 Source file basics</h2> |
| 58 | |
| 59 | <h3 id="s2.1-file-name">2.1 File name</h3> |
| 60 | |
| 61 | <p>The source file name consists of the case-sensitive name of the top-level class it contains |
| 62 | (of which there is <a href="#s3.4.1-one-top-level-class">exactly one</a>), plus the |
| 63 | <code>.java</code> extension.</p> |
| 64 | |
| 65 | <h3 id="s2.2-file-encoding">2.2 File encoding: UTF-8</h3> |
| 66 | |
| 67 | <p>Source files are encoded in <strong>UTF-8</strong>.</p> |
| 68 | |
| 69 | <h3 id="s2.3-special-characters">2.3 Special characters</h3> |
| 70 | |
| 71 | <h4 id="s2.3.1-whitespace-characters">2.3.1 Whitespace characters</h4> |
| 72 | |
| 73 | <p>Aside from the line terminator sequence, the <strong>ASCII horizontal space |
| 74 | character</strong> (<strong>0x20</strong>) is the only whitespace character that appears |
| 75 | anywhere in a source file. This implies that:</p> |
| 76 | |
| 77 | <ol> |
| 78 | <li>All other whitespace characters in string and character literals are escaped.</li> |
| 79 | |
| 80 | <li>Tab characters are <strong>not</strong> used for indentation.</li> |
| 81 | </ol> |
| 82 | |
| 83 | <h4 id="s2.3.2-special-escape-sequences">2.3.2 Special escape sequences</h4> |
| 84 | |
| 85 | <p>For any character that has a |
| 86 | <a href="http://docs.oracle.com/javase/tutorial/java/data/characters.html"> |
| 87 | special escape sequence</a> |
| 88 | (<code class="prettyprint lang-java">\b</code>, |
| 89 | <code class="prettyprint lang-java">\t</code>, |
| 90 | <code class="prettyprint lang-java">\n</code>, |
| 91 | <code class="prettyprint lang-java">\f</code>, |
| 92 | <code class="prettyprint lang-java">\r</code>, |
| 93 | <code class="prettyprint lang-java">\"</code>, |
| 94 | <code class="prettyprint lang-java">\'</code> and |
| 95 | <code class="prettyprint lang-java">\\</code>), that sequence |
| 96 | is used rather than the corresponding octal |
| 97 | (e.g. <code class="badcode">\012</code>) or Unicode |
| 98 | (e.g. <code class="badcode">\u000a</code>) escape.</p> |
| 99 | |
| 100 | <h4 id="s2.3.3-non-ascii-characters">2.3.3 Non-ASCII characters</h4> |
| 101 | |
| 102 | <p>For the remaining non-ASCII characters, either the actual Unicode character |
| 103 | (e.g. <code class="prettyprint lang-java">∞</code>) or the equivalent Unicode escape |
| 104 | (e.g. <code class="prettyprint lang-java">\u221e</code>) is used. The choice depends only on |
| 105 | which makes the code <strong>easier to read and understand</strong>, although Unicode escapes |
| 106 | outside string literals and comments are strongly discouraged.</p> |
| 107 | |
| 108 | <p class="tip"><strong>Tip:</strong> In the Unicode escape case, and occasionally even when actual |
| 109 | Unicode characters are used, an explanatory comment can be very helpful.</p> |
| 110 | |
| 111 | <p>Examples:</p> |
| 112 | |
| 113 | <table> |
| 114 | <tbody><tr> |
| 115 | <th>Example</th> |
| 116 | <th>Discussion</th> |
| 117 | </tr> |
| 118 | |
| 119 | <tr> |
| 120 | <td><code class="prettyprint lang-java">String unitAbbrev = "μs";</code></td> |
| 121 | <td>Best: perfectly clear even without a comment.</td> |
| 122 | </tr> |
| 123 | |
| 124 | <tr> |
| 125 | <td><code class="prettyprint lang-java">String unitAbbrev = "\u03bcs"; // "μs"</code></td> |
| 126 | <td>Allowed, but there's no reason to do this.</td> |
| 127 | </tr> |
| 128 | |
| 129 | <tr> |
| 130 | <td><code class="prettyprint lang-java">String unitAbbrev = "\u03bcs"; |
| 131 | // Greek letter mu, "s"</code></td> |
| 132 | <td>Allowed, but awkward and prone to mistakes.</td> |
| 133 | </tr> |
| 134 | |
| 135 | <tr> |
| 136 | <td><code class="badcode">String unitAbbrev = "\u03bcs";</code></td> |
| 137 | <td>Poor: the reader has no idea what this is.</td> |
| 138 | </tr> |
| 139 | |
| 140 | <tr> |
| 141 | <td><code class="prettyprint lang-java">return '\ufeff' + content; |
| 142 | // byte order mark</code></td> |
| 143 | <td>Good: use escapes for non-printable characters, and comment if necessary.</td> |
| 144 | </tr> |
| 145 | </tbody></table> |
| 146 | |
| 147 | <p class="tip"><strong>Tip:</strong> Never make your code less readable simply out of fear that |
| 148 | some programs might not handle non-ASCII characters properly. If that should happen, those |
| 149 | programs are <strong>broken</strong> and they must be <strong>fixed</strong>.</p> |
| 150 | |
| 151 | |
| 152 | <a name="filestructure"></a> |
| 153 | <h2 id="s3-source-file-structure">3 Source file structure</h2> |
| 154 | |
| 155 | <div> |
| 156 | <p>A source file consists of, <strong>in order</strong>:</p> |
| 157 | |
| 158 | <ol> |
| 159 | <li>License or copyright information, if present</li> |
| 160 | <li>Package statement</li> |
| 161 | <li>Import statements</li> |
| 162 | <li>Exactly one top-level class</li> |
| 163 | </ol> |
| 164 | </div> |
| 165 | |
| 166 | <p><strong>Exactly one blank line</strong> separates each section that is present.</p> |
| 167 | |
| 168 | <h3 id="s3.1-copyright-statement">3.1 License or copyright information, if present</h3> |
| 169 | |
| 170 | <p>If license or copyright information belongs in a file, it belongs here.</p> |
| 171 | |
| 172 | |
| 173 | |
| 174 | <h3 id="s3.2-package-statement">3.2 Package statement</h3> |
| 175 | |
| 176 | <p>The package statement is <strong>not line-wrapped</strong>. The column limit (Section 4.4, |
| 177 | <a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to package statements.</p> |
| 178 | |
| 179 | <a name="imports"></a> |
| 180 | <h3 id="s3.3-import-statements">3.3 Import statements</h3> |
| 181 | |
| 182 | <h4 id="s3.3.1-wildcard-imports">3.3.1 No wildcard imports</h4> |
| 183 | |
| 184 | <p><strong>Wildcard imports</strong>, static or otherwise, <strong>are not used</strong>.</p> |
| 185 | |
| 186 | <h4 id="s3.3.2-import-line-wrapping">3.3.2 No line-wrapping</h4> |
| 187 | |
| 188 | <p>Import statements are <strong>not line-wrapped</strong>. The column limit (Section 4.4, |
| 189 | <a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to import |
| 190 | statements.</p> |
| 191 | |
| 192 | <h4 id="s3.3.3-import-ordering-and-spacing">3.3.3 Ordering and spacing</h4> |
| 193 | |
| 194 | <p>Imports are ordered as follows:</p> |
| 195 | |
| 196 | <ol> |
| 197 | <li>All static imports in a single block.</li> |
| 198 | <li>All non-static imports in a single block.</li> |
| 199 | </ol> |
| 200 | |
| 201 | <p>If there are both static and non-static imports, a single blank line separates the two |
| 202 | blocks. There are no other blank lines between import statements.</p> |
| 203 | |
| 204 | <p>Within each block the imported names appear in ASCII sort order. (<strong>Note:</strong> |
| 205 | this is not the same as the import <em>statements</em> being in ASCII sort order, since '.' |
| 206 | sorts before ';'.)</p> |
| 207 | |
| 208 | |
| 209 | |
| 210 | <h4 id="s3.3.4-import-class-not-static">3.3.4 No static import for classes</h4> |
| 211 | |
| 212 | <p>Static import is not used for static nested classes. They are imported with |
| 213 | normal imports.</p> |
| 214 | |
| 215 | <h3 id="s3.4-class-declaration">3.4 Class declaration</h3> |
| 216 | |
| 217 | <a name="oneclassperfile"></a> |
| 218 | <h4 id="s3.4.1-one-top-level-class">3.4.1 Exactly one top-level class declaration</h4> |
| 219 | |
| 220 | <p>Each top-level class resides in a source file of its own.</p> |
| 221 | |
| 222 | <a name="s3.4.2-class-member-ordering"></a> |
| 223 | <h4 id="s3.4.2-ordering-class-contents">3.4.2 Ordering of class contents</h4> |
| 224 | |
| 225 | <p>The order you choose for the members and initializers of your class can have a great effect on |
| 226 | learnability. However, there's no single correct recipe for how to do it; different classes may |
| 227 | order their contents in different ways.</p> |
| 228 | |
| 229 | <p>What is important is that each class uses <strong><em>some</em> logical order</strong>, which its |
| 230 | maintainer could explain if asked. For example, new methods are not just habitually added to the end |
| 231 | of the class, as that would yield "chronological by date added" ordering, which is not a logical |
| 232 | ordering.</p> |
| 233 | |
| 234 | |
| 235 | |
| 236 | <a name="overloads"></a> |
| 237 | <h5 id="s3.4.2.1-overloads-never-split">3.4.2.1 Overloads: never split</h5> |
| 238 | |
| 239 | <p>When a class has multiple constructors, or multiple methods with the same name, these appear |
| 240 | sequentially, with no other code in between (not even private members).</p> |
| 241 | |
| 242 | <h2 id="s4-formatting">4 Formatting</h2> |
| 243 | |
| 244 | <p class="terminology"><strong>Terminology Note:</strong> <em>block-like construct</em> refers to |
| 245 | the body of a class, method or constructor. Note that, by Section 4.8.3.1 on |
| 246 | <a href="#s4.8.3.1-array-initializers">array initializers</a>, any array initializer |
| 247 | <em>may</em> optionally be treated as if it were a block-like construct.</p> |
| 248 | |
| 249 | <a name="braces"></a> |
| 250 | <h3 id="s4.1-braces">4.1 Braces</h3> |
| 251 | |
| 252 | <h4 id="s4.1.1-braces-always-used">4.1.1 Braces are used where optional</h4> |
| 253 | |
| 254 | <p>Braces are used with |
| 255 | <code class="prettyprint lang-java">if</code>, |
| 256 | <code class="prettyprint lang-java">else</code>, |
| 257 | <code class="prettyprint lang-java">for</code>, |
| 258 | <code class="prettyprint lang-java">do</code> and |
| 259 | <code class="prettyprint lang-java">while</code> statements, even when the |
| 260 | body is empty or contains only a single statement.</p> |
| 261 | |
| 262 | <h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K & R style</h4> |
| 263 | |
| 264 | <p>Braces follow the Kernighan and Ritchie style |
| 265 | ("<a href="http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html">Egyptian brackets</a>") |
| 266 | for <em>nonempty</em> blocks and block-like constructs:</p> |
| 267 | |
| 268 | <ul> |
| 269 | <li>No line break before the opening brace.</li> |
| 270 | |
| 271 | <li>Line break after the opening brace.</li> |
| 272 | |
| 273 | <li>Line break before the closing brace.</li> |
| 274 | |
| 275 | <li>Line break after the closing brace, <em>only if</em> that brace terminates a statement or |
| 276 | terminates the body of a method, constructor, or <em>named</em> class. |
| 277 | For example, there is <em>no</em> line break after the brace if it is followed by |
| 278 | <code class="prettyprint lang-java">else</code> or a comma.</li> |
| 279 | </ul> |
| 280 | |
| 281 | <p>Examples:</p> |
| 282 | |
| 283 | <pre class="prettyprint lang-java">return () -> { |
| 284 | while (condition()) { |
| 285 | method(); |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | return new MyClass() { |
| 290 | @Override public void method() { |
| 291 | if (condition()) { |
| 292 | try { |
| 293 | something(); |
| 294 | } catch (ProblemException e) { |
| 295 | recover(); |
| 296 | } |
| 297 | } else if (otherCondition()) { |
| 298 | somethingElse(); |
| 299 | } else { |
| 300 | lastThing(); |
| 301 | } |
| 302 | } |
| 303 | }; |
| 304 | </pre> |
| 305 | |
| 306 | <p>A few exceptions for enum classes are given in Section 4.8.1, |
| 307 | <a href="#s4.8.1-enum-classes">Enum classes</a>.</p> |
| 308 | |
| 309 | <a name="emptyblocks"></a> |
| 310 | <h4 id="s4.1.3-braces-empty-blocks">4.1.3 Empty blocks: may be concise</h4> |
| 311 | |
| 312 | <p>An empty block or block-like construct may be in K & R style (as described in |
| 313 | <a href="#s4.1.2-blocks-k-r-style">Section 4.1.2</a>). Alternatively, it may be closed immediately |
| 314 | after it is opened, with no characters or line break in between |
| 315 | (<code class="prettyprint lang-java">{}</code>), <strong>unless</strong> it is part of a |
| 316 | <em>multi-block statement</em> (one that directly contains multiple blocks: |
| 317 | <code class="prettyprint lang-java">if/else</code> or |
| 318 | <code class="prettyprint lang-java">try/catch/finally</code>).</p> |
| 319 | |
| 320 | <p>Examples:</p> |
| 321 | |
| 322 | <pre class="prettyprint lang-java"> // This is acceptable |
| 323 | void doNothing() {} |
| 324 | |
| 325 | // This is equally acceptable |
| 326 | void doNothingElse() { |
| 327 | } |
| 328 | </pre> |
| 329 | <pre class="prettyprint lang-java badcode"> // This is not acceptable: No concise empty blocks in a multi-block statement |
| 330 | try { |
| 331 | doSomething(); |
| 332 | } catch (Exception e) {} |
| 333 | </pre> |
| 334 | |
| 335 | <h3 id="s4.2-block-indentation">4.2 Block indentation: +2 spaces</h3> |
| 336 | |
| 337 | <p>Each time a new block or block-like construct is opened, the indent increases by two |
| 338 | spaces. When the block ends, the indent returns to the previous indent level. The indent level |
| 339 | applies to both code and comments throughout the block. (See the example in Section 4.1.2, |
| 340 | <a href="#s4.1.2-blocks-k-r-style">Nonempty blocks: K & R Style</a>.)</p> |
| 341 | |
| 342 | <h3 id="s4.3-one-statement-per-line">4.3 One statement per line</h3> |
| 343 | |
| 344 | <p>Each statement is followed by a line break.</p> |
| 345 | |
| 346 | <a name="columnlimit"></a> |
| 347 | <h3 id="s4.4-column-limit">4.4 Column limit: 100</h3> |
| 348 | |
| 349 | <p>Java code has a column limit of 100 characters. A "character" means any Unicode code point. |
| 350 | Except as noted below, any line that would exceed this limit must be line-wrapped, as explained in |
| 351 | Section 4.5, <a href="#s4.5-line-wrapping">Line-wrapping</a>. |
| 352 | </p> |
| 353 | |
| 354 | <p class="tip">Each Unicode code point counts as one character, even if its display width is |
| 355 | greater or less. For example, if using |
| 356 | <a href="https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms">fullwidth characters</a>, |
| 357 | you may choose to wrap the line earlier than where this rule strictly requires.</p> |
| 358 | |
| 359 | <p><strong>Exceptions:</strong></p> |
| 360 | |
| 361 | <ol> |
| 362 | <li>Lines where obeying the column limit is not possible (for example, a long URL in Javadoc, |
| 363 | or a long JSNI method reference).</li> |
| 364 | |
| 365 | <li><code class="prettyprint lang-java">package</code> and |
| 366 | <code class="prettyprint lang-java">import</code> statements (see Sections |
| 367 | 3.2 <a href="#s3.2-package-statement">Package statement</a> and |
| 368 | 3.3 <a href="#s3.3-import-statements">Import statements</a>).</li> |
| 369 | |
| 370 | <li>Command lines in a comment that may be cut-and-pasted into a shell.</li> |
| 371 | </ol> |
| 372 | |
| 373 | <h3 id="s4.5-line-wrapping">4.5 Line-wrapping</h3> |
| 374 | |
| 375 | <p class="terminology"><strong>Terminology Note:</strong> When code that might otherwise legally |
| 376 | occupy a single line is divided into multiple lines, this activity is called |
| 377 | <em>line-wrapping</em>.</p> |
| 378 | |
| 379 | <p>There is no comprehensive, deterministic formula showing <em>exactly</em> how to line-wrap in |
| 380 | every situation. Very often there are several valid ways to line-wrap the same piece of code.</p> |
| 381 | |
| 382 | <p class="note"><strong>Note:</strong> While the typical reason for line-wrapping is to avoid |
| 383 | overflowing the column limit, even code that would in fact fit within the column limit <em>may</em> |
| 384 | be line-wrapped at the author's discretion.</p> |
| 385 | |
| 386 | <p class="tip"><strong>Tip:</strong> Extracting a method or local variable may solve the problem |
| 387 | without the need to line-wrap.</p> |
| 388 | |
| 389 | <h4 id="s4.5.1-line-wrapping-where-to-break">4.5.1 Where to break</h4> |
| 390 | |
| 391 | <p>The prime directive of line-wrapping is: prefer to break at a |
| 392 | <strong>higher syntactic level</strong>. Also:</p> |
| 393 | |
| 394 | <ol> |
| 395 | <li>When a line is broken at a <em>non-assignment</em> operator the break comes <em>before</em> |
| 396 | the symbol. (Note that this is not the same practice used in Google style for other languages, |
| 397 | such as C++ and JavaScript.) |
| 398 | <ul> |
| 399 | <li>This also applies to the following "operator-like" symbols: |
| 400 | <ul> |
| 401 | <li>the dot separator (<code class="prettyprint lang-java">.</code>)</li> |
| 402 | <li>the two colons of a method reference |
| 403 | (<code class="prettyprint lang-java">::</code>)</li> |
| 404 | <li>an ampersand in a type bound |
| 405 | (<code class="prettyprint lang-java"><T extends Foo & Bar></code>)</li> |
| 406 | <li>a pipe in a catch block |
| 407 | (<code class="prettyprint lang-java">catch (FooException | BarException e)</code>).</li> |
| 408 | </ul> |
| 409 | </li> |
| 410 | </ul> |
| 411 | </li> |
| 412 | |
| 413 | <li>When a line is broken at an <em>assignment</em> operator the break typically comes |
| 414 | <em>after</em> the symbol, but either way is acceptable. |
| 415 | <ul> |
| 416 | <li>This also applies to the "assignment-operator-like" colon in an enhanced |
| 417 | <code class="prettyprint lang-java">for</code> ("foreach") statement.</li> |
| 418 | </ul> |
| 419 | </li> |
| 420 | |
| 421 | <li>A method or constructor name stays attached to the open parenthesis |
| 422 | (<code class="prettyprint lang-java">(</code>) that follows it.</li> |
| 423 | |
| 424 | <li>A comma (<code class="prettyprint lang-java">,</code>) stays attached to the token that |
| 425 | precedes it.</li> |
| 426 | |
| 427 | <li>A line is never broken adjacent to the arrow in a lambda, except that a |
| 428 | break may come immediately after the arrow if the body of the lambda consists |
| 429 | of a single unbraced expression. Examples: |
| 430 | <pre class="prettyprint lang-java">MyLambda<String, Long, Object> lambda = |
| 431 | (String label, Long value, Object obj) -> { |
| 432 | ... |
| 433 | }; |
| 434 | |
| 435 | Predicate<String> predicate = str -> |
| 436 | longExpressionInvolving(str); |
| 437 | </pre> |
| 438 | </li> |
| 439 | </ol> |
| 440 | |
| 441 | <p class="note"><strong>Note:</strong> The primary goal for line wrapping is to have clear |
| 442 | code, <em>not necessarily</em> code that fits in the smallest number of lines.</p> |
| 443 | |
| 444 | <a name="indentation"></a> |
| 445 | <h4 id="s4.5.2-line-wrapping-indent">4.5.2 Indent continuation lines at least +4 spaces</h4> |
| 446 | |
| 447 | <p>When line-wrapping, each line after the first (each <em>continuation line</em>) is indented |
| 448 | at least +4 from the original line.</p> |
| 449 | |
| 450 | <p>When there are multiple continuation lines, indentation may be varied beyond +4 as |
| 451 | desired. In general, two continuation lines use the same indentation level if and only if they |
| 452 | begin with syntactically parallel elements.</p> |
| 453 | |
| 454 | <p>Section 4.6.3 on <a href="#s4.6.3-horizontal-alignment">Horizontal alignment</a> addresses |
| 455 | the discouraged practice of using a variable number of spaces to align certain tokens with |
| 456 | previous lines.</p> |
| 457 | |
| 458 | <h3 id="s4.6-whitespace">4.6 Whitespace</h3> |
| 459 | |
| 460 | <h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical Whitespace</h4> |
| 461 | |
Liam Miller-Cushon | f9347e1 | 2018-05-22 16:39:27 -0700 | [diff] [blame] | 462 | <p>A single blank line always appears:</p> |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 463 | |
| 464 | <ol> |
| 465 | <li><em>Between</em> consecutive members or initializers of a class: fields, constructors, |
| 466 | methods, nested classes, static initializers, and instance initializers. |
| 467 | <ul> |
| 468 | <li><span class="exception"><strong>Exception:</strong> A blank line between two consecutive |
| 469 | fields (having no other code between them) is optional. Such blank lines are used as needed to |
| 470 | create <em>logical groupings</em> of fields.</span></li> |
| 471 | <li><span class="exception"><strong>Exception:</strong> Blank lines between enum constants are |
| 472 | covered in <a href="#s4.8.1-enum-classes">Section 4.8.1</a>.</span></li> |
| 473 | </ul> |
| 474 | </li> |
| 475 | |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 476 | <li>As required by other sections of this document (such as Section 3, |
| 477 | <a href="#s3-source-file-structure">Source file structure</a>, and Section 3.3, |
| 478 | <a href="#s3.3-import-statements">Import statements</a>).</li> |
| 479 | </ol> |
| 480 | |
Liam Miller-Cushon | f9347e1 | 2018-05-22 16:39:27 -0700 | [diff] [blame] | 481 | <p>A single blank line may also appear anywhere it improves readability, for example between |
| 482 | statements to organize the code into logical subsections. A blank line before the first member or |
| 483 | initializer, or after the last member or initializer of the class, is neither encouraged nor |
| 484 | discouraged. |
| 485 | |
| 486 | </p><p><em>Multiple</em> consecutive blank lines are permitted, but never required (or encouraged).</p> |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 487 | |
| 488 | <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4> |
| 489 | |
| 490 | <p>Beyond where required by the language or other style rules, and apart from literals, comments and |
| 491 | Javadoc, a single ASCII space also appears in the following places <strong>only</strong>.</p> |
| 492 | |
| 493 | <ol> |
| 494 | <li>Separating any reserved word, such as |
| 495 | <code class="prettyprint lang-java">if</code>, |
| 496 | <code class="prettyprint lang-java">for</code> or |
| 497 | <code class="prettyprint lang-java">catch</code>, from an open parenthesis |
| 498 | (<code class="prettyprint lang-java">(</code>) |
| 499 | that follows it on that line</li> |
| 500 | |
| 501 | <li>Separating any reserved word, such as |
| 502 | <code class="prettyprint lang-java">else</code> or |
| 503 | <code class="prettyprint lang-java">catch</code>, from a closing curly brace |
| 504 | (<code class="prettyprint lang-java">}</code>) that precedes it on that line</li> |
| 505 | |
| 506 | <li>Before any open curly brace |
| 507 | (<code class="prettyprint lang-java">{</code>), with two exceptions: |
| 508 | <ul> |
| 509 | <li><code class="prettyprint lang-java">@SomeAnnotation({a, b})</code> (no space is used)</li> |
| 510 | |
| 511 | <li><code class="prettyprint lang-java">String[][] x = {{"foo"}};</code> (no space is required |
| 512 | between <code class="prettyprint lang-java">{{</code>, by item 8 below)</li> |
| 513 | </ul> |
| 514 | </li> |
| 515 | |
| 516 | <li>On both sides of any binary or ternary operator. This also applies to the following |
| 517 | "operator-like" symbols: |
| 518 | <ul> |
| 519 | <li>the ampersand in a conjunctive type bound: |
| 520 | <code class="prettyprint lang-java"><T extends Foo & Bar></code></li> |
| 521 | |
| 522 | <li>the pipe for a catch block that handles multiple exceptions: |
| 523 | <code class="prettyprint lang-java">catch (FooException | BarException e)</code></li> |
| 524 | |
| 525 | <li>the colon (<code class="prettyprint lang-java">:</code>) in an enhanced |
| 526 | <code class="prettyprint lang-java">for</code> ("foreach") statement</li> |
| 527 | |
| 528 | <li>the arrow in a lambda expression: |
| 529 | <code class="prettyprint lang-java">(String str) -> str.length()</code></li> |
| 530 | </ul> |
| 531 | but not |
| 532 | |
| 533 | <ul> |
| 534 | <li>the two colons (<code class="prettyprint lang-java">::</code>) of a method reference, which |
| 535 | is written like <code class="prettyprint lang-java">Object::toString</code></li> |
| 536 | <li>the dot separator (<code class="prettyprint lang-java">.</code>), which is written like |
| 537 | <code class="prettyprint lang-java">object.toString()</code></li> |
| 538 | </ul> |
| 539 | </li> |
| 540 | |
| 541 | <li>After <code class="prettyprint lang-java">,:;</code> or the closing parenthesis |
| 542 | (<code class="prettyprint lang-java">)</code>) of a cast</li> |
| 543 | |
| 544 | <li>On both sides of the double slash (<code class="prettyprint lang-java">//</code>) that |
| 545 | begins an end-of-line comment. Here, multiple spaces are allowed, but not required.</li> |
| 546 | |
| 547 | <li>Between the type and variable of a declaration: |
| 548 | <code class="prettyprint lang-java">List<String> list</code></li> |
| 549 | |
| 550 | <li><em>Optional</em> just inside both braces of an array initializer |
| 551 | <ul> |
| 552 | <li><code class="prettyprint lang-java">new int[] {5, 6}</code> and |
| 553 | <code class="prettyprint lang-java">new int[] { 5, 6 }</code> are both valid</li> |
| 554 | </ul> |
| 555 | </li> |
Liam Miller-Cushon | f9347e1 | 2018-05-22 16:39:27 -0700 | [diff] [blame] | 556 | |
| 557 | <li>Between a type annotation and <code class="prettyprint lang-java">[]</code> or |
| 558 | <code class="prettyprint lang-java">...</code>.</li> |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 559 | </ol> |
| 560 | |
Liam Miller-Cushon | f9347e1 | 2018-05-22 16:39:27 -0700 | [diff] [blame] | 561 | <p>This rule is never interpreted as requiring or forbidding additional space at the start or |
| 562 | end of a line; it addresses only <em>interior</em> space.</p> |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 563 | |
| 564 | <h4 id="s4.6.3-horizontal-alignment">4.6.3 Horizontal alignment: never required</h4> |
| 565 | |
| 566 | <p class="terminology"><strong>Terminology Note:</strong> <em>Horizontal alignment</em> is the |
| 567 | practice of adding a variable number of additional spaces in your code with the goal of making |
| 568 | certain tokens appear directly below certain other tokens on previous lines.</p> |
| 569 | |
| 570 | <p>This practice is permitted, but is <strong>never required</strong> by Google Style. It is not |
| 571 | even required to <em>maintain</em> horizontal alignment in places where it was already used.</p> |
| 572 | |
| 573 | <p>Here is an example without alignment, then using alignment:</p> |
| 574 | |
| 575 | <pre class="prettyprint lang-java">private int x; // this is fine |
| 576 | private Color color; // this too |
| 577 | |
| 578 | private int x; // permitted, but future edits |
| 579 | private Color color; // may leave it unaligned |
| 580 | </pre> |
| 581 | |
| 582 | <p class="tip"><strong>Tip:</strong> Alignment can aid readability, but it creates problems for |
| 583 | future maintenance. Consider a future change that needs to touch just one line. This change may |
| 584 | leave the formerly-pleasing formatting mangled, and that is <strong>allowed</strong>. More often |
| 585 | it prompts the coder (perhaps you) to adjust whitespace on nearby lines as well, possibly |
| 586 | triggering a cascading series of reformattings. That one-line change now has a "blast radius." |
| 587 | This can at worst result in pointless busywork, but at best it still corrupts version history |
| 588 | information, slows down reviewers and exacerbates merge conflicts.</p> |
| 589 | |
| 590 | <a name="parentheses"></a> |
| 591 | <h3 id="s4.7-grouping-parentheses">4.7 Grouping parentheses: recommended</h3> |
| 592 | |
| 593 | <p>Optional grouping parentheses are omitted only when author and reviewer agree that there is no |
| 594 | reasonable chance the code will be misinterpreted without them, nor would they have made the code |
| 595 | easier to read. It is <em>not</em> reasonable to assume that every reader has the entire Java |
| 596 | operator precedence table memorized.</p> |
| 597 | |
| 598 | <h3 id="s4.8-specific-constructs">4.8 Specific constructs</h3> |
| 599 | |
| 600 | <h4 id="s4.8.1-enum-classes">4.8.1 Enum classes</h4> |
| 601 | |
| 602 | <p>After each comma that follows an enum constant, a line break is optional. Additional blank |
| 603 | lines (usually just one) are also allowed. This is one possibility: |
| 604 | |
| 605 | </p><pre class="prettyprint lang-java">private enum Answer { |
| 606 | YES { |
| 607 | @Override public String toString() { |
| 608 | return "yes"; |
| 609 | } |
| 610 | }, |
| 611 | |
| 612 | NO, |
| 613 | MAYBE |
| 614 | } |
| 615 | </pre> |
| 616 | |
| 617 | <p>An enum class with no methods and no documentation on its constants may optionally be formatted |
| 618 | as if it were an array initializer (see Section 4.8.3.1 on |
| 619 | <a href="#s4.8.3.1-array-initializers">array initializers</a>).</p> |
| 620 | |
| 621 | <pre class="prettyprint lang-java">private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS } |
| 622 | </pre> |
| 623 | |
| 624 | <p>Since enum classes <em>are classes</em>, all other rules for formatting classes apply.</p> |
| 625 | |
| 626 | <a name="localvariables"></a> |
| 627 | <h4 id="s4.8.2-variable-declarations">4.8.2 Variable declarations</h4> |
| 628 | |
| 629 | <h5 id="s4.8.2.1-variables-per-declaration">4.8.2.1 One variable per declaration</h5> |
| 630 | |
| 631 | <p>Every variable declaration (field or local) declares only one variable: declarations such as |
| 632 | <code class="badcode">int a, b;</code> are not used.</p> |
| 633 | |
| 634 | <p><strong>Exception:</strong> Multiple variable declarations are acceptable in the header of a |
| 635 | <code class="prettyprint lang-java">for</code> loop.</p> |
| 636 | |
| 637 | <h5 id="s4.8.2.2-variables-limited-scope">4.8.2.2 Declared when needed</h5> |
| 638 | |
| 639 | <p>Local variables are <strong>not</strong> habitually declared at the start of their containing |
| 640 | block or block-like construct. Instead, local variables are declared close to the point they are |
| 641 | first used (within reason), to minimize their scope. Local variable declarations typically have |
| 642 | initializers, or are initialized immediately after declaration.</p> |
| 643 | |
| 644 | <h4 id="s4.8.3-arrays">4.8.3 Arrays</h4> |
| 645 | |
| 646 | <h5 id="s4.8.3.1-array-initializers">4.8.3.1 Array initializers: can be "block-like"</h5> |
| 647 | |
| 648 | <p>Any array initializer may <em>optionally</em> be formatted as if it were a "block-like |
| 649 | construct." For example, the following are all valid (<strong>not</strong> an exhaustive |
| 650 | list):</p> |
| 651 | |
| 652 | <pre class="prettyprint lang-java">new int[] { new int[] { |
| 653 | 0, 1, 2, 3 0, |
| 654 | } 1, |
| 655 | 2, |
| 656 | new int[] { 3, |
| 657 | 0, 1, } |
| 658 | 2, 3 |
| 659 | } new int[] |
| 660 | {0, 1, 2, 3} |
| 661 | </pre> |
| 662 | |
| 663 | <h5 id="s4.8.3.2-array-declarations">4.8.3.2 No C-style array declarations</h5> |
| 664 | |
| 665 | <p>The square brackets form a part of the <em>type</em>, not the variable: |
| 666 | <code class="prettyprint lang-java">String[] args</code>, not |
| 667 | <code class="badcode">String args[]</code>.</p> |
| 668 | |
| 669 | <h4 id="s4.8.4-switch">4.8.4 Switch statements</h4> |
| 670 | |
| 671 | |
| 672 | |
| 673 | <p class="terminology"><strong>Terminology Note:</strong> Inside the braces of a |
| 674 | <em>switch block</em> are one or more <em>statement groups</em>. Each statement group consists of |
| 675 | one or more <em>switch labels</em> (either <code class="prettyprint lang-java">case FOO:</code> or |
| 676 | <code class="prettyprint lang-java">default:</code>), followed by one or more statements (or, for |
| 677 | the <em>last</em> statement group, <em>zero</em> or more statements).</p> |
| 678 | |
| 679 | <h5 id="s4.8.4.1-switch-indentation">4.8.4.1 Indentation</h5> |
| 680 | |
| 681 | <p>As with any other block, the contents of a switch block are indented +2.</p> |
| 682 | |
| 683 | <p>After a switch label, there is a line break, and the indentation level is increased +2, exactly |
| 684 | as if a block were being opened. The following switch label returns to the previous indentation |
| 685 | level, as if a block had been closed.</p> |
| 686 | |
| 687 | <a name="fallthrough"></a> |
| 688 | <h5 id="s4.8.4.2-switch-fall-through">4.8.4.2 Fall-through: commented</h5> |
| 689 | |
| 690 | <p>Within a switch block, each statement group either terminates abruptly (with a |
| 691 | <code class="prettyprint lang-java">break</code>, |
| 692 | <code class="prettyprint lang-java">continue</code>, |
| 693 | <code class="prettyprint lang-java">return</code> or thrown exception), or is marked with a comment |
| 694 | to indicate that execution will or <em>might</em> continue into the next statement group. Any |
| 695 | comment that communicates the idea of fall-through is sufficient (typically |
| 696 | <code class="prettyprint lang-java">// fall through</code>). This special comment is not required in |
| 697 | the last statement group of the switch block. Example:</p> |
| 698 | |
| 699 | <pre class="prettyprint lang-java">switch (input) { |
| 700 | case 1: |
| 701 | case 2: |
| 702 | prepareOneOrTwo(); |
| 703 | // fall through |
| 704 | case 3: |
| 705 | handleOneTwoOrThree(); |
| 706 | break; |
| 707 | default: |
| 708 | handleLargeNumber(input); |
| 709 | } |
| 710 | </pre> |
| 711 | |
| 712 | <p>Notice that no comment is needed after <code class="prettyprint lang-java">case 1:</code>, only |
| 713 | at the end of the statement group.</p> |
| 714 | |
| 715 | <h5 id="s4.8.4.3-switch-default">4.8.4.3 The <code>default</code> case is present</h5> |
| 716 | |
| 717 | <p>Each switch statement includes a <code class="prettyprint lang-java">default</code> statement |
| 718 | group, even if it contains no code.</p> |
| 719 | |
| 720 | <p><strong>Exception:</strong> A switch statement for an <code>enum</code> type <em>may</em> omit |
| 721 | the <code class="prettyprint lang-java">default</code> statement group, <em>if</em> it includes |
| 722 | explicit cases covering <em>all</em> possible values of that type. This enables IDEs or other static |
| 723 | analysis tools to issue a warning if any cases were missed. |
| 724 | |
| 725 | </p> |
| 726 | |
| 727 | <a name="annotations"></a> |
| 728 | <h4 id="s4.8.5-annotations">4.8.5 Annotations</h4> |
| 729 | |
| 730 | <p>Annotations applying to a class, method or constructor appear immediately after the |
| 731 | documentation block, and each annotation is listed on a line of its own (that is, one annotation |
| 732 | per line). These line breaks do not constitute line-wrapping (Section |
| 733 | 4.5, <a href="#s4.5-line-wrapping">Line-wrapping</a>), so the indentation level is not |
| 734 | increased. Example:</p> |
| 735 | |
| 736 | <pre class="prettyprint lang-java">@Override |
| 737 | @Nullable |
| 738 | public String getNameIfPresent() { ... } |
| 739 | </pre> |
| 740 | |
| 741 | <p class="exception"><strong>Exception:</strong> A <em>single</em> parameterless annotation |
| 742 | <em>may</em> instead appear together with the first line of the signature, for example:</p> |
| 743 | |
| 744 | <pre class="prettyprint lang-java">@Override public int hashCode() { ... } |
| 745 | </pre> |
| 746 | |
| 747 | <p>Annotations applying to a field also appear immediately after the documentation block, but in |
| 748 | this case, <em>multiple</em> annotations (possibly parameterized) may be listed on the same line; |
| 749 | for example:</p> |
| 750 | |
| 751 | <pre class="prettyprint lang-java">@Partial @Mock DataLoader loader; |
| 752 | </pre> |
| 753 | |
| 754 | <p>There are no specific rules for formatting annotations on parameters, local variables, or types. |
| 755 | </p> |
| 756 | |
| 757 | <a name="comments"></a> |
| 758 | <h4 id="s4.8.6-comments">4.8.6 Comments</h4> |
| 759 | |
| 760 | <p>This section addresses <em>implementation comments</em>. Javadoc is addressed separately in |
| 761 | Section 7, <a href="#s7-javadoc">Javadoc</a>.</p> |
| 762 | |
| 763 | <p>Any line break may be preceded by arbitrary whitespace followed by an implementation comment. |
| 764 | Such a comment renders the line non-blank.</p> |
| 765 | |
| 766 | <h5 id="s4.8.6.1-block-comment-style">4.8.6.1 Block comment style</h5> |
| 767 | |
| 768 | <p>Block comments are indented at the same level as the surrounding code. They may be in |
| 769 | <code class="prettyprint lang-java">/* ... */</code> style or |
| 770 | <code class="prettyprint lang-java">// ...</code> style. For multi-line |
| 771 | <code class="prettyprint lang-java">/* ... */</code> comments, subsequent lines must start with |
| 772 | <code>*</code> aligned with the <code>*</code> on the previous line.</p> |
| 773 | |
| 774 | <pre class="prettyprint lang-java">/* |
| 775 | * This is // And so /* Or you can |
| 776 | * okay. // is this. * even do this. */ |
| 777 | */ |
| 778 | </pre> |
| 779 | |
| 780 | |
| 781 | <p>Comments are not enclosed in boxes drawn with asterisks or other characters.</p> |
| 782 | |
| 783 | <p class="tip"><strong>Tip:</strong> When writing multi-line comments, use the |
| 784 | <code class="prettyprint lang-java">/* ... */</code> style if you want automatic code formatters to |
| 785 | re-wrap the lines when necessary (paragraph-style). Most formatters don't re-wrap lines in |
| 786 | <code class="prettyprint lang-java">// ...</code> style comment blocks.</p> |
| 787 | |
| 788 | |
| 789 | |
| 790 | <a name="modifiers"></a> |
| 791 | <h4 id="s4.8.7-modifiers">4.8.7 Modifiers</h4> |
| 792 | |
| 793 | <p>Class and member modifiers, when present, appear in the order |
| 794 | recommended by the Java Language Specification: |
| 795 | </p> |
| 796 | |
| 797 | <pre>public protected private abstract default static final transient volatile synchronized native strictfp |
| 798 | </pre> |
| 799 | |
| 800 | <h4 id="s4.8.8-numeric-literals">4.8.8 Numeric Literals</h4> |
| 801 | |
| 802 | <p><code>long</code>-valued integer literals use an uppercase <code>L</code> suffix, never |
| 803 | lowercase (to avoid confusion with the digit <code>1</code>). For example, <code>3000000000L</code> |
| 804 | rather than <code class="badcode">3000000000l</code>.</p> |
| 805 | |
| 806 | <a name="naming"></a> |
| 807 | <h2 id="s5-naming">5 Naming</h2> |
| 808 | |
| 809 | <h3 id="s5.1-identifier-names">5.1 Rules common to all identifiers</h3> |
| 810 | |
| 811 | <p>Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, |
| 812 | underscores. Thus each valid identifier name is matched by the regular expression |
| 813 | <code>\w+</code> .</p> |
| 814 | |
Liam Miller-Cushon | f9347e1 | 2018-05-22 16:39:27 -0700 | [diff] [blame] | 815 | <p>In Google Style, special prefixes or suffixes are <strong>not</strong> used. For example, these |
| 816 | names are not Google Style: <code class="badcode">name_</code>, <code class="badcode">mName</code>, |
| 817 | <code class="badcode">s_name</code> and <code class="badcode">kName</code>.</p> |
Ted Osborne | 505ba68 | 2018-01-30 12:36:50 -0500 | [diff] [blame] | 818 | |
| 819 | <h3 id="s5.2-specific-identifier-names">5.2 Rules by identifier type</h3> |
| 820 | |
| 821 | <h4 id="s5.2.1-package-names">5.2.1 Package names</h4> |
| 822 | |
| 823 | <p>Package names are all lowercase, with consecutive words simply concatenated together (no |
| 824 | underscores). For example, <code>com.example.deepspace</code>, not |
| 825 | <code class="badcode">com.example.deepSpace</code> or |
| 826 | <code class="badcode">com.example.deep_space</code>.</p> |
| 827 | |
| 828 | <h4 id="s5.2.2-class-names">5.2.2 Class names</h4> |
| 829 | |
| 830 | <p>Class names are written in <a href="#s5.3-camel-case">UpperCamelCase</a>.</p> |
| 831 | |
| 832 | <p>Class names are typically nouns or noun phrases. For example, |
| 833 | <code class="prettyprint lang-java">Character</code> or |
| 834 | <code class="prettyprint lang-java">ImmutableList</code>. Interface names may also be nouns or |
| 835 | noun phrases (for example, <code class="prettyprint lang-java">List</code>), but may sometimes be |
| 836 | adjectives or adjective phrases instead (for example, |
| 837 | <code class="prettyprint lang-java">Readable</code>).</p> |
| 838 | |
| 839 | <p>There are no specific rules or even well-established conventions for naming annotation types.</p> |
| 840 | |
| 841 | <p><em>Test</em> classes are named starting with the name of the class they are testing, and ending |
| 842 | with <code class="prettyprint lang-java">Test</code>. For example, |
| 843 | <code class="prettyprint lang-java">HashTest</code> or |
| 844 | <code class="prettyprint lang-java">HashIntegrationTest</code>.</p> |
| 845 | |
| 846 | <h4 id="s5.2.3-method-names">5.2.3 Method names</h4> |
| 847 | |
| 848 | <p>Method names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> |
| 849 | |
| 850 | <p>Method names are typically verbs or verb phrases. For example, |
| 851 | <code class="prettyprint lang-java">sendMessage</code> or |
| 852 | <code class="prettyprint lang-java">stop</code>.</p> |
| 853 | |
| 854 | <p>Underscores may appear in JUnit <em>test</em> method names to separate logical components of the |
| 855 | name, with <em>each</em> component written in <a href="#s5.3-camel-case">lowerCamelCase</a>. |
| 856 | One typical pattern is <code><i><methodUnderTest></i>_<i><state></i></code>, |
| 857 | for example <code class="prettyprint lang-java">pop_emptyStack</code>. There is no One Correct |
| 858 | Way to name test methods.</p> |
| 859 | |
| 860 | <a name="constants"></a> |
| 861 | <h4 id="s5.2.4-constant-names">5.2.4 Constant names</h4> |
| 862 | |
| 863 | <p>Constant names use <code class="prettyprint lang-java">CONSTANT_CASE</code>: all uppercase |
| 864 | letters, with each word separated from the next by a single underscore. But what <em>is</em> a |
| 865 | constant, exactly?</p> |
| 866 | |
| 867 | <p>Constants are static final fields whose contents are deeply immutable and whose methods have no |
| 868 | detectable side effects. This includes primitives, Strings, immutable types, and immutable |
| 869 | collections of immutable types. If any of the instance's observable state can change, it is not a |
| 870 | constant. Merely <em>intending</em> to never mutate the object is not enough. Examples:</p> |
| 871 | |
| 872 | <pre class="prettyprint lang-java">// Constants |
| 873 | static final int NUMBER = 5; |
| 874 | static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann"); |
| 875 | static final ImmutableMap<String, Integer> AGES = ImmutableMap.of("Ed", 35, "Ann", 32); |
| 876 | static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is immutable |
| 877 | static final SomeMutableType[] EMPTY_ARRAY = {}; |
| 878 | enum SomeEnum { ENUM_CONSTANT } |
| 879 | |
| 880 | // Not constants |
| 881 | static String nonFinal = "non-final"; |
| 882 | final String nonStatic = "non-static"; |
| 883 | static final Set<String> mutableCollection = new HashSet<String>(); |
| 884 | static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable); |
| 885 | static final ImmutableMap<String, SomeMutableType> mutableValues = |
| 886 | ImmutableMap.of("Ed", mutableInstance, "Ann", mutableInstance2); |
| 887 | static final Logger logger = Logger.getLogger(MyClass.getName()); |
| 888 | static final String[] nonEmptyArray = {"these", "can", "change"}; |
| 889 | </pre> |
| 890 | |
| 891 | <p>These names are typically nouns or noun phrases.</p> |
| 892 | |
| 893 | <h4 id="s5.2.5-non-constant-field-names">5.2.5 Non-constant field names</h4> |
| 894 | |
| 895 | <p>Non-constant field names (static or otherwise) are written |
| 896 | in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> |
| 897 | |
| 898 | <p>These names are typically nouns or noun phrases. For example, |
| 899 | <code class="prettyprint lang-java">computedValues</code> or |
| 900 | <code class="prettyprint lang-java">index</code>.</p> |
| 901 | |
| 902 | <h4 id="s5.2.6-parameter-names">5.2.6 Parameter names</h4> |
| 903 | |
| 904 | <p>Parameter names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> |
| 905 | |
| 906 | <p>One-character parameter names in public methods should be avoided.</p> |
| 907 | |
| 908 | <h4 id="s5.2.7-local-variable-names">5.2.7 Local variable names</h4> |
| 909 | |
| 910 | <p>Local variable names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> |
| 911 | |
| 912 | <p>Even when final and immutable, local variables are not considered to be constants, and should not |
| 913 | be styled as constants.</p> |
| 914 | |
| 915 | <h4 id="s5.2.8-type-variable-names">5.2.8 Type variable names</h4> |
| 916 | |
| 917 | <p>Each type variable is named in one of two styles:</p> |
| 918 | |
| 919 | <ul> |
| 920 | <li>A single capital letter, optionally followed by a single numeral (such as |
| 921 | <code class="prettyprint lang-java">E</code>, <code class="prettyprint lang-java">T</code>, |
| 922 | <code class="prettyprint lang-java">X</code>, <code class="prettyprint lang-java">T2</code>) |
| 923 | </li> |
| 924 | |
| 925 | <li>A name in the form used for classes (see Section 5.2.2, |
| 926 | <a href="#s5.2.2-class-names">Class names</a>), followed by the capital letter |
| 927 | <code class="prettyprint lang-java">T</code> (examples: |
| 928 | <code class="prettyprint lang-java">RequestT</code>, |
| 929 | <code class="prettyprint lang-java">FooBarT</code>).</li> |
| 930 | </ul> |
| 931 | |
| 932 | <a name="acronyms"></a> |
| 933 | <a name="camelcase"></a> |
| 934 | <h3 id="s5.3-camel-case">5.3 Camel case: defined</h3> |
| 935 | |
| 936 | <p>Sometimes there is more than one reasonable way to convert an English phrase into camel case, |
| 937 | such as when acronyms or unusual constructs like "IPv6" or "iOS" are present. To improve |
| 938 | predictability, Google Style specifies the following (nearly) deterministic scheme.</p> |
| 939 | |
| 940 | <p>Beginning with the prose form of the name:</p> |
| 941 | |
| 942 | <ol> |
| 943 | <li>Convert the phrase to plain ASCII and remove any apostrophes. For example, "Müller's |
| 944 | algorithm" might become "Muellers algorithm".</li> |
| 945 | |
| 946 | <li>Divide this result into words, splitting on spaces and any remaining punctuation (typically |
| 947 | hyphens). |
| 948 | |
| 949 | <ul> |
| 950 | <li><em>Recommended:</em> if any word already has a conventional camel-case appearance in common |
| 951 | usage, split this into its constituent parts (e.g., "AdWords" becomes "ad words"). Note |
| 952 | that a word such as "iOS" is not really in camel case <em>per se</em>; it defies <em>any</em> |
| 953 | convention, so this recommendation does not apply.</li> |
| 954 | </ul> |
| 955 | </li> |
| 956 | |
| 957 | <li>Now lowercase <em>everything</em> (including acronyms), then uppercase only the first |
| 958 | character of: |
| 959 | <ul> |
| 960 | <li>... each word, to yield <em>upper camel case</em>, or</li> |
| 961 | <li>... each word except the first, to yield <em>lower camel case</em></li> |
| 962 | </ul> |
| 963 | </li> |
| 964 | |
| 965 | <li>Finally, join all the words into a single identifier.</li> |
| 966 | </ol> |
| 967 | |
| 968 | <p>Note that the casing of the original words is almost entirely disregarded. Examples:</p> |
| 969 | |
| 970 | <table> |
| 971 | <tbody><tr> |
| 972 | <th>Prose form</th> |
| 973 | <th>Correct</th> |
| 974 | <th>Incorrect</th> |
| 975 | </tr> |
| 976 | <tr> |
| 977 | <td>"XML HTTP request"</td> |
| 978 | <td><code class="prettyprint lang-java">XmlHttpRequest</code></td> |
| 979 | <td><code class="badcode">XMLHTTPRequest</code></td> |
| 980 | </tr> |
| 981 | <tr> |
| 982 | <td>"new customer ID"</td> |
| 983 | <td><code class="prettyprint lang-java">newCustomerId</code></td> |
| 984 | <td><code class="badcode">newCustomerID</code></td> |
| 985 | </tr> |
| 986 | <tr> |
| 987 | <td>"inner stopwatch"</td> |
| 988 | <td><code class="prettyprint lang-java">innerStopwatch</code></td> |
| 989 | <td><code class="badcode">innerStopWatch</code></td> |
| 990 | </tr> |
| 991 | <tr> |
| 992 | <td>"supports IPv6 on iOS?"</td> |
| 993 | <td><code class="prettyprint lang-java">supportsIpv6OnIos</code></td> |
| 994 | <td><code class="badcode">supportsIPv6OnIOS</code></td> |
| 995 | </tr> |
| 996 | <tr> |
| 997 | <td>"YouTube importer"</td> |
| 998 | <td><code class="prettyprint lang-java">YouTubeImporter</code><br> |
| 999 | <code class="prettyprint lang-java">YoutubeImporter</code>*</td> |
| 1000 | <td></td> |
| 1001 | </tr> |
| 1002 | </tbody></table> |
| 1003 | |
| 1004 | <p>*Acceptable, but not recommended.</p> |
| 1005 | |
| 1006 | <p class="note"><strong>Note:</strong> Some words are ambiguously hyphenated in the English |
| 1007 | language: for example "nonempty" and "non-empty" are both correct, so the method names |
| 1008 | <code class="prettyprint lang-java">checkNonempty</code> and |
| 1009 | <code class="prettyprint lang-java">checkNonEmpty</code> are likewise both correct.</p> |
| 1010 | |
| 1011 | |
| 1012 | <h2 id="s6-programming-practices">6 Programming Practices</h2> |
| 1013 | |
| 1014 | <h3 id="s6.1-override-annotation">6.1 <code>@Override</code>: always used</h3> |
| 1015 | |
| 1016 | <p>A method is marked with the <code class="prettyprint lang-java">@Override</code> annotation |
| 1017 | whenever it is legal. This includes a class method overriding a superclass method, a class method |
| 1018 | implementing an interface method, and an interface method respecifying a superinterface |
| 1019 | method.</p> |
| 1020 | |
| 1021 | <p class="exception"><strong>Exception:</strong> |
| 1022 | <code class="prettyprint lang-java">@Override</code> may be omitted when the parent method is |
| 1023 | <code class="prettyprint lang-java">@Deprecated</code>.</p> |
| 1024 | |
| 1025 | <a name="caughtexceptions"></a> |
| 1026 | <h3 id="s6.2-caught-exceptions">6.2 Caught exceptions: not ignored</h3> |
| 1027 | |
| 1028 | <p>Except as noted below, it is very rarely correct to do nothing in response to a caught |
| 1029 | exception. (Typical responses are to log it, or if it is considered "impossible", rethrow it as an |
| 1030 | <code class="prettyprint lang-java">AssertionError</code>.)</p> |
| 1031 | |
| 1032 | <p>When it truly is appropriate to take no action whatsoever in a catch block, the reason this is |
| 1033 | justified is explained in a comment.</p> |
| 1034 | |
| 1035 | <pre class="prettyprint lang-java">try { |
| 1036 | int i = Integer.parseInt(response); |
| 1037 | return handleNumericResponse(i); |
| 1038 | } catch (NumberFormatException ok) { |
| 1039 | // it's not numeric; that's fine, just continue |
| 1040 | } |
| 1041 | return handleTextResponse(response); |
| 1042 | </pre> |
| 1043 | |
| 1044 | <p class="exception"><strong>Exception:</strong> In tests, a caught exception may be ignored |
| 1045 | without comment <em>if</em> its name is or begins with <code class="prettyprint lang-java">expected</code>. The |
| 1046 | following is a very common idiom for ensuring that the code under test <em>does</em> throw an |
| 1047 | exception of the expected type, so a comment is unnecessary here.</p> |
| 1048 | |
| 1049 | <pre class="prettyprint lang-java">try { |
| 1050 | emptyStack.pop(); |
| 1051 | fail(); |
| 1052 | } catch (NoSuchElementException expected) { |
| 1053 | } |
| 1054 | </pre> |
| 1055 | |
| 1056 | <h3 id="s6.3-static-members">6.3 Static members: qualified using class</h3> |
| 1057 | |
| 1058 | <p>When a reference to a static class member must be qualified, it is qualified with that class's |
| 1059 | name, not with a reference or expression of that class's type.</p> |
| 1060 | |
| 1061 | <pre class="prettyprint lang-java">Foo aFoo = ...; |
| 1062 | Foo.aStaticMethod(); // good |
| 1063 | <span class="badcode">aFoo.aStaticMethod();</span> // bad |
| 1064 | <span class="badcode">somethingThatYieldsAFoo().aStaticMethod();</span> // very bad |
| 1065 | </pre> |
| 1066 | |
| 1067 | <a name="finalizers"></a> |
| 1068 | <h3 id="s6.4-finalizers">6.4 Finalizers: not used</h3> |
| 1069 | |
| 1070 | <p>It is <strong>extremely rare</strong> to override <code class="prettyprint |
| 1071 | lang-java">Object.finalize</code>.</p> |
| 1072 | |
| 1073 | <p class="tip"><strong>Tip:</strong> Don't do it. If you absolutely must, first read and understand |
| 1074 | |
| 1075 | |
| 1076 | <a href="http://books.google.com/books?isbn=8131726592"><em>Effective Java</em> Item 7,</a> |
| 1077 | |
| 1078 | "Avoid Finalizers," very carefully, and <em>then</em> don't do it.</p> |
| 1079 | |
| 1080 | |
| 1081 | <a name="javadoc"></a> |
| 1082 | <h2 id="s7-javadoc">7 Javadoc</h2> |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | <h3 id="s7.1-javadoc-formatting">7.1 Formatting</h3> |
| 1087 | |
| 1088 | <h4 id="s7.1.1-javadoc-multi-line">7.1.1 General form</h4> |
| 1089 | |
| 1090 | <p>The <em>basic</em> formatting of Javadoc blocks is as seen in this example:</p> |
| 1091 | |
| 1092 | <pre class="prettyprint lang-java">/** |
| 1093 | * Multiple lines of Javadoc text are written here, |
| 1094 | * wrapped normally... |
| 1095 | */ |
| 1096 | public int method(String p1) { ... } |
| 1097 | </pre> |
| 1098 | |
| 1099 | <p>... or in this single-line example:</p> |
| 1100 | |
| 1101 | <pre class="prettyprint lang-java">/** An especially short bit of Javadoc. */ |
| 1102 | </pre> |
| 1103 | |
| 1104 | <p>The basic form is always acceptable. The single-line form may be substituted when the entirety |
| 1105 | of the Javadoc block (including comment markers) can fit on a single line. Note that this only |
| 1106 | applies when there are no block tags such as <code>@return</code>. |
| 1107 | |
| 1108 | </p><h4 id="s7.1.2-javadoc-paragraphs">7.1.2 Paragraphs</h4> |
| 1109 | |
| 1110 | <p>One blank line—that is, a line containing only the aligned leading asterisk |
| 1111 | (<code>*</code>)—appears between paragraphs, and before the group of block tags if |
| 1112 | present. Each paragraph but the first has <code><p></code> immediately before the first word, |
| 1113 | with no space after.</p> |
| 1114 | |
| 1115 | <a name="s7.1.3-javadoc-at-clauses"></a> |
| 1116 | |
| 1117 | <h4 id="s7.1.3-javadoc-block-tags">7.1.3 Block tags</h4> |
| 1118 | |
| 1119 | <p>Any of the standard "block tags" that are used appear in the order <code>@param</code>, |
| 1120 | <code>@return</code>, <code>@throws</code>, <code>@deprecated</code>, and these four types never |
| 1121 | appear with an empty description. When a block tag doesn't fit on a single line, continuation lines |
| 1122 | are indented four (or more) spaces from the position of the <code>@</code>. |
| 1123 | </p> |
| 1124 | |
| 1125 | <h3 id="s7.2-summary-fragment">7.2 The summary fragment</h3> |
| 1126 | |
| 1127 | <p>Each Javadoc block begins with a brief <strong>summary fragment</strong>. This |
| 1128 | fragment is very important: it is the only part of the text that appears in certain contexts such as |
| 1129 | class and method indexes.</p> |
| 1130 | |
| 1131 | <p>This is a fragment—a noun phrase or verb phrase, not a complete sentence. It does |
| 1132 | <strong>not</strong> begin with <code class="badcode">A {@code Foo} is a...</code>, or |
| 1133 | <code class="badcode">This method returns...</code>, nor does it form a complete imperative sentence |
| 1134 | like <code class="badcode">Save the record.</code>. However, the fragment is capitalized and |
| 1135 | punctuated as if it were a complete sentence.</p> |
| 1136 | |
| 1137 | <p class="tip"><strong>Tip:</strong> A common mistake is to write simple Javadoc in the form |
| 1138 | <code class="badcode">/** @return the customer ID */</code>. This is incorrect, and should be |
| 1139 | changed to <code class="prettyprint lang-java">/** Returns the customer ID. */</code>.</p> |
| 1140 | |
| 1141 | <a name="s7.3.3-javadoc-optional"></a> |
| 1142 | <h3 id="s7.3-javadoc-where-required">7.3 Where Javadoc is used</h3> |
| 1143 | |
| 1144 | <p>At the <em>minimum</em>, Javadoc is present for every |
| 1145 | <code class="prettyprint lang-java">public</code> class, and every |
| 1146 | <code class="prettyprint lang-java">public</code> or |
| 1147 | <code class="prettyprint lang-java">protected</code> member of such a class, with a few exceptions |
| 1148 | noted below.</p> |
| 1149 | |
| 1150 | <p>Additional Javadoc content may also be present, as explained in Section 7.3.4, |
| 1151 | <a href="#s7.3.4-javadoc-non-required">Non-required Javadoc</a>.</p> |
| 1152 | |
| 1153 | <h4 id="s7.3.1-javadoc-exception-self-explanatory">7.3.1 Exception: self-explanatory methods</h4> |
| 1154 | |
| 1155 | <p>Javadoc is optional for "simple, obvious" methods like |
| 1156 | <code class="prettyprint lang-java">getFoo</code>, in cases where there <em>really and truly</em> is |
| 1157 | nothing else worthwhile to say but "Returns the foo".</p> |
| 1158 | |
| 1159 | <p class="note"><strong>Important:</strong> it is not appropriate to cite this exception to justify |
| 1160 | omitting relevant information that a typical reader might need to know. For example, for a method |
| 1161 | named <code class="prettyprint lang-java">getCanonicalName</code>, don't omit its documentation |
| 1162 | (with the rationale that it would say only |
| 1163 | <code class="badcode">/** Returns the canonical name. */</code>) if a typical reader may have no idea |
| 1164 | what the term "canonical name" means!</p> |
| 1165 | |
| 1166 | <h4 id="s7.3.2-javadoc-exception-overrides">7.3.2 Exception: overrides</h4> |
| 1167 | |
| 1168 | <p>Javadoc is not always present on a method that overrides a supertype method. |
| 1169 | |
| 1170 | </p> |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | <h4 id="s7.3.4-javadoc-non-required">7.3.4 Non-required Javadoc</h4> |
| 1175 | |
| 1176 | <p>Other classes and members have Javadoc <em>as needed or desired</em>. |
| 1177 | |
| 1178 | </p><p>Whenever an implementation comment would be used to define the overall purpose or behavior of a |
| 1179 | class or member, that comment is written as Javadoc instead (using <code>/**</code>).</p> |
| 1180 | |
| 1181 | <p>Non-required Javadoc is not strictly required to follow the formatting rules of Sections |
| 1182 | 7.1.2, 7.1.3, and 7.2, though it is of course recommended.</p> |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | </div> |
| 1187 | </div> |
| 1188 | </body> |
| 1189 | </html> |