Associativity

Associativity is one of those subjects that is poorly explained in most programming texts. In fact, a good way to judge a programming text for any language is to look for its explanation of associativity. Silence is not golden.

There are three factors that determine the ultimate value of an expression in any algorithmic language, and they work in this order: precedence, associativity, and order of evaluation.

Precedence says that some operations bind more tightly than others. Precedence tells us that the multiplication in a + b * c will be done before the addition, i.e., we have a + (b * c) rather than (a + b) * c. Precedence tells us how to bind operands in an expression that contains different operators.

Associativity is the tie breaker for deciding the binding when we have several operators of equal precedence strung together. If we have 3 * 5 % 3, should we evaluate it as (3 * 5) % 3 or as 3 * (5 % 3) ?

The first is 15 % 3, which is 0. The second is 3 * 2, which is 6! Multiplication and the “%” remainder operation have the same precedence, so precedence does not give the answer. But both operators *, % are left-associative, meaning when you have a bunch of them strung together you start associating operators with operands from the left. Push the result back as a new operand, and continue until the expression is evaluated. In this case, (3 * 5) % 3 is the correct grouping.

There is no extra charge for parentheses. Use parentheses generously in expressions, so that future programmers (and the compiler) can avoid imposing its own view about how you wanted the operands grouped.

Associativity is a terrible name for the process of deciding which operands belong with which operators of equal precedence. A more meaningful description would be, Code Order For Finding/Evaluating Equal Precedence Operator Textstrings.” This is the “COFFEEPOT property” mentioned in Table 7-3.

Note that associativity deals solely with deciding which operands go with which of a sequence of adjacent operators of equal precedence. It doesn't say anything about the order in which those operands are evaluated.

Order of evaluation, if it is specified in a language, tells us the sequence for each operator in which the operands are evaluated. In a strict left-to-right language like Java, the order of evaluation tells us that in (i=2) * i++, the left operand to the multiplication will be evaluated before the right operand, then the multiplication will be done, yielding a result of 4, with i set to 3. Why isn't the auto-increment done before the multiplication? It has a higher precedence after all. The reason is because it is a post increment, and so by definition the operation is not done until the operand has been used.

In C and C++, this expression is undefined because it modifies the same value more than once within an expression. It is legal in Java because the order of evaluation is well defined.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.233.153