Use of do...while(0) in ASSERT macro

K

Kelsey Bjarnason

[snips]

Seriously, it's a reasonable point, but in my defence[1] I generally do my
level best to get the coding standards at a client site changed to mandate
compound statements if they don't already do so. I've had some degree of
success with this. :)

As in:

if ( x )
{
y();
}

This is, IMO, bad. It clutters code with unnecessary crud. If it helps
because y might be some mucked-up macro that does weird things with block
levels or other sorts of nastiness, I'd tend to focus on the problem -
the macro - rather than this sort of approach.

Then again, I'd tend to try to disallow macros beyond the most trivial in
the first place - use a function, not a macro, if it needs to "do things".
 
R

Richard Heathfield

Kelsey said:
[snips]

I generally do
my level best to get the coding standards at a client site changed to
mandate compound statements if they don't already do so. I've had some
degree of success with this. :)

As in:

if ( x )
{
y();
}

Yes, precisely. Well, not quite as much whitespace. I'd prefer:

if(0 != x)
{
y();
}
This is, IMO, bad.

That doesn't surprise me, but I must disagree with you.
It clutters code with unnecessary crud.

That's a negative description. Here's a positive one to balance: it
emphasises the logical structure of the code.
If it helps
because y might be some mucked-up macro that does weird things with block
levels or other sorts of nastiness, I'd tend to focus on the problem -
the macro - rather than this sort of approach.

No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)

No, I'm perfectly happy with my bracing style, thanks - even if you disagree
with it.
Then again, I'd tend to try to disallow macros beyond the most trivial in
the first place - use a function, not a macro, if it needs to "do things".

There are certain rather obvious uses for macros. (Bit-twiddling is one.
Wrapping __FILE__ and __LINE__ is another.)
 
S

Sheldon Simms

No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)

No, I'm perfectly happy with my bracing style, thanks - even if you disagree
with it.

I'm going to offer what may be an odd opinion. I don't follow your style
Richard; I like to use single statements without braces after if(). I have
never seen a single case in which a programmer actually had any problem
with this style. But I would support mandatory bracing in a coding
standards document because I think it makes the code easier to read when
more than one person is working on it.
 
J

John Devereux

Richard Heathfield said:
Yes, precisely. Well, not quite as much whitespace. I'd prefer:

if(0 != x)
{
y();
}

No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)


What is wrong with simply

if(x) y();

???
 
R

Richard Heathfield

John said:
What is wrong with simply

if(x) y();

???

Syntactically, nothing. In practice, there are two reasons why I don't do
this.

Firstly, when I'm stepping through code with one of those new-fangled
debuggers, it's much easier to see whether y() is called if it's on a
separate line.

Secondly (and this applies equally to the multi-line version of the same
code), Murphy's Law states quite clearly that, if I use a single-statement
if() now, I will inevitably, and probably at some point in the very near
future, think of something else that needs to be done in the same block, so
I might as well make it a compound statement right now.
 
J

John Devereux

Richard Heathfield said:
Syntactically, nothing. In practice, there are two reasons why I don't do
this.

Firstly, when I'm stepping through code with one of those new-fangled
debuggers, it's much easier to see whether y() is called if it's on a
separate line.

Yes, that's true. Similarly, I suppose your form makes it easier to
locate the offending line when you get a compiler diagnostic.
Secondly (and this applies equally to the multi-line version of the same
code), Murphy's Law states quite clearly that, if I use a single-statement
if() now, I will inevitably, and probably at some point in the very near
future, think of something else that needs to be done in the same block, so
I might as well make it a compound statement right now.

No. Never do today what can be put off until tomorrow :)
 
C

CBFalconer

Richard said:
.... snip ...

No, the macro isn't the problem. People are the problem. They
/will/ keep turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected.
(Utter foolishness, I know.)

Which is why my compromise, not rigorously adhered to, is:

if (w != x) y();
vs
if (w != x) {
y();
z();
}

This removes the temptation to squeeze in a line somewhere. It
also does not exacerbate the newline shortage.
 
R

Richard Heathfield

CBFalconer said:
Which is why my compromise, not rigorously adhered to, is:

if (w != x) y();
vs
if (w != x) {
y();
z();
}

This removes the temptation to squeeze in a line somewhere. It
also does not exacerbate the newline shortage.

No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:

