Any point to this type of code?

Started by
28 comments, last by M2tM 15 years, 3 months ago
Ok, this is one type of coding I've seen, and I have NO idea why you'd do this.

AddVector( Vector *a, Vector *b, Vector *c )
{
const float a_x = a->x;
const float a_y = a->y;
const float a_z = a->z;
const float b_x = b->x;
const float b_y = b->y;
const float b_z = b->z;
const float c_x = a_x + b_x;
const float c_y = a_y + b_y;
const float c_z = a_z + b_z;
c->x = c_x;
c->y = c_y;
c->z = c_z;
}



Now, lets assume that the pointer notation can't be substituted for references or values. What is the point of making all those 'const float' declarations, that IMHO clutter up the code? Now, assume this type of coding takes place in a bigger function. Now you can't call AddVector(at,offset) because you only have at_x at_y at_z and offset_x offset_y offset_z. So you have to inline that function by hand(or construct new vectors just for the sake of that function call). So again, I wonder, what could possibly be the reason for doing this? and while I'm thinking about it.

#define foo() do {blah} while(0)


(lame... that was supposed to be a multi-line #define) What is the point of the "do/while"? isnt the {} enough to make the scoping work? [Edited by - KulSeran on January 14, 2009 3:33:15 AM]
Advertisement
1) Yes, that code is very unessecary and doesn't really accomplish what it's supposed to do; make the code more readable.
And also, the parameters should probably be references instead of pointers (that is, if it's proper C++).

2) do/while is the older version of just while and therefore you don't see it alot these days.
Having just the {} would merely create a sub-routine, not a loop.
Quote:Original post by KulSeran
What is the point of the "do/while"? isnt the {} enough to make the scoping work?
The point is to require a semicolon after the macro.
#define foo(x) {         \    multi;               \    line;                \    block;               \}#define bar(x) do {      \    multi;               \    line;                \    block;               \} while (0)          ...foo(42)    // <-- no semicolon required herebar(13)    // <-- won't compile unless you add the semicolon

yeah. whoops. typo on my part. shoulda been while(0) not while(1) and so thanks Oxyd. I never thought of that implication. Interesting trick, even if macros make the code hard to read/debug.

And the loop will be optimized away, right?
-----------------------------------------Everyboddy need someboddy!
If you use a modern compiler the while loop will be optimized away, so there is no point whatsoever in having it.
Quote:Original post by HomerSp
2) do/while is the older version of just while and therefore you don't see it alot these days.
Having just the {} would merely create a sub-routine, not a loop.
It's not older, it has different behaviour. And personally, I see it all the time...

Quote:Original post by HomerSp
If you use a modern compiler the while loop will be optimized away, so there is no point whatsoever in having it.
Apart from requiring the semicolon at the end of the macro. Even using an ancient compiler, it'll still compile away.
The do {} while(); defines when the loop condition modifiers are tested

running the block


do{//blah blah blah}while(0); //tested after the first iteration is ran/*will execute //blah blah blah */



while(0) // tested before the first iteration is ran
{
//blah
};




will skip over blah
0))))))>|FritzMar>
Quote:Original post by Evil Steve
It's not older, it has different behaviour. And personally, I see it all the time...

I suppose you're right, I never use it myself but I can see how it would behave differently (which could be useful) in a program.

So pardon the false explanation.

do {  execute} while(0);

will be executed once

while
while(0){ execute}

will be optimized away totally.

Edit: ...which I now see is what FritzMar just explained.
The first thing you posted is an optimisation by someone who probably went overboard with const. accessing a->x through a pointer more than once may have generated worse code than accessing it through the pointer only once, and storing the result in a local variable.

The do .. while thing is to make this kind of code work:
if (expression)    foo();else    bar();

if foo did not use the while(0) trick, and instead had multiple statements within { and } in it, then the semicolon after what looks like just a function call, would cause the else not to match the if.
I would be very surtprised if do .. while(0) is ever responsible for even a single additional assembly instruction, probably even in debug builds, on just about any compiler. A do..while is as simple as a single test and branch, and when the test is always false, then it's effectively a branch-never instruction, of which there is no such thing.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement