if and else branches hold identical code
ID |
objectivec.correctness.duplicate_branches |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Duplicate Logic |
Language |
Objective-C |
Description
Reports an if whose branch body and whose following else (or else if) body are written identically, on one line or across several, including bodies that contain nested blocks. Bodies are compared as source text at the same indentation, so branches that differ in spacing, in length, or by a single statement are not reported.
Rationale
Both paths do the same thing, so the condition decides nothing and evaluating it is pure cost — but the condition is still there, telling every later reader that the two cases are handled differently. The usual cause is a branch duplicated with the intention of editing one copy, where the edit never happened: the case the author meant to special-case silently gets the default behaviour, and no test fails because both branches behave identically. The other common cause is a refactor that made the branches converge, leaving a condition that should have been deleted with it.
The following code illustrates the pattern detected by this rule:
@implementation Publisher
- (void)publish:(BOOL)flag {
// FLAGGED: if and else branches hold identical code
if (flag) {
[self deliver];
} else {
[self deliver];
}
}
- (void)deliver {
}
- (void)cancel {
}
- (void)log {
}
@end