Name

STYL-07: Express complex expressions unambiguously using parentheses.

Synopsis

The rules of operator precedence follow the commonly accepted precedence of algebraic operators. The strong typing approach of PL/SQL,[1] combined with the common precedence rules, make many parentheses unnecessary. When an uncommon combination of operators occurs, however, it may be helpful to add parentheses even when the precedence rules apply.

The rules of evaluation do specify left-to-right evaluation for operators that have the same precedence level. However, this is the most commonly overlooked rule of evaluation when checking expressions for correctness.

Many developers apply a consistent rule for improved readability in this area: always use parentheses around every Boolean expression, including IF, ELSIF, and WHILE statements, as well as variable assignments, regardless of the simplicity of the expressions. So, rather than:

IF cust_rec.min_balance < 1000 THEN ...

you instead write:

IF ( cust_rec.min_balance < 1000 ) THEN ...

Example

You might not want a standard that requires you to always use parentheses, but in some situations, parentheses are all but required for readability. Consider the following expression:

5 + Y**3 MOD 10

The PL/SQL compiler will not be the least bit confused by this statement; it will apply its unambiguous rules and come up with an answer. Developers, however, may not have such an easy time of it. You are better off writing that same line of code as follows:

5 + ((Y ** ...

Get Oracle PL/SQL Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.