views:

53

answers:

3

I know that constants start with the k prefix, but does anyone have their own constant prefix, so they can easily get completion on their constants, instead of Apple's?

I use a three letter prefix for my classes because of my company name, let's pretend it's OMG. I tried prefixing my constants with omgkConstantName but that isn't very satisfying. I was also toying with either kk or ok (the o is from the OMG.) Or maybe I should do kOMGConstantName, that seems more Cocoa-ish?

It seems really useful to quickly get to your own constants, the same way you might want to get to your own classes. Does anyone do this? Is it terrible Cocoa style?

Also, is there a good naming convention for static variables you would only use in one class, like keys for a JSON dictionary? Should they have the k? Should they start uppercase, or do they follow normal case conventions?

// Static Variables
static NSString *searchTextKey = @"searchText";
static NSString *searchResultsKey = @"searchResults";
(more)
A: 

I normally make constants Pascal case.

If you choose to follow the 'namespacing convention' of putting a prefix on everything then you'd use that as well.

So I'd normally do

static NSString *SearchTextKey = @"searchText";

you may choose to namespace it though which would have it be (assuming your prefix is ZK)

static NSString *ZKSearchTextKey = @"searchText";

In general I only namespace things that are used in multiple projects, following the google recommendation (or at least it was when I was settling upon my naming convention).

The most important thing though is that whatever convention you settle on. Stick to it and be consistent (at least within a given project). At a certain point second guessing yourself is just going to waste time.

Bryan McLemore
Definitely agree about consistency. I'm just starting a new multi-Xcode-project project and have the opportunity to set the style convention. I think I'm leaning towards `kOMG` as the prefix. I like being able to tell between constants and classes.
zekel
+1  A: 

I try to consistently use kJAFoo (or kXXFoo where XX is a project prefix) for my public constants – especially actual consts which are exported symbols – but generally use kFoo for enums or static consts inside an implementation file. Similarly, I use sFoo for static variables and gJAFoo in the rare cases where I use exported globals.

None of these cases are as important as namespacing classes (and methods in categories on imported classes), though, since most types of conflicts will emerge at compile or link time rather than runtime.

Ahruman
This is more or less how I do it. It's subjective though.
zekel
A: 

I usually follow the same prefix/capitalisation convention as classes, so where Cocoa has NSTouchPhaseBegan I might have GLTuneNameKey.

Graham Lee