Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of starting constants with 'k'?

Tags:

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.

like image 303
typeoneerror Avatar asked Feb 01 '09 02:02

typeoneerror


People also ask

Is K considered as constant?

A constant function is a linear function whose general format is y = mx + k, where m and k are constants.

What is the purpose of constants?

Constants are used in programming when a value needs to be available to the program, but it will not change during the execution of the program. What is a variable? A variable is a named container, held by the computer in a memory location.


1 Answers

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.

like image 116
mmcdole Avatar answered Oct 03 '22 01:10

mmcdole