Skip to content

Commit 31c830a

Browse files
authored
Merge pull request #671 from cushon/gh-pages
Update Java style guide
2 parents e6233c7 + 8941662 commit 31c830a

File tree

1 file changed

+95
-44
lines changed

1 file changed

+95
-44
lines changed

javaguide.html

+95-44
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h3 id="s1.1-terminology">1.1 Terminology notes</h3>
4242
and comments.
4343

4444
</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>
45+
use the phrase "documentation comments", and instead use the common term "Javadoc."</li>
4646
</ol>
4747

4848
<p>Other "terminology notes" will appear occasionally throughout the document.</p>
@@ -236,8 +236,10 @@ <h4 id="s3.4.2-ordering-class-contents">3.4.2 Ordering of class contents</h4>
236236
<a name="overloads"></a>
237237
<h5 id="s3.4.2.1-overloads-never-split">3.4.2.1 Overloads: never split</h5>
238238

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>
239+
<p>Methods of a class that share the same name appear in a single contiguous group with no other
240+
members in between. The same applies to multiple constructors (which always have the same name).
241+
This rule applies even when modifiers such as <code class="prettyprint lang-java">static</code> or
242+
<code class="prettyprint lang-java">private</code> differ between the methods.</p>
241243

242244
<h2 id="s4-formatting">4 Formatting</h2>
243245

@@ -249,7 +251,7 @@ <h2 id="s4-formatting">4 Formatting</h2>
249251
<a name="braces"></a>
250252
<h3 id="s4.1-braces">4.1 Braces</h3>
251253

252-
<h4 id="s4.1.1-braces-always-used">4.1.1 Braces are used where optional</h4>
254+
<h4 id="s4.1.1-braces-always-used">4.1.1 Use of optional braces</h4>
253255

254256
<p>Braces are used with
255257
<code class="prettyprint lang-java">if</code>,
@@ -259,14 +261,16 @@ <h4 id="s4.1.1-braces-always-used">4.1.1 Braces are used where optional</h4>
259261
<code class="prettyprint lang-java">while</code> statements, even when the
260262
body is empty or contains only a single statement.</p>
261263

264+
<p>Other optional braces, such as those in a lambda expression, remain optional.</p>
265+
262266
<h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K &amp; R style</h4>
263267

264268
<p>Braces follow the Kernighan and Ritchie style
265269
("<a href="http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html">Egyptian brackets</a>")
266270
for <em>nonempty</em> blocks and block-like constructs:</p>
267271

268272
<ul>
269-
<li>No line break before the opening brace.</li>
273+
<li>No line break before the opening brace, except as detailed below.</li>
270274

271275
<li>Line break after the opening brace.</li>
272276

@@ -278,6 +282,11 @@ <h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K &amp; R style</h4>
278282
<code class="prettyprint lang-java">else</code> or a comma.</li>
279283
</ul>
280284

285+
<p>Exception: In places where these rules allow a single statement ending with a semicolon
286+
(<code class="prettyprint lang-java">;</code>), a block of statements can appear, and the opening
287+
brace of this block is preceded by a line break. Blocks like these are typically introduced to
288+
limit the scope of local variables, for example inside switch statements.</p>
289+
281290
<p>Examples:</p>
282291

283292
<pre class="prettyprint lang-java">return () -&gt; {
@@ -299,6 +308,10 @@ <h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K &amp; R style</h4>
299308
} else {
300309
lastThing();
301310
}
311+
{
312+
int x = foo();
313+
frob(x);
314+
}
302315
}
303316
};
304317
</pre>
@@ -367,7 +380,14 @@ <h3 id="s4.4-column-limit">4.4 Column limit: 100</h3>
367380
3.2 <a href="#s3.2-package-statement">Package statement</a> and
368381
3.3 <a href="#s3.3-import-statements">Import statements</a>).</li>
369382

370-
<li>Command lines in a comment that may be cut-and-pasted into a shell.</li>
383+
<li>Command lines in a comment that may be copied-and-pasted into a shell.</li>
384+
385+
<li>Very long identifiers, on the rare occasions they are called for, are allowed to exceed the
386+
column limit. In that case, the valid wrapping for the surrounding code is as produced by
387+
388+
389+
<a href="https://github.com/google/google-java-format">google-java-format</a>.
390+
</li>
371391
</ol>
372392

