重写有状态代码以使其更干
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重写有状态代码以使其更干相关的知识,希望对你有一定的参考价值。
我有这个代码块,其中每个下一组行取决于先前的响应,并且步骤集必须遵循固定路径使其略微有状态。
这导致我的方法大小增加和大量代码重复(即使变量和数据不同)。我怎么能干掉这段代码?
def process
response = ServiceResponse.new(false, [])
# Step 1
project_path = import_project()
if project_path.present?
response.data << Step.new('import', true, "Project imported")
send_realtime_data(response)
hr_config, hr_config_error = fetch_hr_config(project_path)
weighted_scores_config, ws_error = fetch_weighted_scores(project_path)
if hr_config.blank?
response.data << Step.new('.hr_config.yml', false, hr_config_error || "Empty configuration file")
return response
else
response.data << Step.new('.hr_config.yml', true, 'Configuration file found')
send_realtime_data(response)
end
if weighted_scores_config.blank? && ws_error.present?
response.data << Step.new('.hr_weighted_scores.yml', false, ws_error)
return response
else
response.data << Step.new('.hr_weighted_scores.yml', true, "Weighted scoring config found")
send_realtime_data(response)
end
configuration = ::X::FullStack::Configuration.new(
hr_config, {weighted_scores: weighted_scores_config}
)
if !configuration.valid?
response.data << Step.new('validate_configuration', false, "Configuration validation failed", configuration.validations.as_json)
return response
else
response.data << Step.new('validate_configuration', false, "Configuration validated successfully", configuration.validations.as_json)
send_realtime_data(response)
end
#....
end
end
对此方法的响应通过websocket共享(代码来自rails延迟任务),send_realtime_data
方法将数据发送到前端。当方法结束时(通过return
),任务完成,返回值被发送到前端,然后关闭websocket。
答案
你的else
分支是多余的。
def process
response = ServiceResponse.new(false, [])
project_path = import_project
if project_path.blank?
return
end
response.data << Step.new('import', true, "Project imported")
send_realtime_data(response)
hr_config, hr_config_error = fetch_hr_config(project_path)
weighted_scores_config, ws_error = fetch_weighted_scores(project_path)
if hr_config.blank?
response.data << Step.new('.hr_config.yml', false, hr_config_error || "Empty configuration file")
return response
end
response.data << Step.new('.hr_config.yml', true, 'Configuration file found')
send_realtime_data(response)
if weighted_scores_config.blank? && ws_error.present?
response.data << Step.new('.hr_weighted_scores.yml', false, ws_error)
return response
end
response.data << Step.new('.hr_weighted_scores.yml', true, "Weighted scoring config found")
send_realtime_data(response)
configuration = ::X::FullStack::Configuration.new(
hr_config, {weighted_scores: weighted_scores_config}
)
unless configuration.valid?
response.data << Step.new('validate_configuration', false, "Configuration validation failed", configuration.validations.as_json)
return response
end
response.data << Step.new('validate_configuration', false, "Configuration validated successfully", configuration.validations.as_json)
send_realtime_data(response)
#....
end
以上是关于重写有状态代码以使其更干的主要内容,如果未能解决你的问题,请参考以下文章
什么是 python 代码来转置面板数据以使其更容易进行回归和进一步分析