|
http://www.jindent.com |
Previous: Do-While/While/For
|
Next: Switch-Case
|
| Brace style for if-else statements |

| Left brace { new line |
if-else statements appears in a new line
or not.|
if (a < 10) { ... |
|
if (a < 10) { ... |
| Right brace } new line |
if-else statements appears in a new line
or not.|
if (a < 10) { ... } else { ... } |
|
if (a < 10) { ... } else { ... } |
| Indent left brace { |
|
if·(a·<·10) ····{ ····... |
|
if·(a·<·10) { ... |
|
if·(a·<·10)·{ ... |
|
if·(a·<·10){ ... |
| Indent right brace } |
|
if·(a·<·10) ····{ ····... ····} |
|
if·(a·<·10) { ····... } |
| Indent after right brace } |
if-else statements.|
if (a < 10) { ... }else { ... } |
|
if (a < 10) { ... } else { ... } |
| Cuddle braces of empty blocks {} |
if-else blocks.|
if (condition) {} |
|
if (condition) { } |
| Indent cuddled braces {} |
|
if·(condition){} |
|
if·(condition)·{} |
| Prohibit blank lines after left brace { |
if-else blocks.if-else blocks
an unnecessary gap can appear.|
if (a < 10) { ¶ // comment with a preceding blank line callAnotherMethod(); ¶ // another comment ... } |
|
if (a < 10) { // comment with a preceding blank line callAnotherMethod(); ¶ // another comment ... } |
|
See also: | Jindent - Settings - Formatter - Java / SQLJ - Blank Lines - Comments , Jindent - Settings - Formatter - Java / SQLJ - Blank Lines |
| If number of lines in body is at least ... then insert blank line after { |
if-else statements
containing a certain number of lines in their bodies.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { callMethodA(); callMethodB(); } else { callMethodC(); callMethodD(); } |
if (a < 10) looks fine,
but statement if (b < 10) { seems to be formatted too tight:
The statement head if (b < 10) { and the first block statement
callMethodA(i); are connected too close and could cause confusion.
if-else statements use setting
"If number of lines in body is at least ... then insert blank line after {" to insert an extra blank line before the first
block statement.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); } else { ¶ callMethodC(); callMethodD(); } |
if (b < 10) { seems to be much clearer and
statement if (a < 10) { is still formatted as before.if-else body and if at least
2 lines are counted then an extra blank line is inserted after the left brace of if-else head.if-else statements can be kept compact, but statements with an
extended body are formatted more spacious and clearer.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { callMethodA(); callMethodB(); } else { callMethodC(); callMethodD(); } |
if (x == 0) { is formatted in the same way as while (a < 10) {.|
if (a < 10) { ¶ callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); } else { ¶ callMethodC(); callMethodD(); } |
infinite.| If number of lines in body is at least ... then insert blank line before } |
if-else statements
containing a certain number of lines in their bodies.if-else statement bodies.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); ¶ } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); ¶ } |
| Do not insert blank line before single left brace |
|
if (a < 10) { callMethod(--a); } // <- this right brace appears alone ¶ if (a < 10) { callMethodA(); callMethodB(); } else { // <- this right brace does not appear alone callMethodC(); callMethodC(); } // <- this right brace appears alone |
} else { does not appear alone,
it is directly followed by an else token.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); ¶ } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); ¶ } |
if-else statements, but for
all other simple if statements the extra blank lines before the right braces
seems to be unnecessary.|
if (a < 10) { callMethod(--a); } ¶ if (b < 10) { ¶ callMethodA(i); for (int i = 0; i < n; i++) { callMethodB(i); callMethodC(i); } callMethodD(i); } ¶ if (x == 0) { ¶ callMethodA(); callMethodB(); ¶ } else { ¶ callMethodC(); callMethodD(); } |
} else { because this is the only case a
right brace does not appear as a single brace.
|
See also: | Jindent - Settings - Formatter - Java / SQLJ - Braces Style - Presets |