Combine Conditionals

Combines two or more neighboring conditionals with identical bodies into a single conditional statement, where each conditional expression is logically OR'd.

Purpose

This refactoring helps you to eliminate code duplication.

Availability

Available from the context menus or via shortcuts:

  • when the caret is on an if keyword, provided that the current method has another if statement with the same body.

Example

if (productAExists)
    return GetOrders();
if (productBExists)
    return GetOrders();
If productAExists Then
    Return GetOrders()
End If

If productBExists Then
    Return GetOrders()
End If

Result:

if (productAExists || productBExists)
    return GetOrders();
If productAExists OrElse productBExists Then
    Return GetOrders()
End If

Screenshot

CombineConditionals

See Also