Skip to content

Files

Latest commit

 

History

History
56 lines (42 loc) · 1.05 KB

DefaultComesLast.md

File metadata and controls

56 lines (42 loc) · 1.05 KB

Pattern: Misplaced default in switch statement

Issue: -

Description

Java allows default anywhere within the switch statement, but it is more readable if it comes after the last case.

Default configuration

<module name="DefaultComesLast">
    <property name="skipIfLastAndSharedWithCase" value="true"/>
</module>

skipIfLastAndSharedWithCase - whether to allow default along with case if they are not last.

Examples

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;          
}

Further Reading