Pattern: Missing switch default
clause
Issue: -
This rule enforces to use default case in every switch
statement.
Rationale:
- To 'catch' an unexpected value. Even if the developer is sure that all currently possible cases are covered, this should be expressed in the default branch, e.g. by using an assertion. This way the code is protected against later changes.
- To handle 'default' actions, where the cases are for special behavior.
- To show someone reading your code that you've covered that case.
<module name="MissingSwitchDefault"/>
Example of incorrect code:
switch(type)
{
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
}
Example of correct code:
switch(type)
{
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
default:
// unknown type!
// there should probably be some error-handling here, maybe an exception
}