by _delirium · 16y ago · 0 comments
For example, this is idiomatic and readable C (ignoring whether it's a good idea to write this specific string-copy loop these days):
while (*dst++ = *src++);
whereas this is line noise that you have to decode: while ((*(dst++)) = (*(src++)));
The first form does require you to know that ++ binds tighter than the dereferencing operator, so the increment is incrementing the pointer, not the value it points to. But it's easier to scan, and used enough that people who code in C presumably can just glance at it. It makes a nice syntax pun due to the interaction of the order of operators and the postincrement, too. You can read it as: deref dst, then increment dst, which gives the nice intuition that the deref happens, and then the increment happens. The parenthesized version is more confusing on that point, and you'd probably just want to rewrite it into a more verbose loop with 3 separate body statements, if you were going to go that route.Heck, even Java does this regularly. Method-call dot-chaining is idiomatic in Java, even though it relies on an evaluation rule to work properly:
foo.bar().baz().qux()
Not idiomatic: ((foo.bar()).baz()).qux()
For the same two reasons basically. The fully parenthesized version is too noisy, and the unparenthesized version has a nice syntax-pun interpretation: it's a "chain" of results going down the list of methods.It's not just that it's too noisy. One wonders on reading it whether the parens are there to override the default precedence. I have the feeling that there's a trick going on in there and I have to look much harder to verify that it is normal.
Instead of adding parentheses, try writing it like this:
while(*dst)
{
*dst = *src;
dst++;
src++;
}
It's instantly more readably, it's very obvious what's going on, and you've lost no performance. There's also no parentheses to add.Plus, once you start piling on a bunch of such code-explosions, you end up with functions that are much harder to scan, because what could've been an easy to scan 8-line function is pedantically written out as this super-explicit 30-line thing.