Boolean Comparison With Literal
ID |
java.boolean_compare_with_literal |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
code-style, suspicious-comparison |
Rationale
Comparing a boolean expression with a boolean literal is redundant. if (flag == true) is exactly equivalent to if (flag), and if (flag == false) is equivalent to if (!flag). The literal comparison adds noise without adding information and can confuse readers into thinking a non-boolean type is involved.
// Bad -- redundant comparison
if (flag == true) { ... }
if (flag == false) { ... }
if (true == flag) { ... }
Remediation
Use the boolean expression directly.
// Good -- direct boolean expression
if (flag) { ... }
if (!flag) { ... }