Re: C
Re: C
- Subject: Re: C
- From: Clark Cox <email@hidden>
- Date: Sun, 31 Aug 2003 01:31:23 -0400
On Sunday, August 31, 2003, at 00:19, Hsu wrote:
On Saturday, August 30, 2003, at 08:13 PM, Clark Cox wrote:
If your expressions are long enough that someone would need help
understanding them, then you should try writing shorter, more concise
expressions. So, you would never write:
z = x + y * w;
You would instead write this?:
(z = (x + (y * w)));
I would probably write
z = x + (y * w);
But, playing devil's advocate, what if you don't know that addition
has a higher precedence than assignment?
but if forced at gunpoint, I'd still choose the second. If I had to
scan the code looking for a bug 5 years later, the latter is going to
make it much easier to know what was intended rather than just what
was written.
Big fan of the Obfuscated C contest?
Not particularly. :) I tend to like well-commented code whose function
is obvious to any conversant in the language.
Again, if parentheses are needed to make an expression readable,
then the expression is either too complex to begin with, or the
person reading it doesn't know the language (and I count knowing at
least a summary of the precedence rules as part of "knowing" the
language).
Just as knowing how to read algebraic equations implies knowing the
rules of precedence for mathematical operations (i.e. there is
nothing ambigous about "y = 2x^2 + 3", we know that the exponent
comes first, then the multiplication, and then the addition), knowing
how to read/write C (or C++ or Java, etc.) implies knowing the rules
of precedence for it's operations.
It's a question of intention. If you write 'x+y*w' I know how it will
evaluate, but I'm not sure what's intended. 'x+(y*w)' or '(x+y)*z'
make the intention obvious.
When I see extra parentheses, it makes me think "gee, this person
wanted something other than the default behavior". But then, when I see
that they actually wanted the default behavior, it usually makes me
think that they missed something, I then have to scour the calling code
to see if that's what they *really* wanted. If, on the other hand, the
code is written with the language's rules in mind, I know that
parentheses mean "I've changed the order for some reason".
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
- Follow-Ups:
- Re: C
- From: Jeff <email@hidden>
References: | |
| >Re: C (From: Hsu <email@hidden>) |