Private Properties

Objective-C 2.0 properties are pretty useful.

They not only save you time, but also help “automate” memory management on the iPhone. They save you from screwing up your accessors.

Another benefit, is the orthogonal relationship with dot notation. Dot notation short hand makes it extremely quick to change the state of an ivar with minimal keystrokes.

There are (too) many debates about whether this syntax is “ugly”, “tacked on”, etc… This post makes a great case for using dot notation to differentiate the intent of your code.

In fact, properties used in concert with dot notation are so convenient that there is little excuse to ever access instance variables directly. Another bonus is the “automatic” memory management. Well as automatic as you can expect in a reference counting situation.

You may be opposed to creating a property for an ivar you want to be private since doing so allows unrestricted access. This creates an encapsulation problem, but one that is easily circumvented with another Cocoa trick: anonymous categories.

By declaring your private property within an anonymous category in the implementation file, you effectively hide it from other classes.

It is still true, that you can peek in the implementation and find these hidden properties. But it will be readily apparent to you, or anyone else, that they should be left alone since they reside within the implementation file.

I use this technique to clean up my interface files (along with placing private methods in the category as well). This gives your classes a nice, neat, and more importantly, easy-to-understand interface.

Another issue is when you need to react to state changes of an ivar. This can be mitigated using KVO. By observing properties, you can react easily such changes.

Do you use dot notation or do stick with brackets? Do you create properties for all of your ivars or do you prefer to handle their memory management in the implementation?

5 Comments

  1. Posted July 16, 2009 at 7:11 am | Permalink

    I generally stick to dot notation as much as possible since I find using brackets clumsy. That is mostly because of what I was used to in other programming languages I guess.

  2. Arne
    Posted July 16, 2009 at 8:24 am | Permalink

    Personally, I really like the dot-syntax for properties. The convenience of visually separating properties from method-calls outweighs the slightly alien look of the dot-syntax in obj-c 2.0 :)

  3. Posted July 16, 2009 at 3:48 pm | Permalink

    “Another issue is w hen you need to react to state changes of an ivar. This can be mitigated using KVO.”

    Can’t you simply implement the setter method and handle it right when it changes?

  4. Corey
    Posted July 16, 2009 at 3:52 pm | Permalink

    @Matt
    I like the brackets aesthetically, it is just when you forget one that bothers me. I did install Bracket Matcher which helps, but you still lose some code sense.

    @Arne
    I didn’t use Objective-C too much before 2.0, so dot syntax doesn’t really look out of place to me. In fact all of Objective-C was very “alien” to me at first. As with anything, I think that over time you get used to it.

  5. Corey
    Posted July 16, 2009 at 3:55 pm | Permalink

    @Chris Yes you can, but using KVO seems even easier to me than implementing the setter. This was one of the reasons that properties were made, to stop us coders from writing error prone getters and setters. You can always choose the method that works best. I use KVO since it protects me from myself.