2013-08-23 58 views
0

我使用Code Climate同步我的應用程序以進行代碼審查。RAILS中的重構方法複雜性

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認爲它很複雜。那麼,那個代碼的等級仍然是C,我想重構它。我該如何改進?

回答

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') 
+0

謝謝你會試試你的建議 – xirukitepe