Wednesday, June 1, 2011

Code Indentation and Nesting

Code Indentation and Nesting - Speed of Light

The greatest benefit of this style is bailing early. Instead of deeply nesting your code (and thus heavily indenting it), you have simple statements designed to end execution in as few instructions as possible, and designed to be as simple to follow as possible. Consider the examples:


        - (void)doSomethingWithString:(NSString *)s {
            if (nil != s) {
                if ([s length] > 0) {
                    NSLog(@"%@", s);
                }
            }
        }
        
        // VS
        - (void)doSomethingWithString:(NSString *)s {
            if (nil == s)
                return;
        
            if (![s length])
                return;
        
            NSLog(@"%@", s);
        }

No comments:

Post a Comment