#include <stdio.h>
{
int i = 0;
while(i++ < 1000)
{
puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
return 0;
}

You can redirect to a file, of course. If you use them all up, just run it
again.

No charge. God Bless America.
 
A

Alan Balmer

I have
never seen a single case in which a programmer actually had any problem
with this style

I have. I don't know how often it happens, because I only see those
cases where the bug doesn't become obvious immediately and I have to
fix it.
 
D

Dan Pop

In said:
Yes, that's true. Similarly, I suppose your form makes it easier to
locate the offending line when you get a compiler diagnostic.

If this is an issue at all (not everybody is a debugger junkie), there is
a simpler solution:

if (x)
y();

i.e. exactly the way you'd do it if x were a fairly long expression and/or
the y() function had a lot of parameters.

Dan
 
C

Christopher Benson-Manica

Richard Heathfield said:
No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:
(snipped)

You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek. I'd give you a few,
but unfortunately I don't even have one to winky-face this post, never
mind yours. It seems, then, that we will sound much terser than we
really are, at least until China begins exporting cheap semicolons.
 
I

Irrwahn Grausewitz

<unsnipped>
#include <stdio.h>
{
int i = 0;
while(i++ < 1000)
{
puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
return 0;
}
You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek.

As it turns out Richard isn't short on semicolons but on
'int main(void)'s. I can offer him some of my spare ones:

int main(void) int main(void) int main(void) int main(void)
int main(void) int main(void) int main(void) int main(void)

BTW: I still have a whole lot of 'void main(void)'s in a box
in my cellar, left over from my first attempts in writing C
programs; is anybody interested? Free of charge, delivery via
email...
I'd give you a few,
but unfortunately I don't even have one to winky-face this post, never
mind yours. It seems, then, that we will sound much terser than we
really are, at least until China begins exporting cheap semicolons.

Coincidentally I just received a box of brand new premium semicolons
today; here you go:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If you need more, just email me. ;-)
 
C

Christopher Benson-Manica

Irrwahn Grausewitz said:
Coincidentally I just received a box of brand new premium semicolons
today; here you go:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Great, thanks. My winky faces will look stupdendous now ;)
 
A

Alan Balmer

You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek.

I didn't need a "winky-face" to categorize that post ;-)
 
A

Alan Balmer

As it turns out Richard isn't short on semicolons but on
'int main(void)'s. I can offer him some of my spare ones:

I assumed it was a function, to be called whenever more newlines are
needed. Although you could argue that newlines are an end in
themselves.
 
R

Richard Heathfield

Irrwahn said:
If so, he's short on funcion names. ;-)
^^^^^^^


tttttttt
tttttttt
tttttttt
tttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt tttttttt
tttttttt tttttttt
ttttttttttttttttttt
ttttttttttttttttt
tttttttttttttt
tttttttttt


You seemed o be a bi shor of he le er ' '. You are mos welcome o some
of mine.
 
I

Irrwahn Grausewitz

Richard Heathfield said:
Irrwahn Grausewitz wrote:
^^^^^^^


tttttttt
tttttttt
tttttttt
tttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt
tttttttt tttttttt
tttttttt tttttttt
ttttttttttttttttttt
ttttttttttttttttt
tttttttttttttt
t ttt


You seemed o be a bi shor of he le er ' '. You are mos welcome o some
of mine.

Ahh, that's better now, thanks a lot. :D
 
D

Dave Thompson

CBFalconer wrote:

No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:

#include <stdio.h>
{
int i = 0;
while(i++ < 1000)
{
puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
return 0;
}
IRRTYWanted an int main(void) in there.
You can redirect to a file, of course. If you use them all up, just run it
again.

No charge. God Bless America.

<PHB> I tried (with that correction) and after several thousand runs
my battery ran down. Please email me some more electricity. </>

- David.Thompson1 at worldnet.att.net
 
D

Dan Pop

If you [*] feel compelled to keep this inane thread alive, have at least
the minimal decency to insert an [OT] tag in the subject line.
___

[*] By "you" I'm not referring to Richard Heathfield *only*.

Dan

P.S. Deliberately top posting because this is one of the cases where top
posting is appropriate. While keeping RH's nonsense included in my
post, to provide the necessary context, there is no point in
forcing everybody to scroll through it in order to find my text.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top