Dennis Ritchie on the priorities of && || vs. == etc. (1982)
1–10 of 178 posts
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#2Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#3I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#4Besides `not` (which often has different precedence depending on whether it's a keyword or a punctuator, and is a great reminder that multiple levels of pratt parsing is meaningful), `await` is the one with the most variation - in most languages, it binds tighter than binary operators, whereas in C++ it's just barely tighter than assignment.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#5Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#6I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Consider (&& a b) and (|| a b) for no uncertainty over operator precedence and easy variadic representation (&& a b c) at the cost of zero additional parentheses.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#7I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Consider (&& a b) and (|| a b) for no uncertainty over operator precedence and easy variadic representation (&& a b c) at the cost of zero additional parentheses.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#8I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
a && b is just a * b
a || b is just a + b
Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#9However, the precedence of & vs &&, or & vs ||, etc is a source of problems.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#10I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).