AP 29 - Use parentheses when mixing ANDs with ORs
Severity
CautionType
Readability
Problem
There are a few important rules about how operators and conditions precede each other. The most important rule for conditions is that AND comes before OR.
Don’t make complicated expressions that require the reader to know the rules perfectly. Use spaces and parentheses to make the logic clear.
SELECT c1
FROM table1
WHERE x=1
OR z=5
AND y=2
Solution
Use parentheses when mixing ANDs with ORs to improve readability
SELECT c1
FROM table1
WHERE x=1
OR(z=5
AND y=2)
Please note it’s very different to:
SELECT c1
FROM table1
WHERE( x=1
OR z=5
)
AND y=2
and in the above case the logic can’t be expressed without brackets.
Legitimate use of the anti pattern
None