From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3b4bed4f74b8ac49 X-Google-Attributes: gid103376,public From: dewar@merv.cs.nyu.edu (Robert Dewar) Subject: Re: GNAT messages and the not operator (pitfall alert!) Date: 1996/10/26 Message-ID: #1/1 X-Deja-AN: 192243017 references: <54snn6$8j5@newsbf02.news.aol.com> organization: New York University newsgroups: comp.lang.ada Date: 1996-10-26T00:00:00+00:00 List-Id: iJohn Herro says "should be cluttered with unnecessary parentheses. Everyone knows that "**" comes before "*" and "/", which come before binary "+" and "-", and everyone knows that the relational operators have a low precedence. It's also arguable that "everyone" knows that "and" comes before "or". But I'd say in almost every other case, unless the order of evaluation is very obvious, use parentheses!" Well I am never sure what "everyone knows", but if everyone using Ada knows that "and" comes before "or", they are mistaken! The syntactic rules of Ada REQUIRE parenthesization for mixed logical operators. I guess John never made this argument in his own Ada code, which is why he was unaware of this rule (which is an old Ada 83 rule). As for always using parens, yes, everyone agrees with this rule (after all there are very few levels of precendece in Ada anyway, John's rule is more the kind of rule that is useful in a C environment than Ada anyway). The trouble is that one of the other things that "everyone knows" is that unary operators have higher precedence than binary operators, because this is true in every other language than Ada as far as I know, and -a mod b reads wrong naturally to almost everyone. It is a bad mistake in a language to violate these rules that "everyone knows". In Ada it happened from a kind of accidental by product of keeping the number of precedence levels low. In fact it is VERY rarely a problem, because it is only mod that has a problem (for every other binary operator, the minus distributes in any case, so the precedence is unimportant), and taking mod of negative numbers is rare, and using an explicit minus to negate the left operand even rarer. I well remember another case like this in SNOBOL4. In an attempt to duplicate normal mathematical notation more closely, where division is represented by a low precedence horizontal line, Ralph Griswold decided to make division have lower precedence than multiplication, so that A*B / C*D means what it looks like. Well I remember once spending quite a bit of time puzzling over why SPITBOL was generating the "wrong code", when in fact it was completely right, just following this unusual rule. I wonder if anyone ever suggested using spacing to wonder about precedence (I am not suggesting it, it is not a good idea, however, it gives me an idea, if a programmer uses spacing, as in the above example of A*B / C*D in a manner that is clearly at odds with the precedence, then perhaps a warning is a good idea?)