Variable name is longer than 20 characters
ID |
objectivec.maintainability.long_variable_name |
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 longer than 20 characters. The threshold is fixed at 20, the default of the OCLint LongVariableName rule. Only standalone declarations are reported; method parameters and for-header counters are not.
Rationale
Objective-C rewards descriptive names, but past a certain length the name stops describing the variable and starts describing its whole surrounding context - a local called cachedRemoteConfigurationPayloadForSession is repeating the class it lives in and the method it was computed in. Names that long push message sends past the column limit, force awkward line breaks in exactly the expressions that most need to be readable, and are usually a sign that the enclosing method is holding several distinct pieces of state that belong in separate objects.
The following code illustrates the pattern detected by this rule:
@implementation Checkout
- (void)recalculate {
// 34 characters: long, but ordinary Objective-C.
NSInteger numberOfRetriesBeforeGivingUp = 3;
NSString *defaultConfigurationIdentifier = @"default";
// FLAGGED: Variable name is longer than 20 characters
static NSDictionary *cachedRemoteConfigurationPayloadForSessionAndAccount = nil;
// Exactly 41 characters, one over the limit.
// FLAGGED: Variable name is longer than 20 characters
NSInteger abcdefghijabcdefghijabcdefghijabcdefghijk = 0;
NSInteger retryBudget = 3;
NSString *configIdentifier = @"default";
// Exactly 40 characters, at the limit.
NSInteger abcdefghijabcdefghijabcdefghijabcdefghij = 0;
// NSInteger numberOfRetriesBeforeGivingUp = 3;
NSLog(@"NSInteger numberOfRetriesBeforeGivingUp = 3;");
NSLog(@"%ld %@ %@ %ld %@ %ld %ld", (long)numberOfRetriesBeforeGivingUp,
defaultConfigurationIdentifier, cachedRemoteConfigurationPayloadForSessionAndAccount,
(long)abcdefghijabcdefghijabcdefghijabcdefghijk, configIdentifier, (long)retryBudget,
(long)abcdefghijabcdefghijabcdefghijabcdefghij);
}
@end
Remediation
Shorten the name to the part that distinguishes this variable from the others in scope, and let the enclosing type and method supply the rest of the context - inside -[SessionCache refresh], payload says as much as cachedRemoteConfigurationPayloadForSession. Where the long name is genuinely needed to keep several similar values apart, that is usually the signal to extract them into a type of their own.