Code Issues for Switch (Select) and Case statements
A switch statement executes logic, dependent on the value of a given expression (parameter). The types of values a switch statement operates on can be boolean, enum, integral types, and strings. Each switch statement can contain any number of case statements, but no two case constants within the same switch statement can have the same value. Each case statement defines a value to compare against the original expression. You may also include a default label following all other case statements. If none of the other choices match, then the default choice is taken and its statements are executed. If there is no default label, control is transferred outside the switch.
CodeRush Pro provides several code issues specific to a switch statement (C#) or Select statement (VB) as well as its nested case statements. These code issues are of type warning, dead code and code smell.
Empty case statement
Cause:
This code issue is shown when the Switch or Select statement is empty and can be safely removed. If no code is executed inside the case statement, it is redundant.
Sample:
How to fix:
- Remove the empty case statement.
Default branch is missing
Cause:
This is a code issue of the warning type, no matter that the default label is optional. It is shown when the switch or select statement is missing a default label which may be unintentional.
Sample:
How to fix:
- Apply the Add Default Case Branch code fix:
Case statements do not explicitly handle all enum values
Cause:
An expression of a case statement can be of the enumeration type. Enumeration types have a definite number of enum elements. If the switch or select statement handles only a subset of the possible enumeration values it’s checking for, the code issue will be shown because this may be a sign of incomplete code.
Sample:
How to fix:
- Apply the Add Missing Case Statements code fix:
- Add a default branch:
Case statement has incorrect range of integers expression
Cause:
This code issue is useful for the Select statement in Visual Basic, because case statements in VB can handle a specific range of values, not only a single constant value. The range of the values is defined according to the language specification using the “To” operator as follows:
“Case 20 To 30”.
However, if the range expression is specified incorrectly, the code issue of the code smell type is shown.
Sample:
The case statement in the sample actually specifies a single value “-10”, because the expression range is treated as a mathematical minus operation, in other words: 20 – 30 = -10.
How to fix:
- Replace the incorrect integer expressions with the “To” operator:
Similar Posts:
- Code fixes for code issues for switch (select) statements
- Refactoring multi-conditional statements: the switch statement and the “if-else-if” ladder
- Generating the Switch/Select statement for enumeration variables
- Declaring enumeration types and its elements in CodeRush
- Code Issues – Base type constructors are not implemented