Re: while (1) vs. for ( ;; )
- From: Alan Balmer <albalmer@xxxxxxx>
- Date: Thu, 01 Sep 2005 10:08:14 -0700
On Wed, 31 Aug 2005 21:12:25 -0700, "Baxter"
<lbax02.spamguard@xxxxxxxxxxx> wrote:
>"Keith Thompson" <kst-u@xxxxxxx> wrote in message
>news:ln4q96r875.fsf@xxxxxxxxxxxxxxxxxx
>> "Baxter" <lbax02.spamguard@xxxxxxxxxxx> writes:
>> [...]
>> > Why not code it like you would read it in English:
>> >
>> > bool done = false;
>> > while (!done) { // while not done do
>> > ...
>> >
>> > bool mainloop = true;
>> > while (mainloop) { // while mainloop do
>> > ...
>>
>> Because clear English is not necessarily clear C.
>
>It's not your job to write to the machine or to the compiler. Your job is
>to write to the next guy (probably yourself) who will be reading the code.
>
Do you know any C programmer who would have difficulty reading this
code? Would you?
>>
>> You've declared "done" and "mainloop" as variables, which means their
>> values could change.
>
>Yep.
>
>>With "while (1)" (or "for (;;)") I can see at a
>> glance that the loop is an infinite one;
>
>Infinity is a very long time. Your program will absolutely stop before
>then.
You are unfamiliar with the term "infinite loop"?
>
>>with your suggestion, I can
>> never be sure unless I analyze the program and verify that "done" will
>> always be false (and then I'll wonder why the heck you used a
>> variable).
>
>You could also use a #define -- while (PROGRAM_IS_RUNNING) {
>
Why? Since we are indeed writing a computer program, not a novel, I
still have to go find the definition of PROGRAM_IS_RUNNING.
>>
>> The clear meaning of "while (!done)" is that the variable "done"
>> represents a condition that will become true when the loop is meant to
>> terminate.
>
>That program WILL terminate sometime. If nothing else, when the power
>switch is flipped.
Are you saying that the power switch will change the value of "done"?
I suspect that's implementation-dependent.
>
>>
>> I assume that anyone reading my code either is familiar with C, or
>> just won't be able to understand the code.
>>
>Not a good assumption. Even you can mis-read your own code on a bad day -
>you'll wonder the next day how you could have written such code.
Much more likely to happen if you obfuscate the code in the ways
you're suggesting.
--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@xxxxxxx
.
- Follow-Ups:
- Re: while (1) vs. for ( ;; )
- From: Baxter
- Re: while (1) vs. for ( ;; )
- References:
- Re: while (1) vs. for ( ;; )
- From: Baxter
- Re: while (1) vs. for ( ;; )
- Prev by Date: Re: Why does it run so long?
- Next by Date: Re: Detecting freed memory
- Previous by thread: Re: while (1) vs. for ( ;; )
- Next by thread: Re: while (1) vs. for ( ;; )
- Index(es):
Relevant Pages
|
|