macro help

A

ankitks

guys,
I have to do some testing, and usally code flows like this. If
something is wrong, flag error. And if it is correct, see if something
else is wrong, flag error! And recursive like that.

if (a != b) {
flag_error();
}
else {
if ( c != d) {
flag_error();
}
else {
if ( e != f) {
flag_error();
}
} //end of else c != d
} //end of else a !=b

it is really annoying to read code later for debugging. Using a !=b ?
flag_error() : c != d ? ...doesn't seem that practical either.
how can I convert this into macro so that I can use it more readable
something like this!

VERIFY a == b OTHERWISE flag_error();
GO_NEXT;
VERIFY c == d OTHERWISE flag_error();
GO_NEXT;
VERIFY e == f OTHERWISE flag_error();
is this recursive macro possible.

Sorry, for asking dumb quesitons. Trying to make life easy, if possible.
 
C

Clark S. Cox III

guys,
I have to do some testing, and usally code flows like this. If
something is wrong, flag error. And if it is correct, see if something
else is wrong, flag error! And recursive like that.

if (a != b) {
flag_error();
}
else {
if ( c != d) {
flag_error();
}
else {
if ( e != f) {
flag_error();
}
} //end of else c != d
} //end of else a !=b

What's wrong with (it seems at least as readable as your proposed solution):
if (a != b) {
flag_error();
}
else if ( c != d) {
flag_error();
}
else if ( e != f) {
flag_error();
}
else
{
//If we get here, nothing is wrong
}

Or, using exceptions:
if (a != b) {
throw some_error;
}
if ( c != d) {
throw some_error;
}
if ( e != f) {
throw some_error;
}
//If we get here, nothing is wrong
 
D

David Harmon

On 21 Oct 2006 18:45:13 -0700 in comp.lang.c++, (e-mail address removed)
wrote,
I have to do some testing, and usally code flows like this. If
something is wrong, flag error. And if it is correct, see if something
else is wrong, flag error! And recursive like that.

It's actually not recursive; rather, it's alternative.

My version:

if ((a != b)
|| (c != d)
|| (e != f)) flag_error();
 
D

David Harmon

On Sun, 22 Oct 2006 12:51:11 +0200 in comp.lang.c++, "Fox"
That's good if there is one error to all cases.

That seemed to be the example given.
If not, then my next suggestion is:

if (a != b) return flag_error();
if (c != d) return flag_error();
if (e != f) return flag_error();
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top