Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubocop error 'Class definition is too long ruby'

I am getting rubocop error 'Class definition is too long. [236/100]'. My class looks like below:

class SomeClassName
  include HelperModule
  attr_accessor :aaa, :bbb, :ccc

  .... methods .....
end

What might go wrong? The rubocop docs ClassLength says "length of a class exceeds some maximum value". What does it mean?

like image 317
brg Avatar asked Nov 22 '13 12:11

brg


2 Answers

(There's lots of good info here already, but I came to this answer looking for the syntax for specifying the max lines per class in Rubocop, and I figure others might come here for that as well.)

In .rubocop.yml

# Allow classes longer than 100 lines of code
ClassLength:
  Max: 250 # or whatever ends up being appropriate
like image 152
lostphilosopher Avatar answered Oct 02 '22 12:10

lostphilosopher


Yes, this is because the overall lines are considered to be too many by rubucop. I agree classes shouldn't get too long but think that should ultimately be determined by: does the class have a single responsibility, are the methods concise enough, are there methods that could be shared via module, etc... The number/alert is a great warning though. If things in the class look OK, you can add # rubocop:disable ClassLength immediately above the class definition.

like image 41
ron pastore Avatar answered Oct 02 '22 13:10

ron pastore