views:

448

answers:

4

I'm teaching myself Objective-C and I noticed in a lot of books and examples the use of 'k' and camel-casing in constant definition, e.g.

#define kMyConstant 0

What is the significance of the 'k'? Is this unique to Objective-C style, or common to C in general? Why the deviation from (what I've always thought as a best practice) K_MY_CONSTANT style?

Thanks.

+2  A: 

All caps (as in K_MY_CONSTANT) normally denotes a macro - not necessarily a constant. It's generally important for macros to stand out because they clobber all namespaces.

The 'k' convention is used (but not universally) for non-macro constants in C/C++ and probably other languages. I suspect 'k' is used because 'c' is already often used - either to denote a class name (as in CString) or that a variable is a counter.

Michael Burr
+8  A: 

It was mentioned once before in the SO question, Lower case "k" in Cocoa.

It is a general programming notation not specific to Objective-C (i.e. Hungarian Notation) and the "k" stands for "constant".

If you look at the Google cache of Google's guidelines for Objective-C you can see that they used to include it in their styleguide:

Constant names (#defines, enums, const local variables, etc.) should start with a lowercase k and then use mixed case to delimit words, i.e. kInvalidHandle, kWritePerm.

Though a pain to write, they are absolutely vital to keeping our code readable. The following rules describe what you should comment and where. But remember: while comments are very important, the best code is self-documenting. Giving sensible names to types and variables is much better than using obscure names and then trying to explain them through comments.

But it has since been removed in the live version of the document. It should be noted that it goes against the the Official Coding Guidlines for Cocoa from Apple.

Simucal
Very interesting. "In general, don’t use the #define preprocessor command to create constants. For integer constants, use enumerations, and for floating point constants use the const qualifier, as described above." Thanks!
Typeoneerror
PS, sorry about the repeat question. I swear I searched first! ;)
Typeoneerror
@TypeOneError, I didn't mention it to point out a repeat question. I don't necessarily think it is a repeat of that exact question. I was just pointing you to another resource to read. Your question is a little more broad and probably a better one.
Simucal
+2  A: 

The kMyConstant thing goes back to the very old Mac programming days (80s/90s being "very old"). This was a standard Apple used and many Mac programmers also used; this took the place of MY_CONSTANT-type definitions.

I would imagine the authors you are reading were Mac Pascal/C programmers for the old Mac Toolbox prior to being NeXT/Cocoa programmers. The Mac libraries used to be called the "Toolbox"--this was the set of APIs and libraries that turned into Carbon over time.

I still use kMyConstant sometimes; drives my old school Unix programmer colleagues nuts. :-)

Mitch Haile
+1  A: 

I think use of a leading "k" is because all languages pronounce it as a hard consonant and thus it reminds people of the english pronunciation of "constant" :-)

No reserved words start with k so it is easier as a search target.

Alternatively, from a graphical aspect, as a leading character it provides a very clear flag in front of the rest of the name. Leading "c" is less obvious and might be used to indicate roles (such as in Hungarian notation) or classes (if in a case-insensitive language).

MY_CONSTANT is pretty well agreed as being for macro-based constants. It is sometimes important to know that a constant is defined by a macro, for example that implies it is based on literals and thus from a limited range of data types, as well as being defined at a global scope and thus (as Michael Burr pointed out) overrides any local namespaced constants.

Andy Dent