Naming
Names of files
NF1. The name of the header file is the same as name of the class or interface/protocol it defines.
Example:
The header file for the HPLRequest class has the name HPLRequest.h.
The header file for the IHPLResponse Objective-C protocol has the name IHPLResponse.h.
|
Status: Rule
NF2. The name of the implementation file is the same as the name of the class it implements.
Example:
The implementation file for the Objective-C class HPLResponse has the name HPLResponse.m.
|
Status: Rule
NF3. The name of file containing Objective-C categories for existing class or classes has the 'Extensions' or 'Additions' suffix.
Example:
The files containing extensions for the NSSet class have the following names NSSetExtensions.h and NSSetExtensions.m.
The files containing various extensions for the Foundation class may have the following names HPLFoundationAdditions.h and HPLFoundationAdditions.m, where 'HPL' is a project-specific prefix (Hp PaLm).
|
Status: Recommendation
Prefixes
NP1. All prefixes used in names of entities are followed by an uppercase letter.
Status: Rule
NP2. Names of types start with a prefix specific for a particular project.
This prefix is used primarily to distinguish types among other entities taking into account their names only.
Example:
@interface HPLApplicationSetting
{
}
@end
Notes:
The prefix for all HP_Palm projects should be 'HPL'. HP_Palm libraries could have their own prefixes.
Status: Rule
NP3. Names of Objective-C protocols start with capital ‘I’.
This prefix is used primarily to distinguish interfaces looking at their names only.
Example:
@protocol IHPLSettingsProvider <NSObject>
@end
|
Status: Rule
NP4. Names of constants start with lowercase ‘k’.
The same requirement is also applicable for static constants.
Example:
NSString *const kHPLFieldDelimiter = @”:”;
static const NSUInteger kHPLMaximalCacheAmount = 30;
|
Status: Rule
NP5. Names of static variables start with lowercase 's'.
static NSDictionary *sTitleMap = nil;
Status: Rule
NP6. Names of private member variables in Objective-C class start with a lowercase letter.
Example:
@interface HPLApplicationSettings
(
@private
NSDictionary *settings;
id<IRHPLSettingsProvider> settingsProvider;
}
@end
Exceptions:
The only exception is for cases when a name of private member variable starts with a well-known acronym, then an uppercase letter is to be used.
Example:
@interface HPLBanner : NSObject
---
@private
NSString *HTMLBody;
NSURL *URL;
@end
Status: Rule
NP7. Do not use special prefixes for the private member variables.
Example:
@interface MPXCore : NSObject
{
@private
NSDictionary *mpxSettings; // Wrong: prefixes are not allowed
}
@end
Status: Rule
NP8. Names of local variables in Objective-C start with 'the'.
Prefixes are primarily used to avoid interference with private member variables (see NP6).
Example:
@implementation HPLApplicationSettings
- (NSString *)defaultNewsCategory
(
NSString *theResult = nil;
//...
}
- (void)doSomething
(
id theObject = [[self settings] objectForKey:@"key"];
//...
}
@end
Status: Rule
NP9. Names of formal arguments in Objective-C methods start with the following prefixes: 'a/an'.
Prefixes are primarily used to avoid interference with private member variables (see NP6).
Example:
@interface HPLImageCache : NSObject
- (UIImage *)imageForKey:(NSString *)aKey;
- (void)setImage:(UIImage *)anImage forKey:(NSString *)aKey
//...
@end
Status: Rule
NP10. Do not use underscore '_' as a prefix for entities.
We strongly not recommended to use it. Cocoa uses methods and member variables that begin with underscore for its internal purposes, so its usage may potentially lead to incorrect behavior because of accidental overriding of private methods or member variables.
Status: Rule
Naming Conventions
NC1. Names of methods of Objective-C class start with a lowercase letter.
Example:
@interface HPLBanner : NSObject
// Right
- (CGSize)size;
// Wrong
- (CGSize)GetSize;
@end
Exceptions:
The only exception is for cases when a name of method starts with a well-known acronym, then an uppercase letter is to be used.
Example:
@interface HPLBanner : NSObject
// Right
- (NSString *)HTMLBody; // 'HTML' stands for 'Hyper Text Markup Language'
// Wrong
- (UIColor *)rgbColor; // 'RGB' stands for 'Red-Green-Blue'
@end
|
Status: Rule
NC2. Names of macros are all uppercase, with words separated by underscore '_' characters.
The usage of such inconvenient names for macro definitions guarantees they are not suddenly overlapped with other entities. It's a good practice to add some specific prefixes to their names as well just to make their names unique within a particular context.
Example:
#define HPL_MAX_VALUE 100
#define HPL_LOCALIZED_STRING(aKey) NSLocalizedString(aKey, nil)
Exceptions:
Objective-C strings used as keys for key-value coding (KVC) are defined using macros (see CA2), so their names are similar to names of regular constants (see NP4).
Notes:
Try not to use macros at all (see CA1 and CA2).
Status: Rule
NC3. Names Objective-C methods that return an object to be released by a caller start with 'new'.
Example:
@interface HPLCoreFabric
+ (id<IHPLRequest>)newRequestWithInterface:(Protocol *)anInteface;
@end
Status: Rule
NC4. In names that consist of more than one word, write the words together, and start each word that follows the first one with an uppercase letter. Do not use underscores to separate words in complex names.
Example:
// Right
@protocol IHPLGetResultsRequest;
id settingsProvider;
NSDictionary *imageCache;
- (id)lastObject;
@protocol IHPLGetResultsRequest;
id settingsProvider;
NSDictionary *imageCache;
- (id)lastObject;
// Wrong
@protocol IHPLGetResults_Request;
id m_settingsProvider;
NSDictionary *imagecache;
- (id)lastOBJECT;
@protocol IHPLGetResults_Request;
id m_settingsProvider;
NSDictionary *imagecache;
- (id)lastOBJECT;
Status: Rule
NС5. Do not use gratuitous prefixes (e.g. 'my' or Hungarian Notation).
Example:
// Right
NSInteger thePort = kHPLInputPort;
char *theTitle = "Category";
NSInteger thePort = kHPLInputPort;
char *theTitle = "Category";
// Wrong
NSInteger myPort = 80;
char *szTitle = "Category";
NSInteger myPort = 80;
char *szTitle = "Category";
Status: Rule