{"id":1680,"date":"2007-10-29T23:21:42","date_gmt":"2007-10-29T15:21:42","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1680"},"modified":"2008-09-29T01:02:14","modified_gmt":"2008-09-28T17:02:14","slug":"rails-code-review","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1680-rails-code-review","title":{"rendered":"\u63a8\u85a6 Rails Code Review PDF"},"content":{"rendered":"<p>Update: \u539f\u4f5c\u8005\u6709\u6458\u8981\u5728 <a href=\"http:\/\/nubyonrails.com\/articles\/massive-list-of-rails-development-tips\">Massive List of Rails Development Tips<\/a>\u4e00\u6587<\/p>\n<p>\u63a8\u85a6 <a href=\"http:\/\/peedcode.com\" class=\"autohyperlink\">peedcode.com<\/a> \u4e0a\u7684 <a href=\"http:\/\/peepcode.com\/products\/draft-rails-code-review-pdf\">Rails Code Review<\/a> PDF\uff0c\u5ee2\u8a71\u4e0d\u591a\u883b\u5be6\u7528\u7684\uff0c\u4e00\u8def\u770b\u4e0b\u4f86\u99ac\u4e0a\u5b78\u5230\u597d\u5e7e\u62db\u8ddf\u4e0d\u932f\u7684\u5de5\u5177\uff0c\u7c21\u55ae\u8d70\u8a2a\u4e00\u4e0b\uff1a<\/p>\n<p><strong>Store Sessions in the Database<\/strong><\/p>\n<p>\u7528 ActiveRecord \u5b58 Sessions \u5728 AWDwR \u7b2c\u4e8c\u7248\u90fd\u5df2\u7d93\u8b8a\u6210\u5165\u9580\u7bc4\u4f8b\u4e86\uff0c\u4e0d\u904e\u9019\u88e1\u63d0\u4f9b\u7684 Rake code \u4e0d\u932f\u7528\uff0c\u5e6b\u4f60\u6e05\u9664\u8cc7\u6599\u5eab\u4e2d\u904e\u671f\u5169\u9031\u7684Sessions \u3002<\/p>\n<p><strong>Use Custom Configuration Files<\/strong><\/p>\n<p>\u5225\u628a config \u7d71\u7d71\u6254\u5230 environment.rb\uff0c\u53ef\u4ee5\u62c6\u6210\u7368\u7acb\u7684 YAML \u6a94\u6848\u3002\u4e5f\u56e0\u6b64\u53ef\u4ee5\u4e0d\u5fc5\u5c07 production mode \u624d\u7528\u5230\u7684\u8a2d\u5b9a\u6a94(\u5982API\u5bc6\u78bc\u7b49)\u4e1f\u9032 repository\uff0c \u642d\u914d Capistrano \u5728 deploy \u7684\u6642\u5019\u5f9e\u8907\u88fd\u904e\u4f86\u5c31\u53ef\u4ee5\u4e86\uff0c<\/p>\n<p><strong>Use Constants for Repeated Strings<\/strong><\/p>\n<p><strong>Keep Time in UTC<\/strong><\/p>\n<p>\u4e00\u958b\u59cb\u5c31\u7528 UTC \u7576\u9810\u8a2d\u6642\u5340<\/p>\n<p><strong>Don&#8217;t Loop Around ActiveRecord<\/strong><\/p>\n<p>ActiveRecord \u8dd1\u8ff4\u5708\u7684\u6642\u5019\u5c0f\u5fc3\u7522\u751f\u4e00\u7b46\u4e00\u7b46 SQL queries \u554a\uff0c\u8acb\u611b\u7528 :include \u4e00\u6b21\u5c31\u628a(\u5b50)\u8cc7\u6599\u8b80\u51fa\u4f86\uff0c\u800c\u4e0d\u662f\u4e00\u7b46\u7b46\u53bb\u6345\u8cc7\u6599\u5eab\u3002\u751a\u81f3\u76f4\u63a5\u5beb\u9ede SQL conditions\uff0c\u76ee\u6a19\u662f\u8b93 SQL query \u4e00\u6b21\u5373\u53ef\u3002\u82e5\u8981\u7528 :select \u8acb\u642d\u914d :join\u3002<\/p>\n<p><strong>Beware of Binary Fields<\/strong><\/p>\n<p>\u82e5\u6b04\u4f4d\u6709 Binary data\uff0c\u7528 find \u6642\u8acb\u52d9\u5fc5\u611b\u7528 :select \u53ea\u8b80\u51fa\u5fc5\u8981\u6b04\u4f4d\u3002\u53e6\u5916\u4e5f\u5efa\u8b70\u4f7f\u7528 <a href=\"http:\/\/ryandaigle.com\/articles\/2006\/07\/20\/a-rails-feature-you-should-be-using-with_scope\">with_scope<\/a> method \u6216 <a href=\"http:\/\/agilewebdevelopment.com\/plugins\/scope_out\">scope_out plugin<\/a>\u3002<\/p>\n<p><strong>Cache Your Reports<\/strong><\/p>\n<p><strong>Store Country Lists<\/strong><\/p>\n<p>\u5225\u7528 Rails \u5167\u5efa\u7684 country_select\uff0c\u81ea\u5df1\u5beb Model\u624d\u6709\u5f48\u6027\u3002<\/p>\n<p><strong>Avoid Bloated Controllers<\/strong><\/p>\n<p>RESTful \u5c0d controller \u7684\u5efa\u8b70: \u5225\u585e\u592a\u591a action \u5728\u4e00\u500b controller \u88e1\u3002<\/p>\n<p><strong>Keep Your Controllers and Views Skinny<\/strong><\/p>\n<p>\u5be6\u5728\u662f Rails \u7684\u539f\u7f6a: \u56e0\u70ba ActiveRecord \u63d0\u4f9b\u5f88\u591a\u597d\u7528\u7684 method\uff0c\u6240\u4ee5\u6211\u5011\u7fd2\u6163\u628a code \u585e\u5230 controller \u751a\u81f3 view \u88e1\u3002\u6b63\u78ba\u7684\u4f5c\u6cd5\u61c9\u8a72\u76e1\u91cf\u91cd\u69cb\u81f3 Model \u4e2d (\u4f8b\u5982\u6709\u8907\u96dc\u53c3\u6578\u7684 find )\uff0c\u6709\u53ef\u8b80\u6027\u3001\u53ef\u6e2c\u8a66(model unit testing)\u8ddf\u597d\u7684MVC\u3002\u8acb\u770b\u9019\u7bc7\u7d93\u5178\u6587\u7ae0 <a href=\"http:\/\/weblog.jamisbuck.org\/2006\/10\/18\/skinny-controller-fat-model\">Skinny Controller, Fat Model<\/a>\u3002\u9019\u88e1\u4f5c\u8005\u53c8\u5728\u63a8\u85a6\u4e86\u4e00\u6b21 <a href=\"http:\/\/www.caboo.se\/articles\/2006\/2\/22\/nested-with_scope\">with_scope<\/a> \u3002<\/p>\n<p><strong>Don&#8217;t Store Objects in the Session<\/strong><\/p>\n<p>\u5225\u9019\u6a23\u505a session[:user]=user\uff0c\u4f60\u7684memory\u5f88\u5feb\u7528\u5b8c\uff0c\u800c\u4e14session\u8cc7\u6599\u8ddfdb\u8cc7\u6599\u4e0d\u4e00\u81f4\uff0c\u8acb\u7528 session[:user_id] = user.id\u3002\u771f\u8981\u5b58 object \u8acb\u88dd Memcached \u5427\u3002<\/p>\n<p><strong>Avoid Heavy Response Processing<\/strong><\/p>\n<p>\u8017\u7528\u6642\u9593\u7684\u64cd\u4f5c\u8acb\u7528 queuing system\uff0c\u5165\u9580\u65b9\u6cd5\u53ef\u7528 rake \u8ddf crob \u81ea\u52d5\u57f7\u884c\uff0c\u9032\u968e\u6709 BackgroundRB, Amazon&#8217;s SQS \u7b49\u3002\u53ef\u4ee5\u8a66\u8a66 <a href=\"http:\/\/agilewebdevelopment.com\/plugins\/acts_as_state_machine\">acts_as_state_machine <\/a>\u9019\u500b plugin \u4f86\u8a2d\u8a08\u81ea\u5df1\u7684\u6392\u7a0b\u7cfb\u7d71\u3002<\/p>\n<p><strong>Use ActiveRecord Mailing<\/strong><\/p>\n<p>\u5bc4\u9001\u5927\u91cf\u90f5\u4ef6\uff0c\u4ecb\u7d39 ar_mailer plugin<\/p>\n<p><strong>Monitor Your Servers<\/strong><\/p>\n<p>\u6700\u57fa\u672c\u7684 exception_notification plugin \u662f production mode \u5fc5\u88dd\uff0c\u4e00\u65e6\u767c\u751f\u4f8b\u5916\u6703\u5bc4\u4fe1\u544a\u77e5\u4f60\u3002\u4f5c\u8005\u9084\u4ecb\u7d39\u4e86\u4e00\u5806\u5de5\u5177\u3002<\/p>\n<p><strong>Don&#8217;t Cut Costs on Hardware<\/strong><\/p>\n<p><strong>Test-Drive<\/strong><\/p>\n<p><strong>The Rest of the List<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 database indexes<\/li>\n<li>Profile your code: ruby-prof gem<\/li>\n<li>\u4e0d\u4e00\u5b9a\u8981\u88dd\u5927\u7684 ImageMagicK\uff0c\u6709\u5c0f\u5de7\u7684 mini-magick \u6216 image_science \u5c31\u53ef\u4ee5\u7e2e\u5716\u4e86\u3002<\/li>\n<li>\u4f7f\u7528 attr_protected \u4f86\u4fdd\u8b77\u91cd\u8981\u6b04\u4f4d<\/li>\n<li>Automate deployment: \u4f7f\u7528 Capistrano or Vlad<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Update: \u539f\u4f5c\u8005\u6709\u6458\u8981\u5728 Massive List of Rails Development Tips\u4e00 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1680-rails-code-review\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u63a8\u85a6 Rails Code Review PDF\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53],"tags":[],"class_list":["post-1680","post","type-post","status-publish","format-standard","hentry","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-r6","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=1680"}],"version-history":[{"count":0,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1680\/revisions"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}