Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacMan988

macrumors 6502a
Original poster
Jul 7, 2012
833
116
Hi, I've been writing validation related "if conditions" in two different ways and I wonder what would be the better way to do. I want to improve my code quality.

first method:

Code:
-(void) someMethod {

    ...

    if ( [[NSFileManager defaultManager] fileExistsAtPath:path] ) {
    
        //  send email    

        ...
    
    }   else  {

        // show error

        ...

    }

}

second method:

Code:
-(void) someMethod {

    ...

    if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
    
        //  show error
  
        ...

       return;
     
    }

    // send mail

    ...

}

If non of these methods are correct, please tell me the best way to write it. Thanks.
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
I would tend to structure the code more like the second layout. Just because it leads to less indentation and makes the 'success' path flow a little better. It's all personal preference. Note that when it comes to execution performance, it doesn't matter which of those you choose.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.