Versions Compared

Key

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

Wiki Markup
Programmers frequently make errors regarding the precedence of operators because of the unintuitive low-precedence levels of {{&}}, {{\|}}, {{\^}}, {{<<}}, and {{>>}}. Avoid mistakes regarding precedence through the suitable use of parentheses. This also improves code readability, unless taken to excess. The precedence of operations by the order of the subclauses are defined in the Java Tutorials \[[Tutorials 2008|AA. Bibliography#Tutorials 08]\].

Although the guideline it advises against depending on parentheses for specifying evaluation order, guideline "EXP08-J. Do not write more than once to the same variable within an expression advises against depending on parentheses for specifying evaluation order, it " applies only to expressions that contain side - effects.

Noncompliant Code Example

...

Note that this solution performs bitwise operations on signed integers. Care must be exercised when doing this; see guideline "void NUM05-J. Avoid incorrect mixing of signed integers with bitwise operators" for more information.

Exceptions

EXP00EXP06-EX0EX1: Parentheses may be omitted from mathematical expressions that follows follow the algebraic precedence rules. For instance, consider the expression:

...

Detection of all expressions using low-precedence operators without parentheses 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: "EXP00-C. Use parentheses for precedence of operation"

C++ Secure Coding Standard: "EXP00-CPP. Use parentheses for precedence of operation"

Bibliography

Wiki Markup
\[[ESA 2005|AA. Bibliography#ESA 05]\] Rule 65: Use parentheses to explicitly indicate the order of execution of numerical operators
\[[Tutorials 2008|AA. Bibliography#Tutorials 08]\] [Expressions, Statements, and Blocks|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html], [Operators|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html]

...