`
xf986321
  • 浏览: 164786 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

6 Steps To Refactoring Rails (for Mere Mortals)

阅读更多

 

      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.

分享到:
评论

相关推荐

    Professional Refactoring in C# & ASP.NET

    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 to Patterns #### 重要概念:Refactoring(重构) **重构**是一种软件开发过程中常用的技巧,旨在改进现有代码结构而不改变其外部行为。这一过程可以帮助开发者提高代码的可读性和可维护性,同时...

    Resource Refactoring Tool for C#/VB.net

    一个支持VS2005和2008的重构工具,很好用的Refactor tool,但是只支持C#和VB.NET,安装完毕以后在菜单栏会出现一个“重构”选项,点击鼠标右键也可以看见!语言和你的VS一致! 附上源网址,感兴趣的可以自己去看...

    Refactoring To Patterns

    ### Refactoring to Patterns #### 一、概述 《Refactoring To Patterns》这本书由Joshua Kerievsky编写,是关于重构技术与设计模式相结合的重要参考资料。本书深入探讨了如何通过重构来实现更好的软件设计,特别...

    Refactoring

    系统重构架构设计,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 ...

    Refactoring, Pattern, and Refactoring to Pattern

    1. Refactoring; 2. Design Patterns; 3. Refactoring to Patterns; All chm format books with high quality!

    Refactoring-Improving the Design of Existing Code.pdf

    "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.

    refactoring to patterns

    ### 6. 替换条件计算为策略(Replace Conditional Calculations with Strategy) 策略模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换。这使得算法可以独立于使用它的客户端变化。书中阐述了在...

    重构与模式(Refactoring to Patterns)

    简体中文版,djvu格式清晰版,格式体积特别小,易于下载阅读。本文档开头没有封面及出版社印刷版次等信息,因是清晰电子版,仅供大家参考之用,以减少出版社之投诉。大家可以下载对照参悟。djvu格式的打开,可以用...

    Addison.Wesley.Rails.AntiPatterns.Best.Practice.Ruby.on.Rails.Refactoring

    《Rails反模式:最佳实践与Ruby on Rails重构》是一本由Chad Pytel和Tammer Saleh撰写的书籍,深入探讨了在Ruby on Rails开发过程中常见的反模式,并提供了相应的最佳实践和重构策略。本书旨在帮助开发者识别并避免...

    Refactoring To Patterns《重构与模式》英文版

    ### 重构与模式:理论与实践的融合 #### 书籍概览 《重构与模式》是一本由Joshua Kerievsky编写的书籍,由Addison Wesley出版社于2004年8月5日出版,ISBN号为0-321-21335-1,全书共400页。该书旨在将重构技术和...

    the rails way

    #### Refactoring to Resources 讨论了如何利用CRUD(Create, Read, Update, Delete)和RESTful设计原则来优化Rails应用,使其更加符合现代Web开发的最佳实践。 #### Mongrel: Serving, Deploying and Extending ...

    A genetic algorithm-based approach for automated refactoring of component

    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...

    [Agile.Web.Development.with.Rails]4rd英文版(清晰文字版pdf+源码)

    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 中文版

    《Refactoring UI》是一本由Adam Wathan和Steve Schoger合著的设计书,主要面向开发者,帮助他们在不具备专业设计背景的情况下也能提升UI设计的质量。这本书提供了实用的设计建议和技巧,帮助开发者在日常工作中更好...

    重构与模式(Refactoring to patterns).part03.rar

    一共12个包,全下载解压 本书开创性地深入揭示了重构与模式这两种软件开发关键技术之间的联系,说明了通过重构实现模式改善既有的设计,往往优于在新的设计早期使用模式。本书不仅展示了一种应用模式和重构的创新...

Global site tag (gtag.js) - Google Analytics