- 浏览: 164816 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
GunChin:
有些复杂,看得不是很懂
RAILS -
shellfish:
恩,红帽默认的SELinux的级别是强制,这个一般我不大用,装 ...
华思服务器一个奇怪问题的解决方法 -
机器人:
你说得太好了了了了了了了 子 啊啊啊啊,呼啦啦。
GIT handbook -
hbxiao135:
能介绍下 fat free crm的 流程分析吗?
(CRM)customer relationship management sysetm
Since December, Rails has undergone a fairly significant internal refactoring in quite a number of areas. While it was quite tricky at first, we mere mortals have started to hone a process for diving into a new area of the codebase and emerging some time later with a much improved area that does basically the same thing. Here’s the approach we’ve adopted and advocate:
First, refactoring needs to be refactoring, not revision. By that I mean that while you are in the process of invasively improving some code, it is not the appropriate time to also change the functionality of that code. If you do both at the same time, it will be difficult to track down whether a bug in the code is the result of refactoring or functionality changes. We’ve held fast to this requirement for the Rails 3 work Carl and I have been doing, which has resulted in an extremely stable edge, despite making fairly invasive changes.
Second, any kind of significant refactoring without tests is folly. The first thing you should do is take a look at the test suite for the area in question and beef it up if necessary. Thankfully, Rails has a fairly reasonable test suite, and the addition of Sam Ruby’s Agile Web Development on Rails test suite has provided an additional level of confidence in the changes we’re making
Third, once you’re ready to dive in, read through the code carefully. It can be tempting to just go in and hack away at a particularly egregious part of the codebase, but you’ll frequently be changing code that exists for a reason. Something I’ve noticed both in Rails applications and in Rails itself is that code that looks very strange at the beginning of a period of refactoring tends to exist for a reason.
Fourth, as you proceed, make very small changes, then run the full test suite after every change. Commit often. What you want to look for is cases where the boundary APIs around the code you’re writing are messy (so you have multiple ways in to a particular class or area of code where one would suffice). One Rails example would be rendering a template in ActionView from ActionController. When I started in December, ActionController called into ActionView using a number of public and private APIs, so making any changes around those boundaries was very tricky. Some things we wanted to do, like improve the way layouts were selected, was too complex because of the number of ways templates and layouts were rendered. The very first thing I did in the early days of the merge was work toward reducing the number of ways that ActionController told ActionView to render a template.
In the end, we settled on just a single API: render_template_from_controller, which takes a Template object from the template to render, and a Template object for the layout. Once this was done, it became a lot easier to make changes on either side of the boundary, without fear that a small change in ActionView could break any number of things in ActionController. Of course, this assumes that you understand what your boundaries are. This is something that’s learned over time, but a fundamental requirement in good refactoring is having functionality broken up into units that are easy to understand, with small surface area. This is commonly achieved using classes, which is a good starting point, but Ruby has other tricks up its sleeve as you get more advanced, like judicious use of modules (and the new Rails ActiveSupport::Concern).
Fifth, once you have reasonable boundaries, dive in and start making changes. A pretty good rule of thumb is to clean up cases where a public API has started being used for private, internal use. This might mean that changing the internals of your code breaks the public functionality (which, again, should be sacrosanct during this process). Have a zero-tolerance policy for failing tests as you make small changes, especially as you separate out public and private functionality. One example of this in Rails was extensive usage of ActionView’s public render method by private functionality. As a result, the public render method had snippets of code inside to handle special cases (like render :file taking a Template object). The solution in this case was to extract out the private functionality, and have the public render method as well as the private internals call the new extracted methods. This ensures that internal functionality is kept internally, where it can be refactored more easily.
Sixth, don’t be afraid to git reset --hard if you find yourself sinking into quicksand, with rising confusion due to changes you made. Over the course of working with Rails, I’ve lost an hour or more at a time to changes made too rapidly and carelessly, and the only advice I can give is to give up on ratholes as early as you notice them. So that’s it. Six easy steps to refactoring Rails.
发表评论
-
actionmailer发送邮件失败的问题解决记录
2011-05-24 14:12 2342我们公司的WLAN网管采用ruby on rails架构,同时 ... -
ROR rake
2009-10-20 14:05 1072原文: Ruby on Rails Rake Tuto ... -
控制器内部对请求的操作
2009-10-20 13:41 1877控制器内部对请求的操作 一Action方法 1 ... -
在rails环境中直接执行sql语句而不需要创建MODEL
2009-09-23 10:54 2993标准格式是:ActiveRecord::Base.connec ... -
rails 中简单的创建保存一条信息
2009-07-20 09:43 729Securitylog.new( # :ses ... -
uninstall all gems
2009-07-14 11:31 886GEMS=`gem list --no-versions` ... -
rails 拖拉排序效果demo
2009-06-30 10:55 1358要為Ruby on Rails程式加入拖曳排 ... -
something about with_scope
2009-06-23 10:53 1171今天看了一点关于with_scope的知识,有点感觉,写点东西 ... -
Add jQuery datagrids to your Rails applications
2009-06-03 10:39 1532Add jQuery datagrids to your Ra ... -
Multiple Attachments in Rails
2009-06-03 10:19 1827Multiple Attachments in Rails ... -
REST on Rails之自定义路由
2009-06-03 10:03 1693REST on Rails之自定义路由 from L ... -
自定义will_paginage输出
2009-06-03 09:55 794自定义will_paginage输出 from Le ... -
Rails Cookie测试
2009-06-03 09:48 1084Rails Cookie测试 from Le ... -
配置ActionMailer使用GMail发送邮件
2009-06-03 09:45 1462配置ActionMailer使用GMail发送邮件 ... -
rails 记住跳转前的url
2009-06-02 13:44 1389def store_location ses ... -
给Non-ActiveRecord Objects进行validate
2009-06-02 09:33 895对于非ActiveRecord对象的Validation,我们 ... -
file_column简单的照片上传
2009-04-21 18:19 9731.file_column git clone git:/ ... -
cookie + js动态修改iframe 父窗口的链接参数
2009-04-21 18:12 2911取得 由于最近自己做的项目中采用了目前较为流行的经典左右结构 ... -
git 冲突解决
2009-04-21 17:57 5175比较笨的办法就是直接改本地的代码 先git pull ,他 ... -
git 下gw来查看团队的修改
2009-04-07 15:46 864首先开启一个命令行,在命令行中输入 alias gw= ...
相关推荐
This book is for C# and ASP.NET developers who want to learn how to effectively manage and modify their code with refactoring tools and features. Wrox Professional guides are planned and written by ...
### Refactoring to Patterns #### 重要概念:Refactoring(重构) **重构**是一种软件开发过程中常用的技巧,旨在改进现有代码结构而不改变其外部行为。这一过程可以帮助开发者提高代码的可读性和可维护性,同时...
一个支持VS2005和2008的重构工具,很好用的Refactor tool,但是只支持C#和VB.NET,安装完毕以后在菜单栏会出现一个“重构”选项,点击鼠标右键也可以看见!语言和你的VS一致! 附上源网址,感兴趣的可以自己去看...
### Refactoring to Patterns #### 一、概述 《Refactoring To Patterns》这本书由Joshua Kerievsky编写,是关于重构技术与设计模式相结合的重要参考资料。本书深入探讨了如何通过重构来实现更好的软件设计,特别...
系统重构架构设计,A guide to refactoring, the process of changing a software system so that it does not alter the external behavior of the code yet improves its internal structure, for professional ...
1. Refactoring; 2. Design Patterns; 3. Refactoring to Patterns; All chm format books with high quality!
"Refactoring" was conceived in Smalltalk circles, but it wasn't long... The key to keeping code readable and modifiable is refactoring—for frameworks, in particular, but also for software in general.
### 6. 替换条件计算为策略(Replace Conditional Calculations with Strategy) 策略模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换。这使得算法可以独立于使用它的客户端变化。书中阐述了在...
简体中文版,djvu格式清晰版,格式体积特别小,易于下载阅读。本文档开头没有封面及出版社印刷版次等信息,因是清晰电子版,仅供大家参考之用,以减少出版社之投诉。大家可以下载对照参悟。djvu格式的打开,可以用...
《Rails反模式:最佳实践与Ruby on Rails重构》是一本由Chad Pytel和Tammer Saleh撰写的书籍,深入探讨了在Ruby on Rails开发过程中常见的反模式,并提供了相应的最佳实践和重构策略。本书旨在帮助开发者识别并避免...
### 重构与模式:理论与实践的融合 #### 书籍概览 《重构与模式》是一本由Joshua Kerievsky编写的书籍,由Addison Wesley出版社于2004年8月5日出版,ISBN号为0-321-21335-1,全书共400页。该书旨在将重构技术和...
#### Refactoring to Resources 讨论了如何利用CRUD(Create, Read, Update, Delete)和RESTful设计原则来优化Rails应用,使其更加符合现代Web开发的最佳实践。 #### Mongrel: Serving, Deploying and Extending ...
Refactoring provides a de facto behavior-preserving approach to eliminate these anomalies. However, manually determining and performing useful refactorings is a formidable challenge, as stated in the...
The fourth edition of this award-winning classic has been reorganized and refocused so it's more useful than ever before for developers new to Ruby and Rails. Rails 3 is a major release--the changes...
《Refactoring UI》是一本由Adam Wathan和Steve Schoger合著的设计书,主要面向开发者,帮助他们在不具备专业设计背景的情况下也能提升UI设计的质量。这本书提供了实用的设计建议和技巧,帮助开发者在日常工作中更好...
一共12个包,全下载解压 本书开创性地深入揭示了重构与模式这两种软件开发关键技术之间的联系,说明了通过重构实现模式改善既有的设计,往往优于在新的设计早期使用模式。本书不仅展示了一种应用模式和重构的创新...