RuboCop错误:函数的分配分支条件大小太高。我该如何减少它?

发布于 2025-01-25 10:20:01 字数 2433 浏览 3 评论 0原文

1.我刚刚开始在Ruby上工作,并开始使用Rubocop来遵循Ruby的一般指南,但是我遇到了分配分支条件错误,有人可以帮助我吗?

@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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2025-02-01 10:20:01

分配,分支和条件度量(ABC)计数分配分支条件,作为名称暗示。

如果您的作业,分支机构和条件太多,那么修复方法是减少至少一个

  • 任务,
  • 分支或
  • 条件之一。

例如:

这里:

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

您分配splited_data,然后您立即覆盖该分配,而无需在介于两者之间做任何其他事情。这意味着第一个任务是完全没有用的,它根本没有任何作用,而您可以写作

splited_data = file_data[0].split ','

另一个例子:

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

这4个作业可以很容易地写成没有作业:

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

很难提供好的建议,因为您的代码非常复杂且令人费解。最好的方法是将代码分解为多个单独的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文