10

Objective-C doesn't have namespaces, and many (such as CocoaDevCentral's Cocoa Style Guide) recommend prefixing your class names with initials to avoid namespace collision.

Quoting from the above link:

Objective-C doesn't have namespaces, so prefix your class names with initials. This avoids "namespace collision," which is a situation where two pieces of code have the same name but do different things.

That makes sense, I suppose. But honestly, in the context of a relatively small app (say, an iPhone game), is this really an issue? Should I really rename MyViewController to ZPViewController? If not, at what point do namespace collisions really become a concern?

zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • "Objective-C doesn't have namespaces, so prefix your class names with initials." Wow what an awesome band-aid. I'd love to get into Objective-C, but the lack of namespaces is absolutely dumb. – The Muffin Man Apr 25 '12 at 23:52
  • 1
    @Nick Since I wrote this nearly two years ago I've published five apps on the app store, and never had this issue. The lack of namespaces really seems not to be an issue, at least it hasn't been for me (the answers below pretty much back that up, as well). – zpasternack May 06 '12 at 08:15
  • So lets say that you have a class that clashes with another, is there anyway to choose which one you want, or are you forced to go back and rename? – The Muffin Man May 07 '12 at 17:34
  • @Nick I suppose you'd have to rename one, but again, I've never had this issue. Also, Xcode makes it pretty easy to rename classes, just right-click a class name and choose Refactor->Rename. – zpasternack May 08 '12 at 00:22
  • 2
    If you are planning to provide you own framework for distribution, there is a very good chance that you will face a name clash. If you are just going to use some other frameworks, obviously, you will not have a name clash because you have the choice of changing you class name. Adding initials is really not a foolproof solution but it is the best we got at the moment. – Deepak G M Feb 05 '13 at 04:49

4 Answers4

20

If you're writing an application that uses some set of libraries, then you already know what your namespace looks like and you just need to select names that do not conflict with existing available functions.

However, if you are writing a library for use by others, then you should pick a reasonably unique prefix to try to avoid name collisions with other libraries. With only two characters there may still be name collisions, but the frequency will be reduced.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

Small apps shouldn't use up all the good names, so won't have a problem with namespaces.

But it is a good idea to get used to the style that languages are generally written in. It makes it easier to read other people's code, and for others to read yours.

E. g., use camelCase variables in Java, but CamelCase vars in C#, hyphen_separated_names in C, etc.

It will make it easier for you to learn in the long run.

UncleO
  • 8,299
  • 21
  • 29
0

I have read (but haven't verified) that Apple has private classes in their frameworks that don't include any prefixes on the names. So if your application classes' names have no prefixes, you risk colliding with those.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
0

I've worked with repositories where classes were not prefixed. (Or only some of the classes were prefixed.)

One thing that I found painful is it's sometimes hard to tell if code was written by someone inside or outside the company. Using a consistent prefix makes it immediately obvious for someone reading the code for the first time.

Keep in mind that code will be read many more times than written.

Also, it can definitely come in handy when using tools like grep and sed.

funroll
  • 35,925
  • 7
  • 54
  • 59