Skip to main content
A newer version of this page is available. .

Flatten Conditional

  • 2 minutes to read

Purpose

This Refactoring helps you simplify your conditional clauses, and thus increases code readability in the following cases.

  • When the nested conditionals can be replaced with the guard clause.
  • When a part of conditional is redundant and can be removed.
  • When the conditional clause can be replaced by the tested expression (e.g., when a boolean value is assigned or returned within the if block).

Availability

Available when the caret is on an if statement, providing the surrounding code can be simplified.

Usage

  1. Place the caret on a suitable if statement.

    NOTE

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

    public bool MyBinaryOperator(bool a, bool b) {
        if (a)
            if (!b)
                return true;
        return false;
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Flatten Conditional from the menu.

After execution, the Refactoring simplifies the conditional clauses.

public bool MyBinaryOperator(bool a, bool b) {
    if (!a)
        return false;
    if (!b)
        return true;
    return false;
}
See Also