Unnecessary else after a returning if branch
ID |
objectivec.maintainability.else_after_return |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Objective-C |
Description
Reports an else that follows an if branch whose last statement is a return, whether the branch is braced or not. An else if continuing a chain is not reported, and neither is a branch whose return sits inside a nested loop or block rather than closing the branch.
Rationale
The if branch already leaves the method, so nothing after it can run unless the condition was false — the else states a second time what the return has established. The cost is that the whole tail of the method sits one level deeper than it needs to, and every guard added later nests it deeper again, until the part that does the work is the most indented part of the method rather than the plainest.
The following code illustrates the pattern detected by this rule:
@implementation Validator
- (NSString *)describe:(NSInteger)code {
if (code == 0) {
[self log];
// FLAGGED: Unnecessary else after a returning if branch
return @"idle";
} else {
return @"busy";
}
}
- (void)log { }
- (void)reset { }
@end