Hacker News new | past | comments | ask | show | jobs | submit login

Oh yeah? What is the correct interpretation of

    *a.b
Is it

    (*a).b
Or

    *(a.b)
Your heuristic says it should be the former. Is it? What about this one:

    *a->b



The mental precedence list I have is:

* Name operators (e.g., a::b, a.b, a->b)

* a[b], a(b) expressions

* Unary suffix operators (C doesn't have these, but Rust's ? operator applies)

* Unary prefix operators

* Arithmetic operators, following normal mathematical precedence rules (i.e., a + b / c is a + (b / c), not (a + b) / c). Note that I don't have any mental model of how <<, &, |, ^ compare to each other or the normal {,/,%}; {+,-} rank.

Comparison operators

* Short-circuit operators (&&, ||)

* Ternary operator (?:)--and this one is right-associative.

* Assignment operators

This list I think is fairly objective, although C and some of its children "erroneously" place bitwise {&,|,^} below comparison operators instead of above them. The difference between suffix and prefix unary operators is somewhat debatable, but it actually does make sense if you think of array access and function call expressions as unary suffix operators instead of binary operators.


Those show why I called it a heuristic. With some code similar to yours, I would probably use parentheses unless they are frequent enough to deserve some brain cells.


I certainly always always always used parentheses in situations like these back in the day. More because it was the idiom I learned early, rather than a conscious decision to be risk averse or professional.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: