C
Chris F.A. Johnson
Why not use
while(is_the_pope_catholic) {
No, he's not. Did you mean:
while(is_the_pope_Catholic) {
Why not use
while(is_the_pope_catholic) {
Only because you then have to chase after call_the_real_function(). On
the initial writing, this would probably be only a few lines below,
but that could change. Hmm... Actually, the more I think about it, the
less I like it. Each function turns into two, and the validation's
connection to the rest of the code is more tenuous than necessary.
Mark said:I know it's only a trivial example... but why not:
if(nothing_strange_about_the_parameters)
status = call_the_real_function(passing_validated_parameters);
return status;
There are two tasks here - the validation of parameters, and the task for
which the function was actually written. Now, there may well be times when
you know perfectly well that the parameters are valid, because it's
actually impossible for them not to be valid. And there are times when
you're not sure.
In the cases where you are sure the parameters are okay, you can call the
function directly, avoiding the overhead of the parameter check. (You could
reasonably add assertion checks here, since they cannot possibly fire if
your program is correct.)
When you're not sure, you call the validation function - which either calls
the "do it" function directly or, perhaps slightly more cleanly, returns an
"everything's fine" value, after which you can proceed to call the "do it"
function yourself.
Then might they not also flounder with while(true) by not supplying the
standard header <stdbool.h>?
Ben said:As long as you want to be "cute":
#define ever ;;
for (ever) { ... }
(I would never use this.)
Chris McDonald said:I would suggest that both while(1) and for(; are potentially unclear
to a person (undergrad. student) seeing C for the first time
(while assuming they haven't seen Java or C++, either).
OK, I suggest that while(true) is *clearer*.
Richard Heathfield said:I'm quite unlikely to use multiple returns
Christopher Benson-Manica said:Would you suppose that such a view is common in the world of
professional programming?
Charlie said:BS
why not use the real clear thing :
WHILE (TRUE) {
}
or even
WHILE (TRUE == TRUE)
BEGIN
...
END
with obvious macro definitions for dummies ;-)
akarl said:In variable and function declarations the virtues of stdbool.h is clear.
Compare
int isThisAPredicate;
and
bool thisMustBeAPredicate;
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.
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 ?...
}
It is not a view I have seen espoused at the companies where I
have worked.
akarl said:Well, in C we have the similar do-while statement. In Modula on the
other hand there is a real exit-in-the-middle loop construct:
LOOP
...
IF someCondition THEN EXIT END;
...
END
Chris F.A. Johnson said:No, he's not. Did you mean:
while(is_the_pope_Catholic) {
Unfortunately, most programmers "know" that they don't make mistakes,
and so will go ahead and call the functions directly. They won't
put in the assert()'s, either, as those are "a drain on performance"
and if you know you are calling with valid parameters you don't need
them...
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...
Richard said:Alan Balmer said:
[avoiding multiple returns]
IIRC that's one reason, why COBOL has multiple entry points into functions.That's a reasonable argument. Here's another, which is diametrically
opposed.
There are two tasks here - the validation of parameters, and the task for
which the function was actually written. Now, there may well be times when
you know perfectly well that the parameters are valid, because it's
actually impossible for them not to be valid. And there are times when
you're not sure.
In the cases where you are sure the parameters are okay, you can call the
function directly, avoiding the overhead of the parameter check. (You could
reasonably add assertion checks here, since they cannot possibly fire if
your program is correct.)
When you're not sure, you call the validation function - which either calls
the "do it" function directly or, perhaps slightly more cleanly, returns an
"everything's fine" value, after which you can proceed to call the "do it"
function yourself.
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.