Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Created June 4, 2012 16:39
Show Gist options
  • Select an option

  • Save bjhomer/2869460 to your computer and use it in GitHub Desktop.

Select an option

Save bjhomer/2869460 to your computer and use it in GitHub Desktop.
'return' meaning different things semantically
// This is just a toy example, but it demonstrates the semantic
// overloading of the 'return' keyword when blocks are in play.
// Note that I'm not saying this is confusing, per se - I'm just
// saying that it adds cognitive load as I look at the method and
// try to figure out which 'return' statements actually represent
// an early return.
- (Person *)findPersonWithName:(NSString *)name {
NSArray *families;
if (name.length == 0) {
return nil;
}
// People can be part of more than one family; don't
// check the same person multiple times.
NSMutableArray *eliminatedPeople = [NSMutableArray new];
__block Person *thePerson = nil;
[families enumerateObjectsUsingBlock:^(Family *family, NSUInteger familyIndex, BOOL *stopFamilies) {
NSArray *familyMembers = family.people;
[familyMembers enumerateObjectsUsingBlock:^(Person *person, NSUInteger personIndex, BOOL *stopFamily) {
if ([eliminatedPeople containsObject:person]) {
return; // This is really acting more like 'continue'
}
if ([[person name] isEqual:name]) {
thePerson = person;
*stopFamilies = YES;
*stopFamily = YES;
return; // This is really acting more like 'break'
}
[eliminatedPeople addObject:person];
}];
}];
return thePerson; // This really means 'return';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment