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

That's why I always use parentheses, i.e. I never rely on operator precedence. Therein lie troubles, lad.

And, as usual, the xkcd is fantastic.




You don't write 1+2+3, but (1+2)+3?


The same operators don't need precedence rules; each language is processed left-to-right or vice versa. Usually it's left-to-right, in my experience with maybe a dozen languages professionally, maybe twice that in exploration.


With the same operator, the rule is not called precedence, but associativity (left or right; consider 2^3^4 where it matters), but you still need a rule - unless you use parentheses, which you said you always do.


Touche!

If there's a case where one computation needs to occur before others, then that's where I always use parens.

I'm also never doing a^b^c, either as a^(b^c) or (a^b)^c, but if I did, there would be parens.

Readibility in one's code is necessary for those of us who must re-read our code when we need to refamiliarize ourselves with it before making changes. It is said that programmers spend far more time reading code than writing it, and I have found that to be true. As someone who has developed large pieces of software, my strategies include clarity for the reader, who is, first of all, myself.

As to what it's called, yeah, 'associativity' is the math term, but if you think I don't understand the concept, then you don't understand the depth that precedence goes to the heart of parsing source code to produce executable code. And I do understand that, friend, for four decades now.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: