Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-correct line lengths in a library

I am working on a huge project and we have decided to make all code conform to 80 characters per line. It is a ruby based project with wrappers for C. For Ruby, I decided to use Rubocop and with the command:

rubocop --only LineLength

I got 1,714 errors, where length of the line was greater than 80 characters. Aside from that, there were many other errors detected by Rubocop which I want to ignore for now.

I am looking for the easiest way to auto-correct all the line length violations only, to satisfy the 80 character limit both in C and Ruby.

like image 594
ArafatK Avatar asked Jan 31 '16 04:01

ArafatK


1 Answers

Please don't change line length automatically.

Linelength is a metric, not a style. Styles can often be exchanged, like double vs. single quotes, using hashrockets vs. the new hash syntax introduced in ruby 2 etc. Choosing a style is usually a matter of taste and has few (if any) impact on the program itself. That's why there are auto corrections for styles: Changing them does not change semantics.

Metrics include linelength, classlength and ABC-Size, among others. Checking metrics using static code analysis is something completely different than checking styles. Setting a maximum to linelength for instance is not a matter of taste, you'd rather use it to enforce a certain style of programming. A programmer would have to avoid long variable names and deep nesting to keep the linelength under the limit.

Metrics can also indicate problems in the code. For example, too high ABC-size indicates a method might be doing too much.

Aside from that, it would be very difficult, if not impossible, to shorten all lines of code automatically, since ruby is a very complex language.

Instead of reducing linelength automatically, here's some alternatives:

  • see if you can reduce the number of violations by enabling the AllowHeredoc and AllowURI options. Read about them here https://rubocop.readthedocs.io/en/latest/cops_metrics/#metricslinelength.
  • run rubocop --only LineLength --auto-gen-config and use rubocops configuration to stop checking linelength.
  • ask yourself: What value do I gain by reducing linelength?
  • don't think of too long lines as style violation, but rather as a possible indicator of an underlying problem. Try to find that problem, and solve it.
like image 53
SvenDittmer Avatar answered Sep 23 '22 18:09

SvenDittmer