Consolidate Conditional Expression
You have multiple conditionals that lead to the same result or action.
Consolidate all these conditionals in a single expression.
Your code contains many alternating operators that perform identical actions. It is not clear why the operators are split up.
The main purpose of consolidation is to extract the conditional to a separate method for greater clarity.
Eliminates duplicate control flow code. Combining multiple conditionals that have the same "destination" helps to show that you are doing only one complicated check leading to one action.
By consolidating all operators, you can now isolate this complex expression in a new method with a name that explains the conditional's purpose.
How to Refactor
Before refactoring, make sure that the conditionals do not have any "side effects" or otherwise modify something, instead of simply returning values. Side effects may be hiding in the code executed inside the operator itself, such as when something is added to a variable based on the results of a conditional.
- Consolidate the conditionals in a single expression by using
or. As a general rule when consolidating:
- Nested conditionals are joined using
- Consecutive conditionals are joined with
- Perform Extract Method on the operator conditions and give the method a name that reflects the expression's purpose.