Conditional statement does not start its own line
ID |
objectivec.maintainability.conditionals_start_new_lines |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Readability |
Language |
Objective-C |
Description
Reports an if, for, while or switch that begins after a semicolon or a closing brace on the same line, rather than on a line of its own. The clause separators inside a for header, the while of a do-while, and an else or else if continuing a chain are not reported, and neither is a conditional preceded on the line by a string literal.
Rationale
A control statement changes which of the lines below it run, so it is the thing a reader scans the left margin for. Buried after another statement it is missed on the first pass, and the block that follows appears to run unconditionally. It also costs the diff its resolution: changing either the statement or the condition rewrites the same line, so review and blame cannot tell the two edits apart, and a breakpoint cannot be set on one without the other.
The following code illustrates the pattern detected by this rule:
@implementation Dispatcher
- (void)handleCode:(NSInteger)code {
// FLAGGED: Conditional statement does not start its own line
[self reset]; if (code < 0) { [self fail]; }
// FLAGGED: Conditional statement does not start its own line
NSInteger limit = 3; while (code > limit) { code--; }
// FLAGGED: Conditional statement does not start its own line
if (code == 0) { [self accept]; } if (code == 1) { [self retry]; }
// FLAGGED: Conditional statement does not start its own line
NSLog(@"code %ld", (long)code); switch (code) { default: break; }
}
- (void)reset { }
- (void)fail { }
- (void)accept { }
- (void)retry { }
- (void)ignore { }
- (void)step { }
@end