else branch is empty
ID |
objectivec.maintainability.empty_else_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Objective-C |
Description
Reports an else branch whose body holds nothing at all, whether the braces are written on one line or across several. An else if and a branch carrying a statement or an explanatory comment are not reported.
Rationale
An empty else contributes nothing to the flow of the method: removing it changes no behaviour. It is left behind either because the branch was emptied and the shell survived, or because someone meant to fill it in later. A reader has to follow the whole if chain to establish that the branch genuinely does nothing, and every future edit to the condition has to be reasoned about against a branch that exists but is blank.
The following code illustrates the pattern detected by this rule:
@implementation Router
- (void)routeOnOneLine:(NSInteger)code {
if (code == 200) {
[self deliver];
// FLAGGED: else branch is empty
} else { }
}
- (void)deliver {
NSLog(@"delivering");
}
- (void)reportFailure:(NSInteger)code {
NSLog(@"failed with %ld", (long)code);
}
@end
Remediation
Delete the else and its braces. If the branch was meant to hold the interesting case, invert the condition so the real work reads first and the trivial case falls through. Where the emptiness is deliberate, a comment inside the braces recording the reason is clearer than an unexplained blank branch — and also stops this rule reporting it.