简体   繁体   中英

Rubocop error: Assignment Branch Condition size for a function is too high. How can I decrease this?

1.I just started working on ruby and started using Rubocop for following general ruby guidelines but I got Assignment Branch Condition error, is anyone can help me with this?

@months = [{ 1 => 'Jan' }, { 2 => 'Feb' }, { 3 => 'Mar' }, { 4 => 'Apr' }, { 5 => 'May' }, { 6 => 'Jun' },{ 7 => 'Jul' }, { 8 => 'Aug' }, { 9 => 'Sep' }, { 10 => 'Oct' }, { 11 => 'Nov' }, { 12 => 'Dec' }]

   def calc_high_low_temp_of_month(date, address)

      high_temp_of_month = []
      min_temp_of_month = []
      max_humid_of_month = []

      @data_exists = false
      year, month = date.split('/')
      month = month.to_i
      month_hash = @months.detect { |x| x[month] }
      month = month_hash[month]
      begin
        arr = Dir.children(address)
      rescue Errno::ENOENT
        puts 'file not found!!!'
        return ''
      end
      arr.each do |elem|
         next unless elem.include?(year.to_s) && elem.include?(month.to_s)

        file_data = []
        File.foreach("#{address}/#{elem}") do |line|
          unless line.strip.empty?
            file_data.push(line)
            @data_exists = true
          end
        end

        splited_data = []
        splited_data = file_data[0].split ','
        index_of_high_temp = splited_data.find_index('Max TemperatureC')
        index_of_min_temp = splited_data.find_index('Min TemperatureC')
        index_of_humidity = splited_data.find_index('Max Humidity')

        (1...file_data.count).each do |index|
          splited_data = file_data[index].split ','
          if index_of_high_temp <= (splited_data.count - 1) && !splited_data[index_of_high_temp].empty?
            high_temp_of_month.push(splited_data[index_of_high_temp].to_i)
          end
          if index_of_min_temp <= (splited_data.count - 1) && !splited_data[index_of_min_temp].empty?
            min_temp_of_month.push(splited_data[index_of_min_temp].to_i)
          end
          next unless index_of_humidity <= (splited_data.count - 1)

          max_humid_of_month.push(splited_data[index_of_humidity].to_i) unless splited_data[index_of_humidity].empty?
        end
      end
      if @data_exists
        min_max_temp_of_month_hash = {}
        min_max_temp_of_month_hash['highTempOfMonth'] = high_temp_of_month
        min_max_temp_of_month_hash['minTempOfMonth'] = min_temp_of_month
        min_max_temp_of_month_hash['maxHumidOfMonth'] = max_humid_of_month
        min_max_temp_of_month_hash
      else
        ''
      end
    end

The Assignment, Branch, and Condition Metric (ABC) counts Assignments , Branches , and Conditions , as the name implies.

If you have too many Assignments, Branches, and Conditions, then the way to fix that is to reduce the amount of at least one of

  • Assignments,
  • Branches, or
  • Conditions.

Just as an example:

Here:

splited_data = []
splited_data = file_data[0].split ','

You assign splited_data and then you immediately overwrite that assignment without doing anything else in between. Which means that the first assignment is completely useless, it doesn't do anything at all, and you could just write

splited_data = file_data[0].split ','

instead.

Another example:

min_max_temp_of_month_hash = {}
min_max_temp_of_month_hash['highTempOfMonth'] = high_temp_of_month
min_max_temp_of_month_hash['minTempOfMonth'] = min_temp_of_month
min_max_temp_of_month_hash['maxHumidOfMonth'] = max_humid_of_month

These 4 assignments can easily be written as no assignment at all:

{
  'highTempOfMonth' => high_temp_of_month,
  'minTempOfMonth' => min_temp_of_month,
  'maxHumidOfMonth' => max_humid_of_month
}

It is hard to give good advice because your code is incredibly complex and convoluted. The best approach would be to break the code into multiple separate methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM