views:

64

answers:

3

Hey,
If my application is called "Media Player", is it a best practice to name classes: MPSong, MPSinger, MPAlbumsViewController ... ?

A: 

All capital two-letter prefixes are reserved by Apple, so you shouldn’t use those. But otherwise there is no definite answer to this question - it’s a matter of personal taste. I personally use a prefix for library classes but not for application classes.

Sven
Apple [promote](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281-1002226-BBCJECED) the use of two-letter capital prefixes.
dreamlax
@dreamlax: At WWDC '10, one of Apple's engineers advised against prefixing your own classes, saying that Apple reserved 2-letter prefixes for its own use. See https://deimos.apple.com/WebObjects/Core.woa/BrowsePrivately/adc.apple.com.4092349126.04109539109.4144345635?i=2082492890
mipadi
@mipadi: I was going to go this year but my boss said it wasn't necessary, after an Apple Engineer told him that it wasn't necessary (we only deal with a very specific part of Mac OS X development which hasn't changed much since 10.4). Time to sit down and watch all the stuff I missed.
dreamlax
@dreamlax: I actually only know this because I made a similar comment about Apple's stance on 2-letter prefixes on SO, and someone else pointed me to this video. I had the link handy so I thought I'd repost it. (It's a minor part of the video, but the whole talk is pretty helpful if you're a Mac or iPhone dev.)
mipadi
@mipadi: Never mind what some Apple engineer said at WWDC, Apple's *official* guidelines as linked by Dreamlax say to use a two or three capital letter prefix. Until they change, that is the correct advice.
JeremyP
@sven: can you give a reference to a document saying Apple reserves all two letter prefixes?
JeremyP
@Sven again: forgot to say, I follow the same practice as you. Classes in my apps don't have prefixes, but classes in my frameworks do.
JeremyP
@JeremyP: Can’t find an official document that says so. I guess I must have got this info also from the WWDC video. But still it should be common sense not to use two-letter prefixes - I’ve seen it before that people had to rename their classes because Apple released a new framework with exactly the same prefix. You actually see the exact issue in the original question. El Gusto wanted to use the prefix MP which is used by Apples MediaPlayer framework, probably because he didn’t know. Just better to avoid this by avoiding two-letter prefixes at all.
Sven
A: 

I don't know if the way I name classes is a best practice or not, but if I have a class that is unique to the project I just give it a meaningful name and start with a capital letter. However if it is a class that I intend to reuse I may give it a prefix like the initials of my company or myself so I see the class is meant for reuse.

Another reason for a prefix would be if you have the same name because of similar titling in the same project but have different functionality and run the risk of becoming confusing. For example if you decide to make a class called state as in the state in which you live. Then you wish to create a class called state meaning the current state of you application. It would be a good idea to use a prefix like ADState and APState (AD for address and AP for application). In other frameworks they have namespaces that do the same job.

Rob
+2  A: 

Apple's Coding Guidelines for Cocoa has lots of advice along these lines.

In short, using a prefix for class and protocol names is encouraged, especially if you're developing a framework. However, Apple already uses the MP prefix for its MediaPlayer framework on the iPhone, so you probably want to pick another.

Robot K