我将我的应用与Code Climate同步进行代码审核。
LIB /模型/用户/同步/ basecamp_inc.rb
def todo_list(todos,basecamp_account,proj_id)
todos.map{|todo|
task = basecamp_tasklist(proj_id,todo['id'],basecamp_account)
task_list(task['todos']['remaining'],task['name'],'remain') if task['todos']['remaining'].present?
task_list(task['todos']['completed'],task['name'],'complete') if task['todos']['completed'].present?
}
end
def task_list(tasks,todo_list,status)
tasks.map{|task|
task_created(task,todo_list)
task['assignee'].present? && user_owner?(task['assignee']['name']) ? save_task_activity('was assigned to a',task,todo_list,task['updated_at'],'assign') : [tasks,todo_list,status]
status == 'complete' && user_owner?(task['completer']['name']) && current_date?(task['completed_at']) ? save_task_activity('finished',task,todo_list,task['completed_at'],'finished') : [tasks,todo_list,status]
}
end
上述代码被Code Climate检测为具有复杂方法。我不明白为什么Code Climate认为它很复杂。那么,该代码的等级仍然是C,我想重构它。我怎样才能改善这个?
答案 0 :(得分:0)
您应该考虑将以下条件转换为私有方法:
task['assignee'].present? && user_owner?(task['assignee']['name']) ? save_task_activity('was assigned to a',task,todo_list,task['updated_at'],'assign') : [tasks,todo_list,status]
status == 'complete' && user_owner?(task['completer']['name']) && current_date?(task['completed_at']) ? save_task_activity('finished',task,todo_list,task['completed_at'],'finished') : [tasks,todo_list,status]
您应该考虑将以下调用转移到采用status
参数的单个方法,例如:
task_list(task['todos']['remaining'], task['name'], 'remain') if task['todos']['remaining'].present?
将被重构为:
def build_task_list(tasks, name, status)
task_list(tasks, name, status) if task.present?
end
build_task_list(task['todos']['remaining'], task['name'], 'remain')
build_task_list(task['todos']['completed'], task['name'], 'complete')