[ACCEPTED]-Objective C - Why do constants start with k-constants
Starting constants with a "k" is a legacy 12 of the pre-Mac OS X days. In fact, I think 11 the practice might even come from way back 10 in the day, when the Mac OS was written 9 mostly in Pascal, and the predominant development 8 language was Pascal. In C,
#define'd constants 7 are typically written in ALL CAPS, rather 6 than prefixing with a "k".
As for where to 5
#define them where you're going to 4 use them. If you expect people who
#import your 3 code to use the constants, put them in the 2 header file; if the constants are only going 1 to be used internally, put them in the
Current recommendations from Apple for naming constants don't include the 'k' prefix, but 3 many organizations adopted that convention 2 and still use it, so you still see it quite 1 a lot.
The question of what the "k" means 7 is answered in this question.
And if you intend for files 6 other than that particular
.m to use these 5 constants, you have to put the constants 4 in the header, since they can't import the 3
You might be interested in Cocoa Dev 2 Central's C tutorial for Cocoa programmers. It explains a lot of the core 1 concepts.
k prefix comes from a time where many 61 developers loved to use Hungarian notation in their code. In 60 Hungarian notation, every variable has a 59 prefix that tells you what type it is.
pSize would 58 be a pointer named "size" whereas 57
iSize would be an integer named "size". Just 56 looking at the name, you know the type of 55 a variable. This can be pretty helpful in 54 absence of modern IDEs that can show you 53 the type of any variable at any time, otherwise 52 you'd always have to search the declaration 51 to know it. Following the trend of the time, Apple 50 wanted to have a common prefix for all constants.
Okay, why 49 not
c then, like
c for "constant"? Because 48
c was already taken, in Hungarian notation,
c is 47 for "counter" (
cApple means "count 46 of apples"). There's a similar problem 45 with the
class, being a keyword in many languages, so 44 how do you name a variable that points to 43 a class? You will find tons of code naming 42 this variable
klass and thus
k was chosen,
k as 41 in "konstant". In many languages 40 this word actually does start with a k, see here.
Regarding 39 your second question: You should not use 38
#define for constant at all, if you can avoid it, as 37
#define is typeless.
const int x = 10; // Type is int const short y = 20; // Type is short const uint64_t z = 30; // Type is for sure UInt64 const double d = 5000; // Type is for sure double const char * str = "Hello"; // Type is for sure char * #define FOO 90
What type is FOO? It's some 36 kind of number. But what kind of number? So 35 far any type or no type at all. Type will 34 depend on how and where you use
FOO in your 33 code.
Also if you have a fixed set of numbers, use 32 an
enum as then the compiler can verify you 31 are using a valid value and enum values 30 are always constant.
If you have to use a 29 define, it won't matter where you define 28 it. Header files are files you share among 27 multiple code files, so if you need the 26 same define in more than one place, you 25 write it into a header file and include 24 that header file wherever that define is 23 needed. What you write into a code file 22 is only visible within that code file, except 21 for non-static functions and Obj-C classes 20 that are both globally visible by default. But 19 unless a function is declared in a header 18 file and that header file is included into 17 a code file where you want to use that function, the 16 compiler will not know how this function 15 looks like (what parameters it expects, what 14 result value it returns), so it cannot check 13 any of this and must rely that you call 12 it correctly (usually this will cause it 11 to create a warning). Obj-C classes cannot 10 be used at all, unless you tell the current 9 code file at least that this name is the 8 name of a class, yet if you want to actually 7 do something with that class (other than 6 just passing it around), the compiler needs 5 to know the interface of the class, that's 4 why interfaces go into header files (if 3 the class is only used within the current 2 code file, writing interface and implementation 1 into the file is legal and will work, too).
k for "konvention". Seriously; it is just 11 convention.
You can put a #define wherever 10 you like; in a header, in the .m at the 9 top, in the .m right next to where you use 8 it. Just put it before any code that uses 7 it.
The "intro to objective-c" documentation 6 provided with the Xcode tool suite is actually 5 quite good. Read it a few times (I like 4 to re-read it once every 2 to 5 years).
However, neither 3 it nor any of the C books that I'm aware 2 of will answer these particular questions. The 1 answers sort of become obvious through experience.
I believe it is because of the former prevalence 2 of Hungarian Notation, so k was chosen because 1 c stood for character. ( http://en.wikipedia.org/wiki/Hungarian_notation )
More Related questions