RuboCop错误:函数的分配分支条件大小太高。我该如何减少它?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分配,分支和条件度量(ABC)计数分配,分支和条件,作为名称暗示。
如果您的作业,分支机构和条件太多,那么修复方法是减少至少一个
例如:
这里:
您分配
splited_data
,然后您立即覆盖该分配,而无需在介于两者之间做任何其他事情。这意味着第一个任务是完全没有用的,它根本没有任何作用,而您可以写作。
另一个例子:
这4个作业可以很容易地写成没有作业:
很难提供好的建议,因为您的代码非常复杂且令人费解。最好的方法是将代码分解为多个单独的方法。
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
Just as an example:
Here:
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 writeinstead.
Another example:
These 4 assignments can easily be written as no assignment at all:
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.