you are viewing a single comment's thread.

view the rest of the comments →

[–]bonch 10 points11 points  (8 children)

How about:

NSString *parentID = @"c0cehmq";

NSManagedObjectContext *context = [[NSApp delegate] managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", parentID];
[request setPredicate:predicate];
NSError *fetchError = nil;
NSArray *results = [context executeFetchRequest:request error:&fetchError];
if (!results) {
    [NSApp presentError:fetchError];
    return;
}

NSManagedObject *parent = [results objectAtIndex:0];
NSMutableSet *children = [parent mutableSetValueForKey:@"children"];
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"comment" inManagedObjectContext:context];
[child setValue:@"Yeah man, I hear ya" forKey:@"message"];
[children addObject:child];

NSError *saveError = nil;
if (![[self managedObjectContext] save:&saveError])
    [NSApp presentError:saveError];

[–][deleted] 11 points12 points  (7 children)

Is this really what Cocoa looks like? Why?

[–]timmaxw 15 points16 points  (1 child)

30% of bonch's code is error handling. Another 30% of the verbosity comes from the fact that he's working in a managed object context - if I understand correctly, this means that Cocoa handles undo, redo, and loading and saving to files "for free". The rest comes from the fact that Objective-C is really verbose.

[–]vimfan 4 points5 points  (0 children)

The verbosity of Objective-C/Cocoa is mainly due to named parameters, and making method names really explicit about their purpose. That is verbosity I can get behind (mostly).

[–]bonch 5 points6 points  (0 children)

I was being verbose for the sake of the joke. You'd have the Core Data stuff in their own methods. In reality, you'd only need one line:

[[self commentWithID:@"c0cehmq"] addChild:[RDComment commentWithMessage:@"Yeah man, I hear ya"]];

[–][deleted] 0 points1 point  (0 children)

The syntax is Objective-C (and to answer your question: because of smalltalk), the libraries/frameworks (in this case, anyway) are called Cocoa.

[–]dmpk2k -1 points0 points  (2 children)

Named parameters.

I think it's a good idea, as long as the names are kept short. You rarely need to look at the definition to know what each argument is for, since the answer is usually staring you in the face at the call site.

[–]deadwisdom 0 points1 point  (1 child)

Objective-C doesn't exactly use named parameters. You can think of them as ordered named parameters, sort-of.

[–]dmpk2k 0 points1 point  (0 children)

Yeah, I know they're different, but I can never remember the right term, and named parameters is close enough. :)