26 September 2007

Precedence Operator

Kernighan & Pike in The Practice of Programming says ``Parenthesize to resolve ambiguity. Parentheses specify grouping and can be used to make the intent clear even when they are not required.'' p.6-7.

Many times the programmers use a few parenthesis to save code, however this is error-prone at operator precedence, a good concrete example I found was for Unpaws project made in Pascal, where the unary logic "not" operator has more precedence over the Set than the enumeration.

Fix diff patch:
< else if not (c in[6,13,16,17,18,19,20,21,22,23]) then inc(XPos);
> else if not c in[6,13,16,17,18,19,20,21,22,23] then inc(XPos);

Also from A Programmer's Guide to Java Certification, Second Edition says respect to unary operators: ``Expressions where variables are modified multiple times during the evaluation should be avoided, because the order of evaluation is not always immediately apparent.'' p.64

No comments :

My Blog List

Blog Archive


The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.