views:

96

answers:

3

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"; 
+1  A: 

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
Perhaps Cocoa core developers don't like their code SHOUTING AT THEM.
dreamlax
+2  A: 

Personally, I tend to use the lowercase 'k' prefix because it's a little nicer on the eyes, and because it's clear that it's a constant (I never have any variable names that start with a 'k'). Also, Apple uses the 'k', so I just stuck with it.

I also tend to stay away from all-caps constants, because I've reserved all-caps for macro definitions. Just my two cents.

itaiferber
+ 1 Stay away for all caps : that's for #define constant and macro. - 1 'k' prefix is used in C frameworks by Apple and the 'NS' (or what ever you want) is used in the Obj-C frameworks.
gcamp
+1  A: 

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

To sum up:

  • Start with a two-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.

hgpc