Module: RuboCop::Cop::Metrics::Utils::AbcSizeCalculator

Defined in:
lib/rubocop/cop/metrics/utils/abc_size_calculator.rb

Overview

> ABC is .. a software size metric .. computed by counting the number > of assignments, branches and conditions for a section of code. > c2.com/cgi/wiki?AbcMetric

We separate the calculator from the cop so that the calculation, the formula itself, is easier to test.

Constant Summary collapse

BRANCH_NODES =

> Branch – an explicit forward program branch out of scope – a > function call, class method call .. > c2.com/cgi/wiki?AbcMetric

%i[send csend].freeze
CONDITION_NODES =

> Condition – a logical/Boolean test, == != <= >= < > else case > default try catch ? and unary conditionals. > c2.com/cgi/wiki?AbcMetric

CyclomaticComplexity::COUNTED_NODES.freeze

Class Method Summary collapse

Class Method Details

.calculate(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 24

def self.calculate(node)
  assignment = 0
  branch = 0
  condition = 0

  node.each_node do |child|
    if child.assignment?
      assignment += 1
    elsif BRANCH_NODES.include?(child.type)
      branch += 1
    elsif CONDITION_NODES.include?(child.type)
      condition += 1
    end
  end

  Math.sqrt(assignment**2 + branch**2 + condition**2).round(2)
end