Code Style: Difference between revisions

Content added Content deleted
(→‎Placement: wording)
(→‎White Space: Spotless handles spaces)
Line 300: Line 300:
GTNH Style overrides Sun's to not require break's or default when avoidable. The Sun guide states:
GTNH Style overrides Sun's to not require break's or default when avoidable. The Sun guide states:
"Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added."
"Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added."

== White Space ==
==== Blank Lines ====
One blank line should be used in the following circumstances:
* Between method declarations,
* Between the local variables in a method and its first statement,
* Before a block or single-line comment
* Between logical sections inside a method to improve readability.

==== Blank Spaces ====
Blank spaces should be used in the following circumstances:

1. A keyword followed by a parenthesis should be separated by a space. For example:

<pre>
while (true) {
...
}
</pre>

Note that a blank space should not be used between a method name and its opening parenthesis.
This helps to distinguish keywords from method calls.

2. A blank space should appear after commas in argument lists.
3. All binary operators except "." should be separated from their operands by spaces.
Blank spaces should never separate unary operators such as unary minus, increment ("++"), and decrement ("--") from their operands. Example:

<pre>
a += c + d;
a = (a + b) / (c * d);

while (d++ == s++) {
n++;
}
printSize("size is " + foo + "\n");
</pre>

4. The expressions in a for statement should be separated by spaces. Example:

<pre>
for (expr1; expr2; expr3)
</pre>

Casts should be followed by a blank space. Examples:

<pre>
myMethod((byte) aNum, (Object) x);
myMethod((int) (cp + 5), ((int) (i + 3)) + 1);
</pre>


== Naming ==
== Naming ==