WIthout brackets, this statement is ambiguous:if not invincible and hp <= 0
Viewing post in Work in progress: CoffeeScript in ct.js! comments
It isn’t ambiguous. Operators have an order of precedence in all programming languages, and they are almost always the same as they all follow mathematical notation’s conventions.
The operators in your example, in order:
[<=
, not
, and
].
That is the order in which those operators will always be evaluated. If you wanted to change thar order, you have to use parentheses. So to make and
evaluate before not
, you need to write it like:
if not (invincible and hp <=0).
EDIT: This reference sheet.
Of course you could always add parentheses for clarity, which in some cases is a good idea, althought most times it’s best to just split the expression into multiple expressions and take advantage of naming variable.
But for simple logic expressions like the one you mentioned, this is just something programmers are expected to just learn early on.
But for simple logic expressions like the one you mentioned, this is just something programmers are expected to just learn early on.
Or they simply don’t question it at all. Of course, when in doubt, you would use parentheses, but otherwise, statements in CoffeeScript follow the logic of regular speech. if not invincible and hp <= 0
reads naturally and evaluates in an intuitive, expected manner.