@@ -79,7 +79,7 @@ Instead always rethrow with the original exception:
79
79
try {
80
80
doSomething();
81
81
} catch (Exception e) {
82
- // good
82
+ // fine
83
83
throw new IllegalStateExeception("Something failed", e);
84
84
}
85
85
----
@@ -113,7 +113,7 @@ Instead include the full exception and use your logger properly:
113
113
try {
114
114
doSomething();
115
115
} catch (Exception e) {
116
- // good
116
+ // fine
117
117
LOG.error("Something failed: {}", e.getMessage(), e);
118
118
}
119
119
----
@@ -147,7 +147,7 @@ public Boolean isEmpty {
147
147
Instead always use the primitive `boolean` type:
148
148
[source,java]
149
149
----
150
- // good
150
+ // fine
151
151
public boolean isEmpty {
152
152
return size() == 0;
153
153
}
@@ -174,7 +174,7 @@ Instead we should better do this:
174
174
[source,java]
175
175
----
176
176
public class MavenDownloader {
177
- // good
177
+ // fine
178
178
/** The base URL of the central maven repository. */
179
179
public static final String REPOSITORY_URL = "https://repo1.maven.org/maven2/"
180
180
public void download(Dependency dependency) {
@@ -191,7 +191,7 @@ As stated above in case of an interface simply omit the modifiers:
191
191
[source,java]
192
192
----
193
193
public interface MavenDownloader {
194
- // good
194
+ // fine
195
195
/** The base URL of the central maven repository. */
196
196
String REPOSITORY_URL = "https://repo1.maven.org/maven2/"
197
197
void download(Dependency dependency);
@@ -237,7 +237,7 @@ The reason is that it is most likely causing a lot of merge conflicts for featur
237
237
Instead keep the existing signature and add a new one:
238
238
[source,java]
239
239
----
240
- // good
240
+ // fine
241
241
public void doSomething() {
242
242
doSomething(false);
243
243
}
@@ -278,7 +278,7 @@ try {
278
278
Better explicitly check for `null`:
279
279
[source,java]
280
280
----
281
- // good
281
+ // fine
282
282
Foo foo = null;
283
283
if (variable != null) {
284
284
foo = variable.getFoo();
@@ -311,7 +311,7 @@ if (variable.getFoo().getFirst().getSize() > variable.getFoo().getSecond().getSi
311
311
The method `getFoo()` is used in 4 places and called 3 times. Maybe the method call is expensive?
312
312
[source,java]
313
313
----
314
- // good
314
+ // fine
315
315
Candidate candidate;
316
316
Foo foo = variable.getFoo();
317
317
Candidate first = foo.getFirst();
@@ -433,6 +433,41 @@ try (InputStream in = new FileInputStream(file)) {
433
433
}
434
434
----
435
435
436
+ == Enclose blocks with curly braces
437
+ In Java curly braces for blocks can be omitted if there is only a single statement:
438
+ [source,java]
439
+ ----
440
+ // bad
441
+ if (condition())
442
+ doThis();
443
+ else
444
+ doThat();
445
+ ----
446
+ While this is not really wrong it can lead to problems e.g. when adding a statement:
447
+ [source,java]
448
+ ----
449
+ // bad
450
+ if (condition())
451
+ doThis();
452
+ else
453
+ doThat();
454
+ System.err.println("that");
455
+ ----
456
+ Now, it gets hard to see that the last statement is always executed independent of the condition.
457
+ Maybe that should actually go only to the else block as we can guess from the indentation.
458
+ If you always use curly braces this cannot happen and the code is easier to read:
459
+ [source,java]
460
+ ----
461
+ // fine
462
+ if (condition()) {
463
+ doThis();
464
+ } else {
465
+ doThat();
466
+ System.err.println("that");
467
+ }
468
+ //System.err.println("that");
469
+ ----
470
+
436
471
== Lambdas and Streams
437
472
With Java8 you have cool new features like lambdas and monads like (`Stream`, `CompletableFuture`, `Optional`, etc.).
438
473
However, these new features can also be misused or led to code that is hard to read or debug. To avoid pain, we give you the following best practices:
0 commit comments