Local variable declared only to be returned on the next line
ID |
objectivec.maintainability.redundant_local_variable |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Code |
Language |
Objective-C |
Description
Reports a local variable that is declared, initialised, and returned by the immediately following statement at the same indentation. A local that is read more than once, one with any statement between its declaration and the return, and a plain assignment to an existing name are not reported.
Rationale
The variable adds a name and a line without adding information: the reader has to hold the name for exactly one statement to find it means the expression above. Where the name would genuinely explain a complicated expression, the explanation belongs in the method name or a helper; where it does not, the two lines say what one line already said.
The following code illustrates the pattern detected by this rule:
@implementation Formatter
- (NSString *)title {
// FLAGGED: Local variable declared only to be returned on the next line
NSString *result = [self.name uppercaseString];
return result;
}
@end