Comparison with a boolean constant (CompareWithBoolean)¶
| Type | Scope | Severity | Activated by default |
Minutes to fix |
Tags |
|---|---|---|---|---|---|
Code smell |
BSLOS |
Minor |
Yes |
1 |
clumsysuspicious |
Description¶
Comparing an expression with the boolean constant True or False using the = and <> operators is redundant
and reduces code readability. If the expression already has the Boolean type, it is enough to use it directly
(or with the NOT operator).
Moreover, such a construction can hide a design error: if for some reason the type of the expression differs from
Boolean, the code branch may be executed incorrectly.
Examples¶
Bad:
If Value = True Then
// ...
EndIf;
If Value <> False Then
// ...
EndIf;
Good:
If Value Then
// ...
EndIf;
If NOT Value Then
// ...
EndIf;
Exceptions¶
The diagnostic is triggered only if the type of the second operand can be inferred and
it is unambiguously Boolean. If the type cannot be determined or is a union of
types, no issue is reported — in such cases comparison with a boolean constant may be
justified.
For example, SafeMode() returns a value of type Boolean | String, so comparison with
False is correct here and no issue is reported:
If SafeMode() <> False Then
// ...
EndIf;
Sources¶
Snippets¶
Diagnostic ignorance in code¶
// BSLLS:CompareWithBoolean-off
// BSLLS:CompareWithBoolean-on
Parameter for config¶
"CompareWithBoolean": false