Versions Compared

Key

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

According to the Java Language Specification Section 15§15.7, "Evaluation Order"

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

JLS Section 15§15.7.3 "Evaluation Respects Parentheses and Precedence" of the Java Language Specification adds:

Java programming language implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence.

These two requirements can be counter-intuitive when expressions contain side-effects. Evaluation of the operands proceeds left-to-right, without regard to operator precedence rules and indicative parentheses; evaluation of the operators, however, obeys precedence rules and parentheses.

It is recommended that any expression Expressions should never write to memory that it they subsequently readsread, and it they should never write to any memory twice. Memory writing and reading can occur directly in the expression from assignments or indirectly through side - effects in functions called in the expression.

...

This noncompliant code example shows how side - effects in expressions can lead to unanticipated outcomes. The programmer intends to write access control logic based on different threshold levels. Each user has a rating that must be above the threshold to be granted access. As shown, a simple function can calculate the rating. The get() method is expected to return a non-zero factor for users who are authorized, and a zero value for those who are unauthorized.

...

This noncompliant code example reorders the previous expression so that the left-to-right evaluation order of the operands corresponds with the programmer's intent.

Although this code performs as expected, it still represents poor practice by writing to number three times in a single expression.

Code Block
bgColor#ccccff
number = ((31 * ++number) * (number=get())) + (number > threshold[0]? 0 : -2);

Although this code performs as expected, it still represents poor practice, by writing to number three times in a single expression.

Compliant Solution (

...

Order of

...

Evaluation)

This compliant solution uses equivalent code with no side-effects. It performs only one write, to number. The resulting expression can be reordered without concern for the evaluation order of the component expressions, making the code easier to understand and maintain.

Code Block
bgColor#ccccff
final int authnum = get();
number = ((31 * (number + 1)) * authnum) + (authnum > threshold[0]? 0 : -2);

Exceptions

EXP08-EX0EX1: The postfix increment and postfix decrement operators (++ and --) assign a new value to a variable and then subsequently read it. These are well-understood and are an exception to the rule against reading memory that was written in the same expression.

EXP08-EX1EX2: The logical operators || and && have well-understood short-circuit semantics, and so expressions involving these operators may violate this rule. Consider the following code:

Code Block
bgColor#ccccff
InputStream in;
int i;
// Skip one char, process next
while ((i = in.read()) != -1 && (i = in.read()) != -1) {
  // ...
}

This code is compliant, because while Although the overall conditional expression violates the rule, this guideline, this code is compliant because the sub-expressions on either side of the && operator do not violate it. Each has exactly one assignment and one side - effect (the reading of a character from in).

...

Detection of all expressions involving both side-effects and also multiple operator precedence levels is straightforward. Determining the correctness of such uses is infeasible in the general case; heuristic warnings could be useful.

Related

...

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Related Guidelines

C Coding Standard: "EXP30-C. Do not depend on order of evaluation between sequence points"

C++ Coding Standard: "EXP30-CPP. Do not depend on order of evaluation between sequence points"

Bibliography

Wiki Markup
\[[JLS 2005|AA. Bibliography#JLS 05]\] [Section 15§15.7|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7] "Evaluation Order" and [15§15.7.3|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7.3] "Evaluation Respects Parentheses and Precedence"

...