0

我将我的应用程序与 Code Climate 同步以进行代码审查。

lib/models/user/synchrony/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,我想重构它。我该如何改进呢?

4

1 回答 1

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')
于 2013-08-23T10:21:09.650 回答