Reduce RuboCop's LineLength metric to 80
RuboCop has a cop for enforcing a maximum line length. Per .rubocop_todo.yml
this cop is currently configured as follows:
Metrics/LineLength:
Max: 1310
1310 seems a little excessive, and is very far from the usual 80 characters per line limit. While 80 characters tends to be a controversial discussion, it's the most common limit used out there.
To see how much work this would be, I wrote the following script to get the line lengths from app
, lib
, and spec
:
# frozen_string_literal: true
buckets = Hash.new(0)
files = 0
Dir['{app,lib,spec}/**/*.rb'].each do |file|
File.open(file, 'r') do |handle|
handle.each_line do |line|
bucket = line.chomp.length.round(-1)
bucket = 120 if bucket > 120
buckets[bucket] += 1
end
end
files += 1
end
max = buckets.values.max.to_f
puts 'Length Amount Graph'
buckets.sort_by { |a| a[0] }.each do |(bucket, amount)|
percent = ((amount.to_f / max) * 100.0).ceil
blocks = '▮' * percent
puts "#{bucket.to_s.ljust(4, ' ')} #{amount.to_s.ljust(7, ' ')} #{blocks}"
end
For CE this produces:
Length Amount Graph
0 126512 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
10 119784 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
20 47154 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
30 60602 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
40 60323 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
50 52930 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
60 41054 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
70 28129 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
80 18870 ▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮
90 9925 ▮▮▮▮▮▮▮▮
100 6046 ▮▮▮▮▮
110 3489 ▮▮▮
120 4785 ▮▮▮▮
The "length" column describes the line length, in buckets of 10 characters. The 120 bucket also includes all lines longer than 120 characters. Here we can see that the vast majority of lines are up to 80 characters long, meaning it shouldn't be that much work to deal with the remaining lines.
Rationale
In https://gitlab.com/gitlab-org/release/framework/issues/108 we started looking into improving the code quality of Project
, and some of those who have replied so far mentioned that line length is something that can make code more complex to read. I personally agree with this, but there are also some other reasons:
-
80 characters fits in pretty much everything at almost all resolutions, be it a browser, terminal, editor, or something else.
-
80 characters allows for quite a few vertically stacked windows, as is quite common in editors such as Vim and Emacs. For example, for me it's pretty common to have up to four windows next to each other:
-
There is research floating around (mostly paywalled unfortunately) that suggests a limit of around 70 characters for literature is best, with reading speed decreasing as the limit is increased. The exact recommendation varies a bit. For example, https://baymard.com/blog/line-length-readability suggests up to 60 characters. Usually the recommendation is in the range of 60 to 80 characters.
-
It's probably the most common limit, besides the sometimes used 79 characters per line. This should make it easier for people to contribute, without having to reconfigure their editors where necessary.