Re: if statements vs. switch
Re: if statements vs. switch
- Subject: Re: if statements vs. switch
- From: ber <email@hidden>
- Date: Wed, 4 Sep 2002 12:57:06 -0400
Considering a conditional expression is wise. As Ondra implies, using
it is a matter of preference.
If your variables have short names, conditional expression seem to me
more appropriate then
if they are typical obj-c names. So:
flag = (x > limit ? 1 : 0);
in my opinion is clear while being succinct but:
[myWindow setFrameTopLeftPoint:[prefs
integerForKey:@"ControlsDisplaySide]==0?top:bottom];
is too verbose to benefit from a conditional expression.
Over the years I've found that a significant percentage of compiler
optimization bugs center around
conditional expressions. For that reason and for clarity I avoid
conditional expressions in general.
regarding you question about the syntax... From Kernighan&Ritchie's
The C Programming Language
(pick up a copy):
In the expression,
e1 ? e2 : e3
the expression e1 is evaluated first. If it is non-zero (true), then
the expression e2 is
evaluated, and that is the value of the conditional expression.
Otherwise e3 is evaluated,
and that is the value. Only one of e2 and e3 is evaluated. Thus to
set z to the maximum
of a and b,
z = (a > b) ? a : b; /* z = max(a,b) */
brian
On Wednesday, September 4, 2002, at 12:08 PM, Jeremy Dronfield wrote:
On Wednesday, September 4, 2002, at 04:01 pm, Ondra Cada wrote:
Note also that you should consider a conditional expression, too.
With four possibilities it would not be the best solution, but with
two (sometimes even three) of them I would prefer it myself:
[myWindow setFrameTopLeftPoint:[prefs
integerForKey:@"ControlsDisplaySide"
]==0?top:bottom];
_______________________________________________
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.