Versions Compared

Key

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

...

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 unexpectedly 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 achieved by decoupling the boolean expression from the assertion.

Code Block
bgColor#ccccff
void process(int index) {
  boolean nullsRemoved = names.remove(null);
  assert nullsRemoved; /* no side effect */
  /* ... */
}

...