...
Code Block | ||
---|---|---|
| ||
public void exampleFunctionf(boolean a, boolean b) { if (a = b) { /* ... */ } } |
Although the intent of the code could be to assign b
to a
and test the value of the result, it is frequently a case of the programmer mistakenly using the assignment operator =
instead of the equals operator ==
.
...
Code Block | ||
---|---|---|
| ||
public void exampleFunctionf(boolean a, boolean b) { if (a == b) { /* ... */ } } |
When the assignment is intended, the following compliant solution may be used because the programmer's intent is clearer:
Code Block | ||
---|---|---|
| ||
public void exampleFunctionf(boolean a, boolean b) { if ((a = b) == true) { /* ... */ } } |
But it might be preferable to express this same logic as an assignment followed by a conditional:
Code Block | ||
---|---|---|
| ||
public void exampleFunctionf(boolean a, boolean b) { a = b; if (a) { /* ... */ } } |
Risk Assessment
Errors of omission can result in unintended program flow.
...
EXP03-J. Do not use the equality operators when comparing values of boxed primitives 02. Expressions (EXP) EXP05-J. Do not write more than once to the same variable within an expression