I am writing some code for data analysis, and have to exclude samples based on some criteria. In practice I end up writing code such as:
bool Test(SampleType sample)
{
if( ! SubTest1(sample) )
return false;
if( ! SubTest2(sample) )
return false;
if( ! SubTest3(sample) )
return false;
return true;
}
The following seems equivalent to me:
bool Test(SampleType sample)
{
if( ! SubTest1(sample) )
return false;
else if( ! SubTest2(sample) )
return false;
else if( ! SubTest3(sample) )
return false;
else
return true;
}
Is there a difference in terms of computing cost? Is there a arguable preferential one in terms of extendibility/maintainability, aesthetics, etc…?
I know this is probably an inconsequential issue, but once I get these questions stuck in my head I NEED to find the answer.
PS: in case anyone cares, my actual code as of 15/09 can be found at the following:
http://folk.uio.no/henrikq/conf.tgz
Compiler generates the same code for both the versions. But the 1st version is better in maintainability aspect if you compare just with the 2nd version.
The code exits when the
returnstatement is encountered; so there is no use of keepingelsein the upcomingif. It makes the developer understand the code better.Also, if this is the literal code then you can still shrink as,