Skip to main content

Combine/Split Conditional(s)

  • 2 minutes to read

Purpose

Combines two or more neighboring conditionals with identical bodies into a single conditional statement and vice versa. This Refactoring allows you to switch between nested or cascading conditionals and the single conditional with the complex logical expression where each condition is combined with others by the logical AND (&&) or OR (||) operation respectively.

Availability

The Combine Conditionals Refactoring is available in the following cases.

  • When the caret is on an if keyword, provided that the previous or following statement is another conditional with a similar body. The body must have a return, break or continue jump statement in the end.
  • When the caret is on an if keyword, provided that the body contains a nested conditional.

The Split Conditional Refactoring is available when the caret is on an if keyword provided that the conditional expression is a complex logical expression where each condition is combined with others by the logical AND (&&) or OR (||) operation.

Usage

  1. Place the caret on the if keyword of any of the identical conditions.

    Note

    The blinking cursor shows the caret’s position at which the Refactoring is available.

    if (conditionA)
        return;
    if (conditionB)
        return;
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Combine Conditionals from the menu.

After execution, the Refactoring combines the conditions into one using the logical OR operation.

if (conditionA || conditionB)
    return;
See Also