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: 
And here's an example where neighboring conditionals with identical bodies can be combined: 
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: 
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. 
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: 
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: 
Reverse Conditional
Inverts the logic in this conditional statement and swaps the If and Else blocks. 
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;
|