Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases
like enums or simple value comparisons, a switch
statement is the better alternative. With Java 21, the switch
statement has
been significantly improved to support pattern matching and record pattern.
Using a switch
statement instead of an if-else chain provides benefits like clearer code, certainty of covering all cases, and may
even improve performance.
This rule raises an issue when an if-else chain should be replaced by a switch
statement.