Re: String concatenation design
- From: "Arthur J. O'Dwyer" <ajonospam@xxxxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 13:11:23 -0500 (EST)
On Thu, 30 Mar 2006, Rob Thorpe wrote:
andy@xxxxxxxxxxxxxxxxxxxxxxxxx wrote:Rob Thorpe wrote:
Don't do what C, C++ and Perl do, and have 15 levels with slightly
different priorities. These languages are popular but their precendence
levels aren't.
My compiler has 15 levels of precedence with /vastly/ different priorities. I think that's a great improvement over the standard,
slightly different priorities. ;)
What evidence is there to support your claim that the precedence levels
are unpopular?
From the Indhill style guide:"Since C has some unexpected precedence rules, expressions involving
mixed operators should be parenthesized." & later "There is a time and
place for the binary comma operator, but generally it should be
avoided."
So should the ternary operator, generally. I don't think the advice
to avoid the comma operator is relevant.
The GNU style guide is milder:
"Instead, use extra parentheses so that the indentation [of
expressions] shows the nesting:"
Plus I've never read a program of any length that used many of the
precedence levels at once. And most programs I've read seems to ignore
many of them, often relying on * . -> + - * / && || = == only.
Um... how many more levels are there? I'll grant that <= versus ==
is basically never used, but other than that, you really /need/ to have
Postfix tighter than prefix, for -f[x]
Unary tighter than binary, for j + f[x]
Exponentiation tighter than multiplication, if relevant (not in C)
* and / tighter than + and -
All the above tighter than == != < > <= >=
All the above tighter than the ternary operator, if relevant (in C)
All the above tighter than the comma operator, if relevant (in C)
Relational operators tighter than &&
&& tighter than ||
& and ^ tighter than |
That's most of C's rules right there. C splits == != away from <= < >= >
for no obvious reason, messes up the precedence levels of & and |
versus && and || for historical reasons, and needs to find somewhere
fairly arbitrary to stick << and >>; but that's pretty much it.
I think what the style manuals are complaining about isn't the idea of
precedence levels as such, but rather the writing of really complicated
expressions that require the user to consciously /think/ about precedence
levels. If the language had fewer built-in precedence levels, it wouldn't correspond to mathematical notation as closely, and programmers would
have to think harder about precedence. That would be a bad thing.
Think about this: can you remember what they all are? For C and C++?
Yes, except for where the bitwise ops and << >> fit in. But I don't
need to remember, because it works just like the math we all learned
in second grade. As long as nobody uses more than five or six operators
per expression, C's easy for a human to parse.
-Arthur
.
- References:
- String concatenation design
- From: Anton Treuenfels
- Re: String concatenation design
- From: Rob Thorpe
- Re: String concatenation design
- From: Oliver Wong
- Re: String concatenation design
- From: Rob Thorpe
- Re: String concatenation design
- From: andy
- Re: String concatenation design
- From: Rob Thorpe
- String concatenation design
- Prev by Date: Re: String concatenation design
- Next by Date: Re: ALgorithm Solution
- Previous by thread: Re: String concatenation design
- Next by thread: how to print all the combinations ??
- Index(es):