Borkware Miniblog

September 19, 2010

Stay out of Apple’s Namespace

Filed under: Uncategorized — Mark Dalrymple @ 5:00 pm

I’ve been noticing a disturbing trend lately.

A friend asked me recently, “Would naming your own Objective class prefix with NS be an issue with Apple or that is just not good in general?”.

An industry luminary retweeted an otherwise useful macro:

#define CFAutorelease(cf) ((__typeof(cf))[NSMakeCollectable(cf) autorelease])

And recently I saw a blog post where they make their own CLLocationCoordinate2DMake, and then go through contortions to fix things when Apple introduced their own version in a later SDK.

Don’t do this! Stay out of Apple’s Namespace.

There are two big reasons to do this:

  1. Apple changes things. Often. If you add a function, macro, class, or struct in their namespace, you may break. People who use your code may break. People who cut and paste your tweets may break.
  2. It’s confusing for the person reading your code 3 years from now. Or your new hire. Or someone who uses part of your open source library. Or you include it into a posting on a mailing list or stack overflow. “CFAutorelease, I didn’t know that existed.” “uh, sorry, it really doesn’t.”

In general, you should stay out of the namespaces of any code you’re using, such as making your own GTM-named classes if you’re using the Google Toolbox For Mac. That could break you the next time Google revs that library.

CF, NS, CLLocation, etc. Those aren’t your playground.
That’s Apple’s turf. Which kind of makes me sad, since I’m doing stuff for Cycling Fusion now, and using the obvious prefix would be a disaster.

Pick your own prefix and use that. BWCFAutorelease, BWCLLocationCoordinate2DMake. This shows that yes, this is not part of Apple’s API, but also clearly shows the intent of the symbol. Without you being broken by Apple in the future.

About these ads

7 Comments »

  1. Lucky for Cocoa developers there is an informal place to ‘register’ your prefix. If you write code that goes into the public space you should stake a claim to your prefix over at cocoadev.com . Then you have a better chance of people not colliding into ‘your’ namespace.

    Comment by Michael — September 22, 2010 @ 10:29 pm

  2. Actually, as of WWDC ’10 at least, Apple’s official recommendation is that you don’t use *any* two-letter prefixes in your code, in case Apple decides to release a new framework that stomps on your prefix that way. This is mentioned in the WWDC Session 130 video, I don’t know if there’s a documentation release that mentions this or not.

    Comment by Geoff Pado — September 23, 2010 @ 7:45 am

  3. In my ears “CYFU” is a pretty cool prefix ;D

    Comment by Javos — September 23, 2010 @ 7:57 am

  4. I think expecting us to avoid all two-letter prefixes isn’t realistic. I also think that’s much less of an issue than from the outset intentionally naming functions to look like Apple calls. I have GRWorkout and GRHeartRateMonitor classes, which I doubt Apple will be duplicating.

    Comment by Mark Dalrymple — September 23, 2010 @ 8:04 am

  5. You can file this under interesting but useless but it’s a little disheartening to still have this problem when there are other tools that support namespaces. I’m having flashbacks to the days of kilometric identifiers because we had one global namespace.

    Comment by Bong Munoz — September 24, 2010 @ 3:39 pm

  6. I wouldn’t call it “useless” since violation of Apple’s namespace can have a non-trivial, negative impact in the future. Yes it’s sad we don’t have better tools in this respect, but for right now it’s what we have.

    Comment by Mark Dalrymple — September 26, 2010 @ 12:53 pm


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.