Skip to main content

Reverse Boolean

  • 2 minutes to read

Reverses the logical meaning of a Boolean variable, and appropriately inverts all references and assignments to the variable to ensure program behavior remains unchanged. This refactoring is useful when you want to reverse the semantic meaning of a Boolean variable (e.g., changing “notFound” to “found”), and can clean up hard-to-read expressions (e.g., “!notFound” can become simply “found” in C#). After this refactoring completes, the Boolean variable is highlighted for an easy rename, so you can give the variable a new name that is the semantic opposite of the old name.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on a Boolean variable declaration.

#Examples

public MyClass()
{
  _DataNotFound = !FindData();
}
public void ProcessData()
{
  if (_DataNotFound)
    return;
  DataProcessor.OutputDataInfo(_Data);
}
private object _Data;
private bool _DataNotFound;
Public Sub New()
  _DataNotFound = Not FindData()
End Sub
Public Sub ProcessData()
  If _DataNotFound Then
    Return
  End If
  DataProcessor.OutputDataInfo(_Data)
End Sub
Private _Data As object
Private _DataNotFound As Boolean

Result:

public MyClass()
{
  _DataNotFound = FindData();
}
public void ProcessData()
{
  if (!_DataNotFound)
    return;
  DataProcessor.OutputDataInfo(_Data);
}
private object _Data;
private bool _DataNotFound;
Public Sub New()
  _DataNotFound = FindData()
End Sub
Public Sub ProcessData()
  If Not _DataNotFound Then
    Return
  End If
  DataProcessor.OutputDataInfo(_Data)
End Sub
Private _Data As object
Private _DataNotFound As Boolean

#Screenshot

CSharpReverseBoolean