Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#ffcccc
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
bgColor#ccccff
void process(int index) {
  boolean nullsRemoved = names.remove(null);
  assert nullsRemoved; /* no side effect */
  /* ... */
}

...