2009年10月18日 星期日

Ruby on Rails Code Quality Checklist

Ruby on Rails Code Quality Checklist

1. Each controller action only calls one model method other than an initial find or new.
(Make custom .new or .update methods in the model with all necessary). ...
就是盡量把邏輯擺到model 裡, 唯一的例外就是在決定要render view or redirect的時候


2. Only one or two instance variables are shared between each controller and view. ...
保持一兩個instance variable 在controller 和 view 就好
這樣可以讓所有相關連的只有在需要的時候在被call 到, 而且還可以做instance-variable cache in one place
例如不要在controller 裡用到 @post and @related_posts 兩個instance variable, 而是create a method 給 @post, 這樣你就可以在 view 裡面直接用 @post.related_posts


3. All model and variable names are both immediately obvious (to a new developer) and as short as possible without using abbreviations. ...
取個好名字


4. All custom "finds" accessed from more than one place in the code use named_scope instead of a custom method. ...
有關find 什麼東西的, 都用named_scope 來做


5. A .find or .find_by_ is never called in a view or view helper. ...
別直接在view , find or find_by anything, 至少用一下namde_scope or custom methods to do that


6. There is zero custom code that duplicates functionality of a built-in function in rails. ...
7. Code has been aggressively DRYed during development. ...
8. All functionality used in two or more models has been turned into a library/module. ...
9. All logic duplicated between two or more apps has been turned into a gemified plugin. ...

10. STI is not used anywhere ...
別用STI, 用module 來代替, 如果你用STI 你會需要至少bind 兩個model, 這樣你很難回頭
data migration 你會死
但是polymorphic association 是鼓勵的


11. Every design choice should yield the most simplistic design possible for the need of users at the current time.
No guesses for future functionality were designed into the application. ...
對於現在的情況做最簡單的設計, 不要猜測未來的情況, 你可能會猜錯


12. Close to full test coverage exists at the highest level of the application: on and between controller actions. Coverage is highest for code used by the most number of end users. ...

先從user 最常用的開始測起
13. All tests pass before code is merged into a shared repository. ...

merge 前要測過
14. Every fixed defect on a deployed product has tests added to prevent regression. ...
替解掉的defeat 加上測試

15. Every plugin installed has been code reviewed. ...
檢查一下你的plugin code

沒有留言: