Variable name is not lowerCamelCase
ID |
objectivec.maintainability.naming_identifier_style |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Naming |
Language |
Objective-C |
Description
Reports the declaration of a variable whose name is not lowerCamelCase - it starts with an uppercase letter (AccountLabel), or it separates words with underscores (user_count, TOTAL_POSTED). A single leading underscore is allowed, because that is the Cocoa convention for an instance variable, and a name containing a double underscore is left to the reserved-identifier rule so that one declaration is not reported twice. Only standalone declarations are reported; method parameters are not.
Rationale
Cocoa gives each kind of name a distinct shape - UpperCamelCase for classes and protocols, lowerCamelCase for variables, methods and properties, k-prefixed camel case for constants, and ALL_CAPS for preprocessor macros - so a reader can tell what a name refers to before looking it up. A variable spelled TOTAL_POSTED reads as a macro, which changes how a reader expects it to behave: macros have no scope, no type, and are not addressable. Mixing the conventions removes information the reader was relying on.
The following code illustrates the pattern detected by this rule:
@implementation Ledger {
}
- (void)post {
// FLAGGED: Variable name is not lowerCamelCase
NSInteger user_count = 0;
NSLog(@"%ld %@ %ld %@ %ld %@ %@ %ld %ld", (long)user_count, AccountLabel,
(long)TOTAL_POSTED, _posted_label, (long)userCount, accountLabel, _cachedLabel,
(long)count, (long)_pendingCount);
}
@end