Source line is longer than 200 characters
ID |
objectivec.maintainability.long_line |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Formatting |
Language |
Objective-C |
Description
Reports a source line longer than 200 characters. The limit is fixed at 200, not at the 100 columns the Google Objective-C style guide suggests: measured over 1.07M lines of real Objective-C, a 100-column limit flags 5.45% of every line written (18.5 per file) and a 200-column one flags 0.44%, so 200 is where the finding still means something. Comment lines and lines made long by a single string literal count too, because the cost of the length is paid by the reader either way.
Rationale
Objective-C is a verbose language - a keyword message send with four arguments and its type annotations reaches the right-hand margin quickly - so a hard column limit is what keeps a method signature or a message send readable without horizontal scrolling. A long line also hides its own right-hand end in a side-by-side diff and in a code-review pane, which is exactly where a changed argument or an inverted condition needs to be visible.
The following code illustrates the pattern detected by this rule:
@implementation ReportBuilder
// FLAGGED: Source line is longer than 200 characters
- (NSString *)summaryForAccount:(NSString *)account balance:(NSDecimalNumber *)balance currency:(NSString *)currency locale:(NSLocale *)locale timeZone:(NSTimeZone *)tz calendar:(NSCalendar *)cal formatter:(NSNumberFormatter *)formatter options:(NSDictionary *)options {
// A line over the old hundred-column limit but well inside the measured one.
NSString *label = [NSString stringWithFormat:@"%@ %@ %@", account, currency, [locale localeIdentifier]];
return label;
}
@end
Remediation
Break the line. A keyword message send splits naturally at each keyword, with the colons aligned; a long condition splits before each operator; a long format string is better held in a named constant. Where the length comes from deeply nested calls, extract an intermediate local with a name that says what it holds.