Re: while (1) vs. for ( ;; )
- From: Alan Balmer <albalmer@xxxxxxx>
- Date: Mon, 29 Aug 2005 15:59:01 -0700
On Mon, 29 Aug 2005 21:53:55 GMT, "Mark B" <sober@xxxxxxxxxxxx> wrote:
>"Alan Balmer" <albalmer@xxxxxxx> wrote in message
>news:n9s6h1993erv8hnkhkt9p6fv5osg99kp6o@xxxxxxxxxx
>> On Mon, 29 Aug 2005 18:35:12 +0000 (UTC), Richard Heathfield
>> <invalid@xxxxxxxxxxxxxxx> wrote:
>>>Alan Balmer said:
>>>> On Mon, 29 Aug 2005 08:42:47 +0000 (UTC), Richard Heathfield
>>>> <invalid@xxxxxxxxxxxxxxx> wrote:
>>>>>Personally, I prefer neither choice! I would rather have the loop
>>>>>control
>>>>>statement explicitly document the exit condition (unless there genuinely
>>>>>isn't one, such as might be the case in an electronic appliance like a
>>>>>microwave oven, where "forever" can roughly be translated as "whilst
>>>>>power
>>>>>is being supplied to the appliance").
>>>> It often happens that there are multiple exit conditions. Is it your
>>>> preference to set a "get out" flag for these situations?
>>>Yes: done = 1;
>> I've also used a flag with multiple values, when I need to know why
>> the loop was terminated.
>>>> That's my
>>>> preference, though I don't feel strongly about it (and I'm likely to
>>>> use multiple function returns, too, when a single return is too
>>>> contrived.)
>>>I'm quite unlikely to use multiple returns, although I seem to recall that
>>>I
>>>have done so on occasion, when in a tearing hurry.
>> The situation where I'm likely to use multiple returns is for sanity
>> checks at the beginning of a relatively long function;
>> if (something_strange_about_the_parameters)
>> return 0;
>> <do the real stuff>
>> return 1;
>
>I know it's only a trivial example... but why not:
>
>if(nothing_strange_about_the_parameters)
> status = call_the_real_function(passing_validated_parameters);
>return status;
>
Only because you then have to chase after call_the_real_function(). On
the initial writing, this would probably be only a few lines below,
but that could change. Hmm... Actually, the more I think about it, the
less I like it. Each function turns into two, and the validation's
connection to the rest of the code is more tenuous than necessary. You
could impose a convention, I suppose - every function foo() has a
corresponding function validate_foo(). Even then, you have two
functions which need to be synchronized, a bad maintenance situation.
All in all, I think I prefer the single function with two returns. I
just find it silly to have a couple of lines in an if, followed by a
hundred lines in the else, just to conform to the "single return"
rule.
>The 'relatively long functions' are where the bugs typically breed, no?
>
I suppose, but I miss the relevance.
--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@xxxxxxx
.
- Follow-Ups:
- Re: while (1) vs. for ( ;; )
- From: Richard Heathfield
- Re: while (1) vs. for ( ;; )
- References:
- while (1) vs. for ( ;; )
- From: Michael B Allen
- Re: while (1) vs. for ( ;; )
- From: Richard Heathfield
- Re: while (1) vs. for ( ;; )
- From: Alan Balmer
- Re: while (1) vs. for ( ;; )
- From: Richard Heathfield
- Re: while (1) vs. for ( ;; )
- From: Alan Balmer
- Re: while (1) vs. for ( ;; )
- From: Mark B
- while (1) vs. for ( ;; )
- Prev by Date: Re: UnSigned Long
- Next by Date: Re: while (1) vs. for ( ;; )
- Previous by thread: Re: while (1) vs. for ( ;; )
- Next by thread: Re: while (1) vs. for ( ;; )
- Index(es):
Relevant Pages
|
Loading