Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefer if evaluations [closed]

Tags:

objective-c

Which would be the preferred method? With the assuming that myInteger 99.99% of time will have a valid value and this method will get called very rapidly.

-(BOOL)validDate
{
    NSUInteger myInteger = ...//method to obtain value;    
    if (myInteger != NSNotFound)
        return YES;        
    return NO;
}


-(BOOL)validDate
{
    NSUInteger myInteger = ...//method to obtain value;    
    if (myInteger == NSNotFound)
        return NO;        
    return YES;
}
like image 827
user523234 Avatar asked Nov 27 '25 02:11

user523234


1 Answers

The preferred would be:

- (BOOL)validDate
{
    NSUInteger myInteger = ...//method to obtain value;    

    return (myInteger != NSNotFound);
}

because it doesn't actually need to execute a branch.

Note that the difference will be imperceptibly small unless you are running this code a lot...

like image 197
Wain Avatar answered Nov 28 '25 17:11

Wain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!