373393
<h3 id="s4.5-line-wrapping">4.5 Line-wrapping</h3>
@@ -509,7 +529,7 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
509529
<li><code class="prettyprint lang-java">@SomeAnnotation({a, b})</code> (no space is used)</li>
510530

511531
<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>
532+
between <code class="prettyprint lang-java">{{</code>, by item 9 below)</li>
513533
</ul>
514534
</li>
515535

@@ -541,8 +561,11 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
541561
<li>After <code class="prettyprint lang-java">,:;</code> or the closing parenthesis
542562
(<code class="prettyprint lang-java">)</code>) of a cast</li>
543563

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>
564+
<li>Between any content and a double slash (<code class="prettyprint lang-java">//</code>) which
565+
begins a comment. Multiple spaces are allowed.</li>
566+
567+
<li>Between a double slash (<code class="prettyprint lang-java">//</code>) which begins a comment
568+
and the comment's text. Multiple spaces are allowed.</li>
546569

547570
<li>Between the type and variable of a declaration:
548571
<code class="prettyprint lang-java">List&lt;String&gt; list</code></li>
@@ -712,7 +735,7 @@ <h5 id="s4.8.4.2-switch-fall-through">4.8.4.2 Fall-through: commented</h5>
712735
<p>Notice that no comment is needed after <code class="prettyprint lang-java">case 1:</code>, only
713736
at the end of the statement group.</p>
714737

715-
<h5 id="s4.8.4.3-switch-default">4.8.4.3 The <code>default</code> case is present</h5>
738+
<h5 id="s4.8.4.3-switch-default">4.8.4.3 Presence of the <code>default</code> label</h5>
716739

717740
<p>Each switch statement includes a <code class="prettyprint lang-java">default</code> statement
718741
group, even if it contains no code.</p>
@@ -727,14 +750,37 @@ <h5 id="s4.8.4.3-switch-default">4.8.4.3 The <code>default</code> case is presen
727750
<a name="annotations"></a>
728751
<h4 id="s4.8.5-annotations">4.8.5 Annotations</h4>
729752

730-
<p>Annotations applying to a class, method or constructor appear immediately after the
753+
<h5 id="s4.8.5.1-type-use-annotation-style">4.8.5.1 Type-use annotations</h5>
754+
755+
<p>Type-use annotations appear immediately before the annotated type. An annotation is a type-use
756+
annotation if it is meta-annotated with
757+
<code class="prettyprint lang-java">@Target(ElementType.TYPE_USE)</code>. Example:</p>
758+
759+
<pre class="prettyprint lang-java">final @Nullable String name;
760+
761+
public @Nullable Person getPersonByName(String name);
762+
</pre>
763+
764+
<h5 id="s4.8.5.2-class-annotation-style">4.8.5.2 Class annotations</h5>
765+
766+
<p>Annotations applying to a class appear immediately after the
731767
documentation block, and each annotation is listed on a line of its own (that is, one annotation
732768
per line). These line breaks do not constitute line-wrapping (Section
733769
4.5, <a href="#s4.5-line-wrapping">Line-wrapping</a>), so the indentation level is not
734770
increased. Example:</p>
735771

736-
<pre class="prettyprint lang-java">@Override
737-
@Nullable
772+
<pre class="prettyprint lang-java">@Deprecated
773+
@CheckReturnValue
774+
public final class Frozzler { ... }
775+
</pre>
776+
777+
<h5 id="s4.8.5.3-method-annotation-style">4.8.5.3 Method and constructor annotations</h5>
778+
779+
<p>The rules for annotations on method and constructor declarations are the same as the
780+
<a href="#s4.8.5.2-class-annotation-style">previous section</a>. Example: </p>
781+
782+
<pre class="prettyprint lang-java">@Deprecated
783+
@Override
738784
public String getNameIfPresent() { ... }
739785
</pre>
740786

@@ -744,15 +790,19 @@ <h4 id="s4.8.5-annotations">4.8.5 Annotations</h4>
744790
<pre class="prettyprint lang-java">@Override public int hashCode() { ... }
745791
</pre>
746792

793+
<h5 id="s4.8.5.4-field-annotation-style">4.8.5.4 Field annotations</h5>
794+
747795
<p>Annotations applying to a field also appear immediately after the documentation block, but in
748796
this case, <em>multiple</em> annotations (possibly parameterized) may be listed on the same line;
749797
for example:</p>
750798

751799
<pre class="prettyprint lang-java">@Partial @Mock DataLoader loader;
752800
</pre>
753801

754-
<p>There are no specific rules for formatting annotations on parameters, local variables, or types.
755-
</p>
802+
<h5 id="s4.8.5.5-local-parameter-annotation-style">4.8.5.5 Parameter and local variable annotations</h5>
803+
804+
<p>There are no specific rules for formatting annotations on parameters or local variables (except,
805+
of course, when the annotation is a type-use annotation).</p>
756806

757807
<a name="comments"></a>
758808
<h4 id="s4.8.6-comments">4.8.6 Comments</h4>
@@ -820,8 +870,8 @@ <h3 id="s5.2-specific-identifier-names">5.2 Rules by identifier type</h3>
820870

821871
<h4 id="s5.2.1-package-names">5.2.1 Package names</h4>
822872

823-
<p>Package names are all lowercase, with consecutive words simply concatenated together (no
824-
underscores). For example, <code>com.example.deepspace</code>, not
873+
<p>Package names use only lowercase letters and digits (no underscores). Consecutive words are
874+
simply concatenated together. For example, <code>com.example.deepspace</code>, not
825875
<code class="badcode">com.example.deepSpace</code> or
826876
<code class="badcode">com.example.deep_space</code>.</p>
827877

@@ -838,10 +888,11 @@ <h4 id="s5.2.2-class-names">5.2.2 Class names</h4>
838888

839889
<p>There are no specific rules or even well-established conventions for naming annotation types.</p>
840890

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>
891+
<p>A <em>test</em> class has a name that ends with <code class="prettyprint lang-java">Test</code>,
892+
for example, <code class="prettyprint lang-java">HashIntegrationTest</code>.
893+
If it covers a single class, its name is the name of that class
894+
plus <code class="prettyprint lang-java">Test</code>, for example <code class="prettyprint
895+
lang-java">HashImplTest</code>.</p>
845896

846897
<h4 id="s5.2.3-method-names">5.2.3 Method names</h4>
847898

@@ -852,30 +903,28 @@ <h4 id="s5.2.3-method-names">5.2.3 Method names</h4>
852903
<code class="prettyprint lang-java">stop</code>.</p>
853904

854905
<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>&lt;methodUnderTest&gt;</i>_<i>&lt;state&gt;</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>
906+
name, with <em>each</em> component written in <a href="#s5.3-camel-case">lowerCamelCase</a>, for
907+
example <code class="prettyprint lang-java">transferMoney_deductsFromSource</code>. There is no One
908+
Correct Way to name test methods.</p>
859909

860910
<a name="constants"></a>
861911
<h4 id="s5.2.4-constant-names">5.2.4 Constant names</h4>
862912

863-
<p>Constant names use <code class="prettyprint lang-java">CONSTANT_CASE</code>: all uppercase
913+
<p>Constant names use <code class="prettyprint lang-java">UPPER_SNAKE_CASE</code>: all uppercase
864914
letters, with each word separated from the next by a single underscore. But what <em>is</em> a
865915
constant, exactly?</p>
866916

867917
<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
918+
detectable side effects. Examples include primitives, strings, immutable value classes, and anything
919+
set to <code>null</code>. If any of the instance's observable state can change, it is not a
870920
constant. Merely <em>intending</em> to never mutate the object is not enough. Examples:</p>
871921

872922
<pre class="prettyprint lang-java">// Constants
873923
static final int NUMBER = 5;
874924
static final ImmutableList&lt;String&gt; NAMES = ImmutableList.of("Ed", "Ann");
875-
static final ImmutableMap&lt;String, Integer&gt; AGES = ImmutableMap.of("Ed", 35, "Ann", 32);
925+
static final Map&lt;String, Integer&gt; AGES = ImmutableMap.of("Ed", 35, "Ann", 32);
876926
static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is immutable
877927
static final SomeMutableType[] EMPTY_ARRAY = {};
878-
enum SomeEnum { ENUM_CONSTANT }
879928

880929
// Not constants
881930
static String nonFinal = "non-final";
@@ -1073,9 +1122,9 @@ <h3 id="s6.4-finalizers">6.4 Finalizers: not used</h3>
10731122
<p class="tip"><strong>Tip:</strong> Don't do it. If you absolutely must, first read and understand
10741123

10751124

1076-
<a href="http://books.google.com/books?isbn=8131726592"><em>Effective Java</em> Item 7,</a>
1125+
<a href="http://books.google.com/books?isbn=0134686047"><em>Effective Java</em> Item 8</a>,
10771126

1078-
"Avoid Finalizers," very carefully, and <em>then</em> don't do it.</p>
1127+
"Avoid finalizers and cleaners" very carefully, and <em>then</em> don't do it.</p>
10791128

10801129

10811130
<a name="javadoc"></a>
@@ -1107,14 +1156,15 @@ <h4 id="s7.1.1-javadoc-multi-line">7.1.1 General form</h4>
11071156

11081157
</p><h4 id="s7.1.2-javadoc-paragraphs">7.1.2 Paragraphs</h4>
11091158

1110-
<p>One blank line&#8212;that is, a line containing only the aligned leading asterisk
1111-
(<code>*</code>)&#8212;appears between paragraphs, and before the group of block tags if
1112-
present. Each paragraph but the first has <code>&lt;p&gt;</code> immediately before the first word,
1113-
with no space after.</p>
1159+
<p>One blank line—that is, a line containing only the aligned leading asterisk
1160+
(<code>*</code>)—appears between paragraphs, and before the group of block tags if present.
1161+
Each paragraph except the first has <code>&lt;p&gt;</code> immediately before the first word, with
1162+
no space after it. HTML tags for other block-level elements, such as <code>&lt;ul&gt;</code> or
1163+
<code>&lt;table&gt;</code>, are <em>not</em> preceded with <code>&lt;p&gt;</code>.</p>
11141164

11151165
<a name="s7.1.3-javadoc-at-clauses"></a>
11161166

1117-
<h4 id="s7.1.3-javadoc-block-tags">7.1.3 Block tags</h4>
1167+
</p><h4 id="s7.1.3-javadoc-block-tags">7.1.3 Block tags</h4>
11181168

11191169
<p>Any of the standard "block tags" that are used appear in the order <code>@param</code>,
11201170
<code>@return</code>, <code>@throws</code>, <code>@deprecated</code>, and these four types never
@@ -1135,8 +1185,9 @@ <h3 id="s7.2-summary-fragment">7.2 The summary fragment</h3>
11351185
punctuated as if it were a complete sentence.</p>
11361186

11371187
<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>
1188+
<code class="badcode prettyprint lang-java">/** @return the customer ID */</code>. This is
1189+
incorrect, and should be changed to
1190+
<code class="prettyprint lang-java">/** Returns the customer ID. */</code>.</p>
11401191

11411192
<a name="s7.3.3-javadoc-optional"></a>
11421193
<h3 id="s7.3-javadoc-where-required">7.3 Where Javadoc is used</h3>
@@ -1150,11 +1201,11 @@ <h3 id="s7.3-javadoc-where-required">7.3 Where Javadoc is used</h3>
11501201
<p>Additional Javadoc content may also be present, as explained in Section 7.3.4,
11511202
<a href="#s7.3.4-javadoc-non-required">Non-required Javadoc</a>.</p>
11521203

1153-
<h4 id="s7.3.1-javadoc-exception-self-explanatory">7.3.1 Exception: self-explanatory methods</h4>
1204+
<h4 id="s7.3.1-javadoc-exception-self-explanatory">7.3.1 Exception: self-explanatory members</h4>
11541205

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>
1206+
<p>Javadoc is optional for "simple, obvious" members like
1207+
<code class="prettyprint lang-java">getFoo()</code>, in cases where there <em>really and truly</em>
1208+
is nothing else worthwhile to say but "Returns the foo".</p>
11581209

11591210
<p class="note"><strong>Important:</strong> it is not appropriate to cite this exception to justify
11601211
omitting relevant information that a typical reader might need to know. For example, for a method
@@ -1179,7 +1230,7 @@ <h4 id="s7.3.4-javadoc-non-required">7.3.4 Non-required Javadoc</h4>
11791230
class or member, that comment is written as Javadoc instead (using <code>/**</code>).</p>
11801231

11811232
<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>
1233+
7.1.1, 7.1.2, 7.1.3, and 7.2, though it is of course recommended.</p>
11831234

11841235

11851236

0 commit comments

Comments
 (0)