Tim Rentsch said:
I believe I understand what you're saying; to make sure let me
pose a question. Boiled down, is what you're saying anything
more than "people who share my sense of program style will find
the version with parentheses more clear"? If that's all you're
saying, of course I wouldn't argue with that. If you mean to say
more than that, that didn't come out (at least not to me) in
your message - can you explain further what it is?
Hmm, good question. I suppose ultimately it's really just a matter of
personal preference. Nevertheless, I think I'll try to justify it (I
don't expect this to convince anyone).
Consider these two expression statements:
var1 = var2 = some_value;
var1 = var2 == some_value;
Both have exactly the same precedence hierarchy; if you built a parse
tree from each (I won't take the time to draw it), it would look
identical except for the use of "=" in one and "==" in the other.
But the first is an example of a fairly common pattern, a chained
assignment. Even though it's *really" an assignment to var1,
whose RHS happens to be an assignment expression, it can reasonably
be thought of as something more linear than that. It could be
extended to:
var1 = var2 = var3 = var4 = var5 = some_value;
(though I'd certainly question the need for something like that).
It's similar in that respect to an if/else chain; we write:
if (cond1) {
stmt1;
}
else if (cond2) {
stmt2;
}
else if (cond3) {
stmt3;
}
else {
stmt4;
}
rather than
if (cond1) {
stmt1;
}
else {
if (cond2) {
stmt2;
}
else {
if (cond3) {
stmt3;
}
else {
stmt4;
}
}
}
The latter corresponds more closely to the parse tree; the former
corresponds to the more linear way we think about it. (Of course we
could have arbitrarily complex trees of nested if/else statements,
but the linear form is quite common.)
On the other hand, in the second statement, though it has the same
structure, var1 and var2 play very different roles. var2 is compared
against some_value, and the result is assigned to var1. (Yes, I'm
stating the obvious.) It doesn't have the same linear structure
that a chained assignment has. We *could* write
var1 = var2 == var3 == var4 == var5 == some_value;
but it's hardly likely that it would make sense (and if it did,
I'd definitely want to add some parentheses).
In my personal opinion, adding superfluous parentheses:
var1 = (var2 == some_value);
both emphasizes the structure of the expression and makes it stand out
more clearly versus another common idiom:
var1 = var2 = some_value;
that otherwise looks very similar.
But yes, I'm pretty much saying that "people who share my sense of
program style will find the version with parentheses more clear".
}