40

What's the naming convention for constants in Objective-C (or most widely used way to name them)?

Is there a different criteria for extern constants?

Some styles I have seen:

NSString* const kPreferenceFirstRun = @"FirstRun";

// Replace "XY" by a prefix representing your company, project or module
NSString* const XYPreferenceFirstRun = @"FirstRun"; 
hpique
  • 119,096
  • 131
  • 338
  • 476

2 Answers2

40

After a bit of googling I've found the official coding guidelines for Cocoa.

To sum up:

  • Start with a two- or three-letter prefix in ALL-CAPS
  • Rest in UpperCamelCase
  • Same criteria for extern constants

I agree with itaiferber that the k prefix style is clearer and also much more useful for autocompletion. It would be interesting to know if this style is more popular than the official guidelines.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
hpique
  • 119,096
  • 131
  • 338
  • 476
  • 1
    I think the benefit of a two-letter prefix vs k prefix is twofold: 1) the two letter prefix allows you to easily filter your auto-complete choices to only the group of constants you are looking for 2) when looking at code, you know exactly what domain the constant came from. Imagine if you are working on a project that uses multiple sibling projects in Xcode and they all use k as the only identifier for constants -- you'd be stuck sorting through a list of all constants from all sibling projects. Alternately, if each project used its own two letter domain prefix it becomes much easier. – memmons Mar 12 '12 at 18:05
  • Very interesting point, and very interesting answer. And would it be better to have the constants declared on each header file (MyClass.h), or would it be better to have a global .h file (Constants.h or similar) exclusively containing constant declaration? I've seen this last practice in many C/C++ projects, so why not in objC – voghDev Jul 17 '14 at 08:10
  • It's worth noting that a lot of Apple libraries are using the kPrefixConstantNamingConvention. So whether you go for one or the other is mostly up to personal preference. I normally use the kPrefix, but I also tend to avoid global constants, so namespacing those constants become less important. – Erik B Nov 12 '15 at 12:57
5

it's seems to me, the best practice is to name constants in upper-case. but cocoa-core developers don't seem to share my opinion)) they use CamelCase for constants

heximal
  • 10,327
  • 5
  • 46
  • 69
  • 44
    Perhaps Cocoa core developers don't like their code SHOUTING AT THEM. – dreamlax Oct 30 '10 at 10:41
  • 3
    The all-uppercase technique was used in C because it made macros (not constants, macros) stand out. Because macros are such a dangerous construct in C, this was a very useful technique because it would draw your eye to likely trouble spots. Unfortunately, this convention has been misguidedly copied in languages that have perfectly good constant definition mechanisms built into the language itself. – Ferruccio Mar 10 '11 at 19:35
  • k is super easy to recognize, and as @hgpc mentioned, works well for autocomplete. Preprocessor macros use the all caps, so maybe it is nicer to use them for that alone. – jpswain Aug 05 '11 at 21:57
  • 7
    All upper case constants in obj-c is not a good idea because that is typically reserved for #defines – memmons Mar 12 '12 at 17:59