'if' can be replaced with 'switch'
Reports if
statements that can be replaced with switch
statements.
The replacement result is usually shorter and clearer.
Example:
After the quick-fix is applied:
This inspection only reports if the language level of the project or module is 7 or higher.
Use the Minimum number of 'if' condition branches field to specify the minimum number of if
condition branches for an if
statement to have to be reported. Note that the terminal else
branch (without if
) is not counted.
Use the Suggest switch on numbers option to enable the suggestion of switch
statements on primitive and boxed numbers and characters.
Use the Suggest switch on enums option to enable the suggestion of switch
statements on enum
constants.
Use the Only suggest on null-safe expressions option to suggest switch
statements that can't introduce a NullPointerException
only.
Inspection options
Option | Type | Default |
---|---|---|
Minimum number of 'if' condition branches | Number | 3 |
Suggest switch on numbers | Checkbox | false |
Suggest switch on enums | Checkbox | false |
Only suggest on null-safe expressions | Checkbox | true |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |