CodeRush Xpress

Refactorings for Conditionals

 

Combine Conditionals

Combines nested conditionals to into a binary expression performing a logical AND operation. For example, "if (e1) if (e2)" becomes "if (e1 && e2)". This refactoring is the opposite of Split Conditional. This refactoring can also combine two or more neighboring conditionals with identical bodies into a single conditional statement where each conditional expression is logically OR’d.

Here's an example where nested conditionals can be combined:

Refactoring - Combine Conditionals

And here's an example where neighboring conditionals with identical bodies can be combined:

Refactoring - Combine Conditionals

Combine conditionals will also remove any redundancy that might appear in the newly combined expression. For example, notice in the preview hint for the following how the reference to the hasQualified parameter appears only once:

Refactoring - Combine Conditionals

Compress to Ternary Expression

Converts an if/else conditional with assignments in each branch into a ternary expression. This refactoring is the opposite of Expand Ternary Expression.

Refactoring - Compress to Ternary Expression

Expand Ternary Expression

Converts a ternary expression into an if/else block. This refactoring is the opposite of Compress to Ternary Expression.

Before:

[C#]

column = cellPosition == CellPosition.Last ? _NumColumns - 1 : 1;

After:

[C#]

if (cellPosition == CellPosition.Last)
   column = _NumColumns - 1;
else
   column = 1;

Flatten Conditional

Unindents all or a portion of the conditional statement. This refactoring applies one of the following refactorings: Replace Nested Conditional with Guard Clause, Remove Redundant Else, or Reverse Conditional followed by Remove Redundant Else. Flatten conditional can also recognize “if (E) return true; else return false;” and convert all of this to simply “return E;”.

Here's one example for Flatten Conditional, where an indented code block (the last one of a method) becomes unindented by reversing the conditional and exiting the method:

Refactoring - Flatten Conditional

Here's another example preview hint for Flatten Conditional, where an else keyword and the corresponding braces are removed, unindenting the contents of the block:

Refactoring - Flatten Conditional

Reverse Conditional

Inverts the logic in this conditional statement and swaps the If and Else blocks.

Refactoring - Reverse Conditional

Split Conditional

Two behaviors:

  • Converts a conditional with a binary expression performing a logical AND operation into nested conditionals. For example, in C#, "if (e1 && e2)" becomes "if (e1) if (e2)".

    Before:

    [C#]

    if (fileName != null && fileName != String.Empty)
      PlayInCell(fileName, column, row);

    After:

    [C#]

    if (fileName != null)
      if (fileName != String.Empty)
        PlayInCell(fileName, column, row);
  • Converts a conditional with a binary expression performing a logical OR operation into neighboring conditionals.

    Before:

    [C#]

    if (fileName == null || fileName == String.Empty)
      return;

    After:

    [C#]

    if (fileName == null)
      return;
    if (fileName == String.Empty)
      return;
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.