Class: RuboCop::Cop::Obsession::MethodOrder

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
CommentsHelp, ConfigurableEnforcedStyle, Helpers, VisibilityHelp
Defined in:
lib/rubocop/cop/obsession/method_order.rb

Overview

This cop checks for private/protected methods that are not ordered correctly. It supports autocorrect.

Note 1: the order of public methods is not enforced. They can be defined in any order the developer wants, like by order of importance. This is because public methods are usually called outside of the class and often not called within the class at all. If possible though, developers should still try to order their public methods when it makes sense.

Note 2: for top to bottom styles, method order cannot be computed for methods called by ‘send`, metaprogramming, private methods called by superclasses or modules, etc. This cop’s suggestions and autocorrections may be slightly off for these cases.

Note 3: for simplicity, protected methods do not have to be ordered if there are both a protected section and a private section.

Examples:

EnforcedStyle: drill_down (default)

In this style, code should read from top to bottom. More
particularly, methods should be defined in the same order as the
order when they are first mentioned. Put another way, you go to the
bottom of the method call tree before going back up. See examples
below.

This style is similar to the code example provided in the "Reading
Code from Top to Bottom: The Stepdown Rule" chapter from Robert C.
Martin's "Clean Code" book, but diverges from it in that it drills
down to the bottom of the call tree as much as possible.

# bad
class Foo
  def perform
    return if method_a?
    method_b
    method_c
  end

  private

  def method_c; ...; end
  def method_b; ...; end
  def method_a?; ...; end
end

# good
class Foo
  def perform
    return if method_a?
    method_b
    method_c
  end

  private

  def method_a?; ...; end
  def method_b; ...; end
  def method_c; ...; end
end

# bad
class Foo
  def perform
    method_a
    method_b
  end

  private

  def method_a; method_c; end
  def method_b; method_c; end
  def method_c; ...; end
end

# good
class Foo
  def perform
    method_a
    method_b
  end

  private

  def method_a; method_c; end
  def method_c; ...; end
  def method_b; method_c; end
end

EnforcedStyle: step_down

In this style, code should read from top to bottom. More
particularly, common called methods (which tend to have a lower level
of abstraction) are defined after the group of methods that calls
them (these caller methods tend to have a higher level of
abstraction). The idea is to gradually descend one level of
abstraction at a time.

This style adheres more strictly to the code example provided in the
"Reading Code from Top to Bottom: The Stepdown Rule" chapter from
Robert C. Martin's "Clean Code" book.

# bad
class Foo
  def perform
    method_a
    method_b
  end

  private

  def method_a; method_c; end
  def method_c; ...; end
  def method_b; method_c; end
end

# good
class Foo
  def perform
    method_a
    method_b
  end

  private

  def method_a; method_c; end
  def method_b; method_c; end
  def method_c; ...; end
end

EnforcedStyle: alphabetical

In this style, methods are ordered alphabetically. This style is
unambiguous and interpretation-free.

# bad
class Foo
  def perform; ...; end

  private

  def method_c; ...; end
  def method_b; ...; end
  def method_a; ...; end
end

# good
class Foo
  def perform; ...; end

  private

  def method_a; ...; end
  def method_b; ...; end
  def method_c; ...; end
end

Defined Under Namespace

Classes: Node

Constant Summary collapse

MSG =
'Method `%<after>s` should appear below `%<previous>s`.'

Instance Method Summary collapse

Methods included from Helpers

#rails_callback?, #verb?

Instance Method Details

#on_class(class_node) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/rubocop/cop/obsession/method_order.rb', line 189

def on_class(class_node)
  @class_node = class_node
  find_private_node || return
  build_methods || return

  build_ordered_private_methods
  build_private_methods

  verify_private_methods_order
end