Non-Short-Circuit Operator
ID |
java.non_short_circuit |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
code-style |
Description
Reports use of bitwise & or | operators between boolean expressions. Unlike && and ||, the bitwise operators evaluate both operands even when the result is already determined by the first operand.
Rationale
Using & instead of && (or | instead of ||) defeats short-circuit evaluation. This can lead to NullPointerException when the second operand depends on the first being true, and causes unnecessary side effects.
// Bad -- both sides always evaluated
if ((obj != null) & (obj.isValid())) { ... }
Remediation
Use the short-circuit operators && and ||.
if ((obj != null) && (obj.isValid())) { ... }