Search code examples
rubyrubocop

Metrics/AbcSize: Assignment Branch Condition size for chart_wise_results is too high. (Ruby)


I am trying to solve the mentioned offense firstly, my function looks like this

  def chart_wise_results
    require 'colorize'
    require 'time'

    _arg, year, path, _month = ARGV
    folder_name = path.split('/')
    year = year.split('/')
    collection = file_collection("#{path}/#{folder_name[2]}_#{year[0]}_#{Date::ABBR_MONTHNAMES[year[1].to_i]}.txt")
    collection.shift
    collection.each do |w|
      puts ( '+' * w[1].to_i).red + "#{w[1]}C", ('+' * w[3].to_i).blue + "#{w[3]}C"

    end
  end
end

What I have tried: I have tried removing the w[1].to_i and tried printing it with static number. I need this library as I have to print colored output.


Solution

  • AbcSize is a metric which tells how many assignments, branches and conditions there are (more info here or here).

    You can try to simplify your code, e. g. using extract method refactoring technique.

    def print_element(element)
      puts ( '+' * element[1].to_i).red + "#{element[1]}C", ('+' * element[3].to_i).blue + "#{element[3]}C"
    end
    

    and then

      collection.each do |w|
        print_element(w)
      end