29 lines
803 B
Plaintext
29 lines
803 B
Plaintext
This rule applies whenever an <code>if</code> statement is followed by one or more <code>else if</code> statements; the final <code>else if</code> should be followed by an <code>else</code> statement.
|
|
|
|
The requirement for a final <code>else</code> statement is defensive programming.
|
|
The <code>else</code> statement should either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final <code>case _</code> clause in a <code>match</code>.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
if (x == 0) {
|
|
doSomething
|
|
} else if (x == 1) {
|
|
doSomethingElse
|
|
}
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
if (x == 0) {
|
|
doSomething
|
|
} else if (x == 1) {
|
|
doSomethingElse
|
|
} else {
|
|
throw new IllegalStateException
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|