Re: while (1) vs. for ( ;; )
- From: "Charlie Gordon" <news@xxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 09:48:03 +0200
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:ln8xyir8ex.fsf@xxxxxxxxxxxxxxxxxx
> "Charlie Gordon" <news@xxxxxxxxxxx> writes:
> > "Keith Thompson" <kst-u@xxxxxxx> wrote in message
> > news:lnu0h7qh5r.fsf@xxxxxxxxxxxxxxxxxx
> >> Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx> writes:
> >> > pete <pfiland@xxxxxxxxxxxxxx> writes:
> >>
> >> I won't try to speak for pete, but since "while (1)" and "for (;;)"
> >> are semantically identical, I'd be very surprised if there were any
> >> difference in defect rate. It's something that seems so obvious to me
> >> that I wouldn't bother trying to measure it without a very good
> >> reason. If there were a difference, I'd tend to assume that it's a
> >> difference in training (perhaps the books or classes that use one form
> >> happen, by coincidence to be better than the ones that use the other
> >> form). Do you have some reason to think there's a significant
> >> difference?
> >
> > Assume the programmer always uses while(1) { ... } for his endless
> > loops. You quickly get used to interpreting these automatically as
> > you read them, without paying much attention. What if there are
> > some while(l) { ... } loops as well. Will you always keep a sharp
> > enough eye to catch these correctly ?
>
> Any code that declares a variable called "l" has worse problems than
> while() vs. for().
I agree completely, as I explain it in the very same post.
> In well-written code, something that looks like "while (1)" *is*
> "while (1)"; I don't have to waste my time distinguishing between '1'
> and 'l'.
There isn't so much "well-written" code out there ;-)
Well-written code definitely has to adhere to strict Coding Rules.
Among these is a golden one: consistency. This means in particular that simple
constructs should be implemented the same way everywhere. Coding Rules should
define the "standard" way to implement indefinite loops, be it for(;;),
while(1), while(true) FOREVER... and the programmer is required to use whatever
was decided.
I personnally read a lot of code, well-written and not: in my experience,
something that looks like a while (1) can sometimes be a while (l), so I am
extra careful with these, while (1) makes me suspicious. It is a matter of
style, but also of maturity: I have oserved that skilled experienced programmers
tend to use for (;;) more than while (1), whereas beginners will think of
while(! done) and java natives will prefer while (true). while (1) comes a
distant second in this list. This is a statistical approach, one of many that
help figuring the skill level of the programmer when code proofing: I don't look
for the same kind of bugs in beginner's code and in expert's code. And there
will be bugs: C has pitfalls for all levels.
Chqrlie.
.
- References:
- while (1) vs. for ( ;; )
- From: Michael B Allen
- Re: while (1) vs. for ( ;; )
- From: Tim Rentsch
- Re: while (1) vs. for ( ;; )
- From: pete
- Re: while (1) vs. for ( ;; )
- From: Tim Rentsch
- Re: while (1) vs. for ( ;; )
- From: Keith Thompson
- Re: while (1) vs. for ( ;; )
- From: Charlie Gordon
- Re: while (1) vs. for ( ;; )
- From: Keith Thompson
- while (1) vs. for ( ;; )
- Prev by Date: Re: low-level question
- Next by Date: Re: while (1) vs. for ( ;; )
- Previous by thread: Re: while (1) vs. for ( ;; )
- Next by thread: where the Turboc C c function?
- Index(es):