News for hackers. Stuff that matters.
Re:Tim Bray doesn't know operator precedence rules by _delirium on Wednesday June 30, 2010 @06:16AM (#1474376)
I mostly do this, especially for mathematical expressions, but in some cases languages have developed idioms where the common and readable style is to leave out some parens. It's dumb to do it just as some kind language-lawyering way to save chars, and it does raise the amount of background knowledge about the language you need, but putting parens everywhere can really break readability as well.

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.
Re:Tim Bray doesn't know operator precedence rules by rlpb on Wednesday June 30, 2010 @08:07AM (#1474568)
> The fully parenthesized version is too noisy...

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.

Re:Tim Bray doesn't know operator precedence rules by CountHackulus on Wednesday June 30, 2010 @05:05PM (#1476235)
I'd just like to point out that in your string example, you gain nothing by keeping that string copy short. The compiler doesn't see lines, all it sees are tokens, and what you've written there is fine for a compiler to read, but difficult for a human.

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.
Re:Tim Bray doesn't know operator precedence rules by _delirium on Wednesday June 30, 2010 @05:18PM (#1476277)
I actually find that style considerably less readable, though admittedly I'm quite familiar with C code. It's just less common for one (not the idiomatic way of writing code in the language), but it also emphasizes this tedious loop-iteration-at-a-time interpretation, while the original is easy to read as a sort of Matlab-style single vector operation, once you're used to it ("copy all the srcs to all the dsts").

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.

Re:Tim Bray doesn't know operator precedence rules by scott_s on Wednesday June 30, 2010 @05:27PM (#1476312)
It's idomatic C. It's difficult for unexperienced C coders to read, but easy for experienced C coders who have read lots of other people's code.
Re:Tim Bray doesn't know operator precedence rules by edanm on Wednesday June 30, 2010 @07:04AM (#1474457)
Agreed. I think the rule is to understand what is obvious and readable and what is not. Doing "var++" is very readable, as are method calls, specifically because they're designed to be used in this way.