Class: RuboCop::Cop::Obsession::TooManyParagraphs

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/obsession/too_many_paragraphs.rb

Overview

This cop checks for methods with too many paragraphs.

You should organize your method code into 2 to 3 paragraphs maximum. The 3 possible paragraphs themes could be: initialization, action, and result. Shape your paragraph according to those themes. Spec code should also be organized into 2 to 3 paragraphs (initialization/action/result or given/when/then paragraphs).

After organizing your paragraphs, if they are too long or dense, it could be a sign that they should be broken into smaller methods.

Examples:


# bad
def set_seo_content
  return if seo_content.present?

  template = SeoTemplate.find_by(template_type: 'BlogPost')

  return if template.blank?

  self.seo_content = build_seo_content(seo_template: template, slug: slug)

  Rails.logger.info('Content has been set')
end

# good
def set_seo_content
  return if seo_content.present?
  template = SeoTemplate.find_by(template_type: 'BlogPost')
  return if template.blank?

  self.seo_content = build_seo_content(seo_template: template, slug: slug)

  Rails.logger.info('Content has been set')
end

Constant Summary collapse

MSG =
'Organize method into 2 to 3 paragraphs (init, action, result).'
MAX_PARAGRAPHS =
3

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object Also known as: on_defs



46
47
48
49
50
51
# File 'lib/rubocop/cop/obsession/too_many_paragraphs.rb', line 46

def on_def(node)
  lines = processed_source.lines[node.first_line..(node.last_line - 2)]
  blank_lines_count = lines.count(&:blank?)

  add_offense(node) if blank_lines_count >= MAX_PARAGRAPHS
end