Skip to main content
gamedev.net
🔒 Locked

Return values and contitional statements...

Started by SAE Superman 2 replies 500+ views
Original Post
SAE Superman
SAE Superman
I was just wondering which way everyone else uses return values in condition statements: Option 1:
int func_retval = func( x, y, z );

if( func_retval )
{
   give_away_a_ferrari();
}

Option 2;
if( func( x, y, z ) )
{
   give_away_a_ferrari();
}

I prefer option #1 because it makes it easier to debug and if I have multiple dependancies on a function return value, I can use func_retval multiple times. Does anyone know the different speeds of the two? What are the pro's and con's? I know one con is you have to have an int for every func you want to use the return value for (can be a little messy?) and a pro is why I said I use option #1 above [grin] Discuss... [Edited by - SAE Superman on February 24, 2005 11:53:37 AM]
kuphryn
kuphryn
I prefer the second sample when possible.

Kuphryn
doynax
doynax
First of all, on any decent compiler (really, there's no magic involved) neither version will be any faster. Most also debuggers has a feature to view return values, although it's still somewhat easier to grab them directly from a variable.

My suggestion is that you don't set any fixed rules for using the former or the latter. Try to judge which would make the source more readable instead. For example if second version makes the line too long or complex it's probably a good idea to split it into two parts especially if the condition itself is complex.

In many cases you're forced to use one or the other (mostly the first). Such as when testing for multiple return values or checking for specific error conditions outside of the function's normal domain.
Oluseyi
Oluseyi
Quote:
Original post by SAE Superman
Does anyone know the different speeds of the two? What are the pro's and con's?
Speed is not a factor. There are no "pros" and "cons"; it all comes down to whether you will perform multiple tests on the return value and whether it is expensive to compute.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.