Dev Juice: What’s the deal with k for constants?

Dear Dev Juice,

What’s the deal with the k in #define kFilename. Is it German? Does it stand for konstant? What gives?

Pure Fusion

Dear Pure Fusion,

The k prefix seems to originate in a naming system called Hungarian Notation, developed by Charles Simonyi at Xerox PARC. The idea behind this system is that the name of a variable should reflect its semantics.

Wikipedia offers a sense of these naming conventions, such as arru8NumberList, meaning the variable is an array of unsigned 8-bit integers.

Apple follows a similar but more practical convention. Like the original Hungarian Notation, it encodes opaque types, but it does so with a distinctly Apple spin and without all the silly made up non-obvious naming bits like “arru8”.

As you’ve already noted, Apple’s “k” prefix indicates constants. Typically, the k prefix is followed by type, followed by a more general indication of the item’s use. A typical Apple symbol, such as kCFCharacterSetWhitespace can easily be broken down as k (constant) + CFCharacterSet (opaque type) + Whitespace (unique role).

Hungarian Notation’s goal of encoding semantics into symbols has gone in and out of fashion over time. Some devs love it, others hate it. Most fall in the middle, as does Apple’s usage.

Being able to instantly identify the role of a symbol as a constant is a win, but a mindless adherence to pedantry in unnatural naming can become a stumbling block. Fortunately, Apple seems to have chosen its path well and its naming schemes are easy to follow while remaining semantically rich.

Happy developing!

Dev Juice: What’s the deal with k for constants? originally appeared on TUAW – The Unofficial Apple Weblog on Fri, 05 Aug 2011 10:00:00 EST. Please see our terms for use of feeds.

Source | Permalink | Email this | Comments

Leave a Reply

Your email address will not be published. Required fields are marked *