S
Simon Biber
Tim said:I myself would have no problem with people writing
FOREVER {
}
No, it must be the following!
FOREVER
{
}
Tim said:I myself would have no problem with people writing
FOREVER {
}
I was surprised at how many people reported that a compiler
they use issues a warning for 'while(1)' and gave that as a
reason for giving preference to the 'for(;' form. It
seems like a choice should be made on the basis of what's a
better expression (lower defect rate, higher productivity,
better understood by the readers), not on the basis of some
errant warning message.
Richard said:Seems you've never heard of break.
What about following?
#defined loop_forever for(;
Christopher said:Would you suppose that such a view is common in the world of
professional programming? My work experience is still quite limited,
and it's hard to tell ubiquitous programming conventions from
idiosyncratic oddities...
No. In my experience, people in the world of professional programming
prefer:
* enormous functions
* multiple exit points from functions and loops
* multiple entry points, if they can get them
* spaghetti logic
* tight coupling
* vry shrt var nms
* evnshrterfnnms
* low cohesion
* lots of debugging
* hardly any testing
* lots of maintenance
That's not been my experience.
IIRC that's one reason, why COBOL has multiple entry points into functions.
Richard Heathfield said:Michael B Allen said:
Should there be any preference between the following logically equivalent
statements?
while (1) {
vs.
for ( ;; ) {
I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.
[snip]
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").
What Richard might be saying, but isn't really what I think
he's trying to say, is that the control expression should
redundantly express the condition for loop exit, even if the
loop is never exited by the test on the loop control.
My first reaction was that he's meaning to say that loops
that look infinite (but aren't) are bad,
and the code should
be reworked so that the loop control expression is really
what controls the loop body.
pete said:The warning is the tie breaker.
There is no difference in defect rate,
productivity,
or understanding by the readers.
Richard Heathfield said:Tim Rentsch said:
[snip]
My first reaction was that he's meaning to say that loops
that look infinite (but aren't) are bad,
All else being equal, yes...
and the code should
be reworked so that the loop control expression is really
what controls the loop body.
...and yes.
(This reply is purely for clarification of my earlier intent - I did read
the rest of your article, and I accept that our viewpoints differ.)
Richard said:Default User said:
You've been a very lucky chap. (And, on occasion, so have I.)
Tim Rentsch said:Is this statement just one of belief, or are you offering
some evidence?
There is for some developers. Among other things, some
debugging techniques work better when the 'while(1)' form is
used. If the people who prefer the 'for(;' form don't see
any difference in productivity, that seems to be an argument
in favor of using the 'while(1)' form.
At least one poster in this thread said something along
the lines of using 'while' for "looping" and 'for' for
"iteration". In the non-infinite case, that's usually
my leaning also. So there is *some* difference in how
the two forms are understood.
Keith Thompson said:In another thread, I've railed against "if (0 = a)", even though it's
semantically identical to "if (a == 0)". And yes, I'm taking the
opposite side of this argument. The difference, which is largely in
my head, is that I don't find either "while (1)" or "for (;" to be
ugly or jarring.
What Richard might be saying, but isn't really what I think
he's trying to say, is that the control expression should
redundantly express the condition for loop exit, even if the
loop is never exited by the test on the loop control. So
for example,
/* p != NULL; */
while( p != NULL ){
...
...
p = blah_blah_blah();
if( p == NULL ) break;
...
... code that doesn't affect p ...
}
Keith Thompson said:Charlie Gordon said:news:[email protected]... [...]
You are right, but things are a bit more complicated than this: pretending to
clean up the C language is doomed.
Just look at :
#include <stdbool.h>
#include <ctype.h>
...
while (isdigit(*s) == true) {
... sometime works, sometimes not ?...
}
That's solved by following a simple rule: never compare a value to a
literal true or false. Comparing to true or false is both error-prone
and useless. If an expression is a condition, just use it as a
condition.
The existence of type bool doesn't mean you can't use
while (isdigit(*s)) {
...
}
Default User said:Perhaps luck in that the software here has generally been written to
Common Coding Standards (if ERT opposes it, it must be good) which
either forbid or recommend against most of the things on your list.
Naturally, debugging, testing, and maintenance are different, not
really things controlled by a standard.
Keith Thompson said: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?
What debugging techniques are you referring to? Off the top of my
head, I'd say that any debugging technique that treats the two forms
significantly differently is broken, but I'm prepared to be
enlightened.
I read both "while (1)" and "for (;" as "this is an infinite loop,
or at least one for which the termination condition is not specified",
and any C programmer should be familiar with both forms. I see the
point of preferring "for (;" when iterating over some set of
discrete entities, but I don't think it's a strong argument.
In another thread, I've railed against "if (0 == a)", even though it's
semantically identical to "if (a == 0)". And yes, I'm taking the
opposite side of this argument. The difference, which is largely in
my head, is that I don't find either "while (1)" or "for (;" to be
ugly or jarring.
Charlie said:toCharlie Gordon said:[...]
You are right, but things are a bit more complicated than this: pretending
clean up the C language is doomed.
Just look at :
#include <stdbool.h>
#include <ctype.h>
...
while (isdigit(*s) == true) {
... sometime works, sometimes not ?...
}
That's solved by following a simple rule: never compare a value to a
literal true or false. Comparing to true or false is both error-prone
and useless. If an expression is a condition, just use it as a
condition.
The existence of type bool doesn't mean you can't use
while (isdigit(*s)) {
...
}
I just meant to stress the point, that the C language is full of traps and
pitfalls... giving it a java flavor doesn't fix them.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.