When switch
statements have large sets of multi-line case
clauses, the code becomes hard to read and maintain.
For example, the Cognitive Complexity is going to be particularly high.
In such scenarios, it’s better to refactor the switch
to only have single-line case clauses.
When all the case
clauses of a switch
statement are single-line, the readability of the code is not affected. Moreover,
switch
statements with single-line case
clauses can easily be converted into switch
expressions, which are
more concise for assignment and avoid the need for break
statements.
Exceptions
This rule ignores:
-
switch
statements over Enum
arguments
- fall-through cases
-
return
, break
and throw
statements in case clauses