[PATCH] use for (;;) instead of while (1) to avoid warning: conditional expression is constant

318 views
Skip to first unread message

Hannes Eder

unread,
Dec 2, 2007, 12:54:19 PM12/2/07
to fard...@googlegroups.com
See attached .diff

Instead of for (;;) to following macro could be also be used:

#define forever for(;;)

Comments?

-Hannes

forever-200712021.diff

Alex Yaroslavsky

unread,
Dec 2, 2007, 1:12:34 PM12/2/07
to Hannes Eder
Hello, Hannes!

HE> Instead of for (;;) to following macro could be also be used:

HE> #define forever for(;;)

HE> Comments?
for(;;) and while (1) are good enough, 1337 not needed :)

--
Bye,
Alex.


Hannes Eder

unread,
Dec 2, 2007, 1:47:20 PM12/2/07
to fardeven
Hi Alex,

> HE> Instead of for (;;) to following macro could be also be used:
> HE> #define forever for(;;)
>
> HE> Comments?
A> for(;;) and while (1) are good enough, 1337 not needed :)

Hehe. Yes it could be considered 1337 to use "forever" in the code,
and I think it gives a clear idea of the intended meaning of the
statement.

my 2 cents ;)
-Hannes

Alex Yaroslavsky

unread,
Dec 2, 2007, 1:53:23 PM12/2/07
to Hannes Eder
Hello, Hannes!

>> HE> Instead of for (;;) to following macro could be also be used:
>> HE> #define forever for(;;)
>>
>> HE> Comments?
A>> for(;;) and while (1) are good enough, 1337 not needed :)

HE> Hehe. Yes it could be considered 1337 to use "forever" in the code,
HE> and I think it gives a clear idea of the intended meaning of the
HE> statement.
Well, for(;;) is very clear to any c/c++ programmer.

--
Bye,
Alex.


Hannes Eder

unread,
Dec 3, 2007, 5:22:58 AM12/3/07
to fardeven
Hi, Alex!

A> for(;;) and while (1) are good enough, 1337 not needed :)

A> Well, for(;;) is very clear to any c/c++ programmer.

using "for (;;)" instead of "while (true)" or "while (1)" has the
advantage that it avoids the warning 'C4127 conditional expression is
constant', which in turn can help to discover code were this happens
in
error, therefore I submitted the patch.

-Hannes

Alex Yaroslavsky

unread,
Dec 3, 2007, 5:28:49 AM12/3/07
to fard...@googlegroups.com
This is a bit too much. This level of warnings interferes more with
the work than it helps.

techtonik

unread,
Dec 3, 2007, 6:31:34 PM12/3/07
to fard...@googlegroups.com

Only for those who knows the code. Unfortunately, these folks are minority. =)

--
--anatoly t.

Hannes Eder

unread,
Dec 4, 2007, 4:38:39 AM12/4/07
to fard...@googlegroups.com
Anatoly t. [tech...@gmail.com] worte:

Ok, using a macro "forever" instead of "for (;;)" is a matter of
taste, that's why I stated "could be used" to bring it up for
discussion. I do not what to force anybody to use that.

But on the other hand using "for (;;)" instead of "while (1)" has a
clear advantage: it avoids a compiler warning while having the same
semantic.

Do you agree on "fewer warnings, cleaner code"?

To make it clear I don't want to force anybody to compile the code
with /W4 all the time, but from time to time this can be quite useful
to discover issues. I usually disable a few warnings while inspecting
others, and from time to time I think it's quite useful to run a code
analysis (/analyze).

And how much work is it to change "while (1)" and co. to "for (;;)"? I
regex search/replace does the job. While this pay off, I do think so
on the long run.

Well, just my 2 cents.

-Hannes

Hannes Eder

unread,
Dec 9, 2007, 8:34:10 AM12/9/07
to fard...@googlegroups.com
However, please find an updated version of this patch attached, I hope
somebody is keen enough to apply it ;)

