[英]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? 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' }] @months = [{ 1 => '一月' }, { 2 => '二月' }, { 3 => '三月' }, { 4 => '四月' }, { 5 => '五月' }, { 6 => '六月' },{ 7 => '七月' }, { 8 => '八月' }, { 9 => '九月' }, { 10 => '十月' }, { 11 => '十一月' }, { 12 => '十二月' }]
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.顾名思义,分配、分支和条件指标(ABC) 计算分配、分支和条件。
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如果你有太多的任务、分支和条件,那么解决这个问题的方法是减少至少一个的数量
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.您分配
splited_data
然后立即覆盖该分配,中间不做任何其他事情。 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:这 4 个作业可以很容易地写成根本没有作业:
{
'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.
最好的方法是将代码分解为多个单独的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.