Why is this an issue?
A boolean literal can be represented in two different ways: True
or False
. They can be combined with logical operators
(Not, And, Or, =
) to produce logical expressions that represent truth values. However, comparing a boolean literal to a variable or
expression that evaluates to a boolean value is unnecessary and can make the code harder to read and understand. The more complex a boolean expression
is, the harder it will be for developers to understand its meaning and expected behavior, and it will favour the introduction of new bugs.
How to tix it
Remove redundant boolean literals from expressions to improve readability and make the code more maintainable.
Code examples
Noncompliant code example
If BooleanMethod() = True Then ' Noncompliant
' ...
End If
If BooleanMethod() = False Then ' Noncompliant
' ...
End If
If BooleanMethod() OrElse False Then ' Noncompliant
' ...
End If
DoSomething(Not False) ' Noncompliant
DoSomething(BooleanMethod() = True) ' Noncompliant
Dim booleanVariable = If(BooleanMethod(), True, False) ' Noncompliant
booleanVariable = If(BooleanMethod(), True, exp) ' Noncompliant
booleanVariable = If(BooleanMethod(), False, exp) ' Noncompliant
booleanVariable = If(BooleanMethod(), exp, True) ' Noncompliant
booleanVariable = If(BooleanMethod(), exp, False) ' Noncompliant
Compliant solution
If BooleanMethod() Then
' ...
End If
If Not BooleanMethod() Then
' ...
End If
If BooleanMethod() Then
' ...
End If
DoSomething(True)
DoSomething(BooleanMethod())
Dim booleanVariable = BooleanMethod()
booleanVariable = BooleanMethod() OrElse exp
booleanVariable = Not BooleanMethod() AndAlso exp
booleanVariable = Not BooleanMethod() OrElse exp
booleanVariable = BooleanMethod() AndAlso exp