views:

43

answers:

3

I have searched many questions on ObjC accessors and synthesized accessors to no avail. This question is more of a "help me settle an issue" question; I don't expect one answer, but I'm rather looking for experts to weigh in on the argument.

In a Cocoa Touch class, I would write some code like this (where soundEffects is a synthesized NSArray property):

id foo = [self.soundEffects objectAtIndex:1];

A colleague asked me to explain why the above is any better than this line:

id foo = [soundEffects objectAtIndex:1];

Well, functionally, it's no different.

My arguments for the former are as follows:

  1. self.soundEffects tells every other coder working on the code that this is an iVar, not a locally scoped variable.

  2. If we ever needed to, we could put custom logic in the soundEffects getter accessor.

  3. For no concrete reason, it "feels" like the right thing to do after working in Obj-C for a year.

He accepts arguments #1 and #2 as valid, but also gives the counterpoint:

  1. Isn't this just code bloat?

  2. Shouldn't a class be allowed to talk to its own iVars directly without having to call a method (the getter) on itself?

Any takers?

+2  A: 

using something like self.variable = nil makes the variable go through its setter and therefore gets memory managed for free. if you just use variable = nil for instance in a dealloc method, it will cause a leak since it is not actually going through the synthesized setter for the variable and decreasing the retain count. See this post memory leak with self.

For this reason, it is advisable (i believe) to always use self. when dealing with instance variables that you own as far as memory management is concerned.

Jesse Naugher
Thanks. We're well convinced about using self.property = nil instead of just property = nil. What I'm more interested in is the getter side of things. Any thoughts?
phooze
Its actually considered to be better to release your instance variables directly in dealloc rather than using property accessors.
Luke Redpath
Luke- would love to know why. Any ideas?
phooze
@phooze I *think* the issue is that property accessors can potentially trigger notifications, which is undesirable in an object you are in the process of deallocating.
walkytalky
walkytalky - aha, thanks. Always wanted to know that.
phooze
+1  A: 

I have personally settled on using an underscore prefix for ivars, and this kind of sythesize

@synthesize name = _name;

That way I don't mix them up. The major issue with not using self is that this code

_name = ...

is very different from

self.name = ...

When the @property uses the retain option. The first does not retain the object, and the second calls the synthesized setter that retains.

The only time it makes a big difference is with assigning, so I tend to use self. all of the time so I make sure I do it on assigns.

Lou Franco
Be careful if you're using that leading underscore with Cocoa frameworks. Apple reserve the use of the leading underscore for class iVars and you may run into trouble picking an identical name for an iVar of a subclass.
ohhorob
+1  A: 

Your point 1 is not quite right: self.soundEffects is not an ivar, although it may happen to give you something which is -- as it does in the case of your synthesized NSArray, at the moment.

This in turn implies that your point 2 is the crux of the matter -- if you route all access through the accessor, then everything is nicely encapsulated and you're free to modify the implementation later without having to worry about side effects.

It's also good practice for when you use the mutator, so you maintain consistent memory management.

For the most part, I'd say it's advisable to route through self.property for everything that is a property, and restrict direct ivar access to things which are strictly internal. However, I'll admit that in some cases -- especially for things that don't use retain/copy semantics -- it can be more of a style preference.

walkytalky
"Strictly internal" is I guess the issue. When using a getter inside of the same class, some can argue that a class needs to know its own implementation. A functional programming fan, I prefer to keep it even tighter than that (methods are as self-reliant as possible), so that's why I prefer the self.something syntax.
phooze