开发者

iPhone SDK: speed vs mem footprint

In my app delegate I have a variable which various controllers use (thank to the news group advise on how to access such variable from controllers). Each controller may need to access it more then once. I use this code:

((MyAppDelegate *) [UIApplication sharedApplication].delegate).soundEffects

Ignoring readably issue (as noneone other them me will ever see these src) would it change anything if in the controller init I have I get a pointer to the app delegate object and just access it like so:

...
MyAppDelegate *foo = [UIApplication sharedApplication].delegate
...
foo.soundEffects ...

It looks like second approach would require more mem storage to hold instance va开发者_如何学Pythonriable foo, while my first approach could be slower to get to the variable.

What is better?

thanks


You don't need to worry about speed or memory in this case. Resist the temptation to optimize before you know you have a problem. You're much better served by focusing on the readability of your code, even if you're the only one reading it. Well designed code that's easier to read and maintain will have fewer bugs, and will be easier to optimize if you do find yourself facing speed or memory problems down the road. Don't hesitate to sacrifice a few bytes here and there if it makes your code easier to read.

In this case, your proposed change is certainly a step in the right direction, so go ahead and make this incremental change if you'd like.

If you're feeling bold and want to take another step in the right direction, you could add a soundEffects member variable to each controller that needs it, make it a property using @property (nonatomic, retain), and set that property after you construct each controller (in your app delegate's application:didFinishLaunchingWithOptions:, and wherever else you might be constructing these controllers). This way your controllers don't even need to know that the app delegate exists. This "loose coupling" is an object oriented design virtue that minimizes the dependencies between your objects, making them easier to maintain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