Method declares six or more keyword parameters
ID |
objectivec.maintainability.max_params |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Complexity |
Language |
Objective-C |
Description
Reports the declaration of an instance or class method whose selector has six or more colon-introduced keyword parts, and therefore six or more parameters. The threshold is fixed at 6. Message sends are not reported, only declarations.
Rationale
Objective-C names each argument at the call site, so a long selector is more readable than the equivalent C parameter list - but the underlying problem survives the naming. Six parameters is six things the caller has to have on hand and keep consistent, and it usually means the method is doing several jobs at once or that a set of values that always travel together has never been given a type of its own. Every new caller has to supply all six, so the list only grows.
The following code illustrates the pattern detected by this rule:
@implementation AccountForm
// FLAGGED: Method declares six or more keyword parameters
- (void)configureWithName:(NSString *)name age:(NSInteger)age city:(NSString *)city zip:(NSString *)zip country:(NSString *)country phone:(NSString *)phone {
NSLog(@"%@", name);
}
@end
Remediation
Group the parameters that always travel together into a model object or a configuration object and pass that instead, or split the method so each part takes only what it needs. Where several parameters are optional, a builder or a configuration block reads better than a selector that grows a keyword per option.