Pattern: Misplaced default
in switch
statement
Issue: -
Java allows default
anywhere within the switch
statement, but it is more readable if it comes after the last case
.
<module name="DefaultComesLast">
<property name="skipIfLastAndSharedWithCase" value="true"/>
</module>
skipIfLastAndSharedWithCase
- whether to allow default
along with case
if they are not last.
Example of incorrect code:
switch (type) {
case 1:
handleTypeOne();
break;
default:
handleUnknownType();
break;
case 2:
handleTypeTwo();
break;
}
Example of correct code:
switch (type) {
case 1:
handleTypeOne();
break;
case 2:
handleTypeTwo();
break;
default:
handleUnknownType();
break;
}