2

I'm little bit confused about constants naming.

kName
NameKey
NAME_KEY

Definitely usage of these fashions?

ento
  • 5,801
  • 6
  • 51
  • 69
user500
  • 4,519
  • 6
  • 43
  • 56

6 Answers6

2

According to Coding Guidelines for Cocoa, constances created with const shoul look like this:

The format for const constants is exemplified by the following declaration:

const float NSLightGray;

To see guidelines click here

Apan
  • 1,027
  • 2
  • 10
  • 27
1

I suggest to pick your own project or company prefix (like NS or AV above) and stick with it. So, if your prefix is AB:

  • ABMegaTopKey for const variables
  • AB_MEGA_TOP_FLAG for #defines

Apple uses different style depending, I guess, on who wrote the project and when was it written.

djromero
  • 19,551
  • 4
  • 71
  • 68
0

kName is used for string or variable constants. NAME_KEY is used for #define constants.

Michaël
  • 6,676
  • 3
  • 36
  • 55
  • That doesn't seem right, look at AV Foundation Error Constants: "NSString *const AVErrorDeviceKey;" for example, they are not prefixed. – Nick Weaver Mar 07 '11 at 15:30
  • AV is the prefix of the framework, but in your code, your constants are never named like that. – Michaël Mar 07 '11 at 15:34
0

kName is Apple style, while NAME_KEY is alot more widespread among other languages. Doing a lil more research: k is hungarian notation

Community
  • 1
  • 1
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
0

I always use the third style coming from a mainly C background, although I will usually put a prefix on them for frameworks e.g.

NSString* const JP_NAME_KEY = @"name";
JeremyP
  • 84,577
  • 15
  • 123
  • 161
0

there are a few visible 'cultures' in naming constants across the libraries.

I typically declare constants quite literally, using upper Camel with underscores to separate the longer names:

  • Prefix with organization code
  • then prefix with library (if applicable)
  • then prefix it by type or category

    ORGConstantName

    ORGLibrary_ConstantName

    ORGLibraryType_ConstantName

    ORGLibraryCategory_ConstantName

it's verbose, but it avoids clashes, and it's really clear when the constant applies only to a certain scope, type, library, or other context.

whatever you settle on, just make sure it's consistent.

justin
  • 104,054
  • 14
  • 179
  • 226