Class: Roodi::Checks::AbcMetricMethodCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/roodi/checks/abc_metric_method_check.rb

Overview

The ABC metric method check calculates the number of Assignments, Branches and Conditionals in your code. It is similar to cyclomatic complexity, so the lower the better.

Constant Summary collapse

ASSIGNMENTS =
[:lasgn]
BRANCHES =
[:vcall, :call]
CONDITIONS =
[:==, :"!=", :<=, :>=, :<, :>]
OPERATORS =
[:*, :/, :%, :+, :<<, :>>, :&, :|, :^]
DEFAULT_SCORE =
10

Constants inherited from Check

Check::NODE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Check

#add_error, #end_file, #errors, #evaluate_end, #evaluate_node, #evaluate_node_end, #evaluate_node_start, make, #position, #start_file

Constructor Details

#initializeAbcMetricMethodCheck

Returns a new instance of AbcMetricMethodCheck.


17
18
19
20
# File 'lib/roodi/checks/abc_metric_method_check.rb', line 17

def initialize
  super()
  self.score = DEFAULT_SCORE
end

Instance Attribute Details

#scoreObject

Returns the value of attribute score


15
16
17
# File 'lib/roodi/checks/abc_metric_method_check.rb', line 15

def score
  @score
end

Instance Method Details

#evaluate_start(node) ⇒ Object


26
27
28
29
30
31
32
33
# File 'lib/roodi/checks/abc_metric_method_check.rb', line 26

def evaluate_start(node)
  method_name = node[1]
  a = count_assignments(node)
  b = count_branches(node)
  c = count_conditionals(node)
  score = Math.sqrt(a*a + b*b + c*c)
  add_error "Method name \"#{method_name}\" has an ABC metric score of <#{a},#{b},#{c}> = #{score}.  It should be #{@score} or less." unless score <= @score
end

#interesting_nodesObject


22
23
24
# File 'lib/roodi/checks/abc_metric_method_check.rb', line 22

def interesting_nodes
  [:defn]
end