Use immiedatly properties or use temporary variables during setup?

If I have a property like this:

@property (nonatomic, retain) UILabel* lblUsername; 

Should I in viewDidLoad do this:

 self.lblUsername = [[[UILabel alloc] init...] autorelease]; self.lblUsername.text = @"A label"; self.lblUsername.... 

Or should I do this:

 UILabel* usernameLabel = [[UILabel alloc] init...]; usernameLabel.text = @"A label"; usernameLabel.... self.lblUsername = usernameLabel; [usernameLabel release]; 

I have seen both, and I'm not sure what I should use, any advantages of using one over the other? (I know that both are "correct" in the syntax and both work, but which is preferable?)

+4
source share
2 answers

I would go a second time every time. In addition to the important fact that it makes no sense to use half-designed objects in your public interface (even if you still do not have a code that could detect an error), the second method is more effective at zero cost. For each self.thing.otherThing = you send at least two messages, and more if you ever get involved in KVO. There is no use for this indirectness - it does not make your code more understandable, it does not make it more concise, it does not make it more secure, nothing.

+2
source

I prefer the second method. The only real advantage is to reduce the load on the pool of autoresists, which is minimal, if only to initialize several such objects. It is unlikely that you are modifying this property in another thread, which could potentially cause unintended behavior when using the first method, but I believe this is a risk. It also seems to me that this is the difference in building a component and then installing it or installing an incomplete component and then creating it in place.

+4
source

Source: https://habr.com/ru/post/1415236/


All Articles