Re: Is it okay if I use a lot of while(true) loops?
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>, on 21/08/2010
15:31:41, wrote:
?? Tiib wrote:
On 21 aug, 19:03, "Alf P. Steinbach /Usenet"<alf.p.steinbach
+use...@gmail.com> wrote:
* James Kanze, on 21.08.2010 16:55:
On Aug 20, 6:57 pm, "Francesco S. Carta"<entul...@gmail.com> wrote:
tni<t...@example.invalid>, on 20/08/2010 19:53:19, wrote:
On 2010-08-20 18:42, James Kanze wrote:
On Aug 20, 3:36 pm, Alexander<alva...@gmail.com> wrote:
In my code I usually find it much simpler to use a while(true) (or
do ... while(true)) loop instead of putting a real condition,
and make
a test inside the body of the loop. Usually I find it much more
comfortable to check manually.
So, if you were reading my code, would you dislike this?
Very. At the top of the loop, you say it's forever. If it's
not, then you've lied to the reader of your program.
No, you haven't.
I'm pretty sure that James was kidding up there. This issue and all
common argumentations about it are so obvious and straightforward that
it's hard to believe otherwise :-)
I'm certainly not kidding. I believe in writing clean, easily
understood code. Using break in the middle of a loop is not
clean, easily understood code. It's forbidden anywhere I have
an influence over the coding standard.
Then you get contortions& redundancy for common loop-and-a-half loops.
There are two arguments against 'while(true)' as opposed to 'for(;;)':
* more to write, and
* causes sillywarnings with MSVC. :-)
I think we had the discussion about loop-and-a-half at least twice
before, and
you've been partially convinced by some example like
for( ;; )
{
showGameState();
cmd = usersCommand();
if( cmd == exitCmd ) { break; }
doCommand( cmd );
}
Why not:
Command cmd = restCmd; // or where is that cmd declared?
while( cmd != exitCmd )
{
showGameState();
cmd = usersCommand();
doCommand( cmd );
}
I guess because it's not equivalent: your code would doCommand() even if
cmd == exitCmd and Francesco's code wouldn't;
Just for the records, I posted no code in this thread; that was Alf's code.
--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com