Skip to content

Commit 097bbdc

Browse files
Merge branch 'main' into feature/devonfw#103-implement-version-security-checks
2 parents be3ec96 + fdf3447 commit 097bbdc

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

documentation/coding-conventions.asciidoc

+43-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Instead always rethrow with the original exception:
7979
try {
8080
doSomething();
8181
} catch (Exception e) {
82-
// good
82+
// fine
8383
throw new IllegalStateExeception("Something failed", e);
8484
}
8585
----
@@ -113,7 +113,7 @@ Instead include the full exception and use your logger properly:
113113
try {
114114
doSomething();
115115
} catch (Exception e) {
116-
// good
116+
// fine
117117
LOG.error("Something failed: {}", e.getMessage(), e);
118118
}
119119
----
@@ -147,7 +147,7 @@ public Boolean isEmpty {
147147
Instead always use the primitive `boolean` type:
148148
[source,java]
149149
----
150-
// good
150+
// fine
151151
public boolean isEmpty {
152152
return size() == 0;
153153
}
@@ -174,7 +174,7 @@ Instead we should better do this:
174174
[source,java]
175175
----
176176
public class MavenDownloader {
177-
// good
177+
// fine
178178
/** The base URL of the central maven repository. */
179179
public static final String REPOSITORY_URL = "https://repo1.maven.org/maven2/"
180180
public void download(Dependency dependency) {
@@ -191,7 +191,7 @@ As stated above in case of an interface simply omit the modifiers:
191191
[source,java]
192192
----
193193
public interface MavenDownloader {
194-
// good
194+
// fine
195195
/** The base URL of the central maven repository. */
196196
String REPOSITORY_URL = "https://repo1.maven.org/maven2/"
197197
void download(Dependency dependency);
@@ -237,7 +237,7 @@ The reason is that it is most likely causing a lot of merge conflicts for featur
237237
Instead keep the existing signature and add a new one:
238238
[source,java]
239239
----
240-
// good
240+
// fine
241241
public void doSomething() {
242242
doSomething(false);
243243
}
@@ -278,7 +278,7 @@ try {
278278
Better explicitly check for `null`:
279279
[source,java]
280280
----
281-
// good
281+
// fine
282282
Foo foo = null;
283283
if (variable != null) {
284284
foo = variable.getFoo();
@@ -311,7 +311,7 @@ if (variable.getFoo().getFirst().getSize() > variable.getFoo().getSecond().getSi
311311
The method `getFoo()` is used in 4 places and called 3 times. Maybe the method call is expensive?
312312
[source,java]
313313
----
314-
// good
314+
// fine
315315
Candidate candidate;
316316
Foo foo = variable.getFoo();
317317
Candidate first = foo.getFirst();
@@ -433,6 +433,41 @@ try (InputStream in = new FileInputStream(file)) {
433433
}
434434
----
435435

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+
436471
== Lambdas and Streams
437472
With Java8 you have cool new features like lambdas and monads like (`Stream`, `CompletableFuture`, `Optional`, etc.).
438473
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

Comments
 (0)