Does it?
I would normally pick this,
NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
Over this,
[self setMonths: [self monthsForYear:newChosenYear]];
Mostly because it’s easy to understand at a first glance. Where the second approach, not so much.
But what are really the implications of this? monthsForChosenYear, is just a pointer, but it must be stored somehow.
I’m not asking if the impact is so small that I wouldn’t need to worry about it. But I am very curious about this.
Even a redirect to some document explaining in better detail would be nice.
Thank you in advance!
Nuno
A long answer to hopefully assuage your curiosity, and having curiosity is good! The performance & memory impact is either zero or miniscule. You wonder how the pointer is stored. When you type:
You are asking for a box (variable), to be referred to by the name
monthsForChosenYear, to be allocated in local storage. This box will be automatically reclaimed when the enclosing method exits, and possibly earlier than that if the compiler figures out it is no longer needed. This box can hold a value of typeNSArray *.When you type:
You are asking for two things, it is just a shorthand for:
and the second line calls a method and stores the returned value in your box named
monthsForChosenYear. Finally when you type:the value stored in the your box
monthsForChosenYearis passed to a method. If you no longer use the boxmonthsForChosenYearthe compiler may reclaim it, or it may wait till the end of the enclosing method or some other suitable point.Compilers analyze the usage of boxes and optimise, sometimes they will not even allocate a box if it is determined one is not needed. The cost of allocating a box is infinitesimal.
*[Note: there are actually usually two kinds of local boxes. The second, often called a register, has an allocation cost which is usually even smaller than infinitesimal. Which kind of box is used is decided by the compiler.]*
When you type:
You are asking for two methods to be called in one line and the value returned from the inner call (
[self monthsForYear:newChosenYear]) has to be passed to the outer call. Where does the compiler store this result? The compiler in effect translates the above into:and from the above you know how that goes. There is a small advantage to the compiler in that it knows how long
compilerTemporaryBoxis needed, so it doesn’t need to figure it out, but that will not effect the compiled code.So after all that the overall answer is it doesn’t matter how you write it.
Furthermore the type of memory management you use: MRC, ARC or GC; will not effect the answer – the compiler will end up treating both your variants the same.
So go for the style you find best for you. HTH.