嵌套方法调用与单次变量 [英] Nested method calls vs. one-shot variables
问题描述
当嵌套方法调用或使用单稳变量时,最佳做法是什么?
What is the best practice when nesting method calls or using one-shot variables?
应该永远不要使用单稳变量吗?
Should you never use one-shot variables?
例如:
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:[NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata"]]
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]
error:&error];
应该总是将嵌套方法分成单次变量吗?
Should you always break up a nested method into one-shot variables?
例如:
NSNumber *yesNumber = [NSNumber numberWithBool:YES];
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:yesNumber
forKey:NSMigratePersistentStoresAutomaticallyOption];
NSString *urlPath = [applicationSupportDirectory stringByAppendingPathComponent:@"storedata"];
NSURL *url = [NSURL fileURLWithPath: urlPath];
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:url
options:optionsDict
error:&error];
还是应该使用两者的组合?
Or should you use some combination of the two?
例如:
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent:@"storedata"]];
[persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:url
options:optionsDict
error:&error];
我倾向于结合两者,但我想听听别人的说这个。如果上面不清楚,这些一次性变量是为了分解嵌套方法而创建的,不会在任何地方使用。
I tend to go with a combination of the two but I would like to hear what everyone else has to say about this. In case it is not clear above, these "one-shot variables" are created for the sole purpose of breaking up the nested method and will not be used anywhere.
推荐答案
我个人认为你应该使用一次性变量,因为它没有任何开销(在发布模式,即优化),它使调试更容易。除非你有特定的理由不要。
Personally I think that you should use "one-shot" variables since it doesn't come with any overhead (in "release" mode, that is with optimization on) and it makes debugging easier. Unless of course you have a specific reason not to.
这篇关于嵌套方法调用与单次变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!