I don’t see any value in forcing the single return. Actually I think it generally makes code much harder to read. It turns the execution path from a tree into an onion.
I don’t see any value in forcing the single return. Actually I think it generally makes code much harder to read. It turns the execution path from a tree into an onion.—
So it’s just not me! Gah, I thought I was going crazy. I can’t stand single-return code, yet all Apple sample and template code uses it! I just had to google it to see if there was some good reason for it, or if they were just being annoying pricks. Turns out they are just annoying pricks.
Compare!
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
do your stuff...
}
return self;
}
… to …
- (id)initWithFrame:(CGRect)frame {
if ( ! [super initWithFrame:frame] ) return nil;
do your stuff...
return self;
}
Keep the common case left-most; indent the uncommon code and return early if the strange case makes the common case not make sense. So simple, so clean!