...
Noncompliant Code Example
This noncompliant code example demonstrates an action being carried out in an assertion. The idea is to delete all the null
names from the list, however, the boolean
expression is not evaluated.
Code Block | ||
---|---|---|
| ||
void process(int index) { assert names.remove(null); /* side effect */ /* ... */ } |
...
Avoid the possibility of side effects in assertions. This can be achived by decoupling the boolean
from the assertion.
Code Block | ||
---|---|---|
| ||
void process(int index) { boolean nullsRemoved = names.remove(null); assert nullsRemoved; /* no side effect */ /* ... */ } |
...