The assert()
statement is a convenient mechanism for incorporating diagnostic tests in code. Expressions used with the standard assert
statement must avoid side effects. Typically, the behavior of the assert
statement depends on the status of a runtime property. When enabled, the assert
statement is designed to evaluate its expression argument and throw an AssertionError
if the result of the expression is false
. When disabled, assert
is defined to be a no-operationop; any side effects resulting from evaluation of the expression in the assertion are lost when assertions are disabled. Consequently, programs must not use side-effecting expressions in assertions.
Noncompliant Code Example
This noncompliant code example demonstrates an action being carried out in an assertion. The idea is is attempting to delete all the null
names from the list ; howeverin an assertion. However, the boolean
expression is unexpectedly not evaluated when assertions are disabled.
...
Automated detection of assertion operands that contain locally - visible side effects is straightforward. Some analyses could require programmer assistance to determine which method invocations could contain lack side effects.
Related Guidelines
...