-Hannes

forever-200712091.patch

techtonik

unread,
Dec 10, 2007, 1:57:06 PM12/10/07
to fard...@googlegroups.com
On Dec 4, 2007 11:38 AM, Hannes Eder <hanne...@gmail.com> wrote:
>
> But on the other hand using "for (;;)" instead of "while (1)" has a
> clear advantage: it avoids a compiler warning while having the same
> semantic.
>
> Do you agree on "fewer warnings, cleaner code"?

It is better to concentrate on releasing first Beta than on polishing
borked code. Although it allows you to study the code base, if you
won't fix compiler warning until Beta, you will have one more chance
to review the code more carefully and remove mistakes before
"release".

Fixing compiler warning is just a thin layer of sugar on a top of a
real cake. Inside the cake the stuff is more interesting.

--
--anatoly t.

Hannes Eder

unread,
Dec 20, 2007, 10:35:27 AM12/20/07
to fard...@googlegroups.com

Hi Anatoly,

Ok, that's your opinion. Herb Sutter and Andrei Alexandrescu state
another opinion on this topic (see [1]). I think this "icing on the
cake"-metaphor does not fit very well, although I agree that the
interesting stuff is "inside", I think of this warnings more as of
"the dust that should be removed in order to have a better sight on
the inside ". Especially in this case, these warnings "for(;;) vs.
while (true)" are just noise in the compile run output, that can be
easily removed. Furthermore I conjecture nobody will have a look at
this warnings and fix them before the next release, proof me wrong ;)

Just my 2 cents,
-Hannes

Refs:

[1] Herb Sutter, Andrei Alexandrescu: C++ coding Standards: 101 Rules,
Guidelines and Best Practices, Addision Wesly, October 2004.

Especially for this topic see the excerpt at:

http://www.informit.com/articles/article.aspx?p=373337&rl=1


techtonik

unread,
Dec 23, 2007, 2:08:02 AM12/23/07
to fard...@googlegroups.com
Hello, Hannes

On Dec 20, 2007 5:35 PM, Hannes Eder <hanne...@gmail.com> wrote:

> > Fixing compiler warning is just a thin layer of sugar on a top of a
> > real cake. Inside the cake the stuff is more interesting.
>
> Ok, that's your opinion. Herb Sutter and Andrei Alexandrescu state
> another opinion on this topic (see [1]). I think this "icing on the
> cake"-metaphor does not fit very well, although I agree that the
> interesting stuff is "inside", I think of this warnings more as of
> "the dust that should be removed in order to have a better sight on
> the inside ".

If you are unable to see the machine under a thin pile of dust then
how are you going to fix oily cogs covered inside? =) In any case I do
not have write access to help you and I don't want to see anybody lose
their enthusiasm just because one little patch is avoided for now.

> Especially in this case, these warnings "for(;;) vs.
> while (true)" are just noise in the compile run output, that can be
> easily removed. Furthermore I conjecture nobody will have a look at
> this warnings and fix them before the next release, proof me wrong ;)

Sometimes compiler may be dumb and it is not the reason to obfuscate
things on early stage of development. There are chances that somebody
fix the warnings before next release, but first we need beta. Warnings
are pretty ok for flaky alpha code, so there are not many chances they
are to be fixed right now.

> Refs:
>
> [1] Herb Sutter, Andrei Alexandrescu: C++ coding Standards: 101 Rules,
> Guidelines and Best Practices, Addision Wesly, October 2004.
>
> Especially for this topic see the excerpt at:
>
> http://www.informit.com/articles/article.aspx?p=373337&rl=1

Nice reference, but it's just a recommendation - not a rule that could
be forced. Perhaps you need to earn some karma first to be heard, by
providing more important piece of code (which affects the binaries
produced). =)

Happy Holidays!
--
--anatoly t.

Reply all
Reply to author
Forward
0 new messages