Development Tips (Issue 1)

Friday, March 7th, 2008

Recently, I’ve been collecting a bunch of best practices and style tips that I’ve been picking up. With ObjC2 and the iPhone SDK, the Cocoa landscape is changing quite rapidly, and a lot of my old conventions need to be updated and rethought given new modern tools.

Here’s a bunch of things I’ve found myself doing lately. I’ll try and write more of these down and publish them more often — positive feedback greatly appreciated. Thanks to the (now defunct) slerp iPhone tumblr blog for some of the inspiration.

  • Try switching Xcode to use either the Midnight or Dusk color themes. On a computer screen, light text with a dark background is often easier to read than the other way around. Here’s a preview of Dusk:

    Picture 1.png

    Isn’t that purdy?

  • Look at using sigils on your member variables: i.e.:

    @interface Foo : NSOBject {
        NSArray *mFooArray;
    - (void)addFoo;
    @implementation Foo
    - (void)addFoo
        [mFoo addObject:[FooGetter retrieveFooFromFooLand]];

    If you ever do access storage, you’ll know these are member variables. Same for static (sSomeStaticVar) and global (gEvilGlobalVariable). This is mitigated somewhat by Xcode’s highlighting (which I wish wasn’t so buggy — I filed this radar about it).

  • If, on the other hand, you’re using ObjC2, and you’re using properties, use as the accessor at all times. It Looks cleaner and is much less ambiguous. I still name my members with m sigils though, i.e.:

    @interface SwimmingPool : NSObject {
        float mDepth;
    @property (nonatomic) float mDepth;
    - (BOOL)isItSafe;
    @implementation SwimmingPool
    @synthesize depth = mDepth;
    - (BOOL)isItSafe
        // XXX Actually implement government safety standards... should be good for now though.
        return self.depth < 6.0f;

    Not sure if I like this, but it definitely is more explicit and if I do need to access members directly, I know what I’m doing.

  • Please please don’t use _ at the beginning of members or method names to mean “private.” That’s reserved by Apple, and I’ve had a collision on a method name bite me in the ass before. Granted, I figured it out pretty quick and renamed my method, but still. Don’t do it. Instead consider using m sigils for members. For “private” methods, I don’t really see much of a point to the _ prefix anyway — there’s no actual enforcement of this by anything. I have seen a few people postfix _ to private method names, i.e.

    - (void)somePrivateMethodWithArgs_:(id)arg andStuff:(id)arg2

    Not entirely sure how I feel about that. Comments welcome.

  • Strings should probably be copy properties, i.e.

    @property (nonatomic, copy) NSString *label;

    If the copying becomes a bottleneck (which is possible on devices with limited amounts of memory, like a cell phone), you could remove it. I would advocate correctness over speed here — it’s better to be able to optimize later than have to hunt down weird bugs because you mutate something you shouldn’t have. Again, comments welcome either way — I’m certainly not adamant about this one quite yet.

  • You may have noticed I’m using the nonatomic attribute for properties. This is because I’m working primarily in a “managed memory” (i.e., old school) environment. If you have switched to garbage collection, you should not include nonatomic — the documentation states that most of the time synthesized accessors in GC mode don’t require the type of overhead that the old-style ones do. That overhead appears to be a lock plus retain + autorelease. “Most of the time” is a pretty important caveat, by the way.

I’ve got a bunch more tips, but I’ve got to save something for issue 2!

I’m very open to feedback on any of these tips — feel free to comment if you don’t like what I’m doing, but I would ask that you briefly explain why your solution is better.


  1. Joe Heck replied on March 12th, 2008:

    I understood from Jens that _something member variables were perfectly acceptable, and even encouraged. But for member variables only, NOT methods, as Apple uses the _methodName for private methods internally.

  2. Colin replied on March 12th, 2008:

    @JoeHeck: Just from a visual perspective, I like leaving the _ prefix for only internal apple stuff. Just for consistencies sake.

  3. Alex Jacque replied on May 24th, 2008:

    Hey Colin, long time no… well, anything really. Glad to see you’re doing well for yourself. Also, I definitely appreciate this article since I’m just now starting to get into Obj-C because of the iPhone SDK and I generally get confused about a lot of this stuff (but I’m learning!).