编程小贴士

给你的编程提供小点子


什么是重构,什么不是重构

有时候,会有程序员跑到我这里说他们不喜欢某个东西的设计,“我们需要给它来个全面的重构”,来纠正里面的错误。哦,哦。这听起来可不是个好主意。而且这听起来也不是重构…

重构(Refactoring)这个词最初由Martin Fowler 和 Kent Beck给下的定义,它是

一种修改,使软件的内部结构更容易理解,在不改变软件的可见行为方式前提下使软件更容易变更…它是一种有节制的整理代码、使bug产生几率最小化的方法。

重构的结果是引用了快捷方法、去除了重复代码和死代码,使设计和逻辑更加清晰。是在更好的、更聪明的使用编程语言。是在优势利用你现在 知道、但当时的开发程序员并不知道——或并没有加以利用的信息。不断的简化代码,让它们更容易理解。不断的使它们在将来的变更变得更容易、更安全。

在这个过程中发现了bug、修改bug,这不是重构。优化不是重构。强化异常捕捉、增加预防性代码不是重构。让代码更容易测试不是重构——尽管重构能达到相同的效果。这些所有的事都是有益的。但这些都不是重构。

程序员,特别是做维护工作的程序员,清理代码是他们的日常工作之一。这是基本工作,是必须要做的。Martin Fowler等人的贡献是使重构代码的最佳实践方法格式化,并把常见的、证明切实有效的重构模式——重构的目标和重构的步骤——进行归档分类。

重构很简单。尽可能在写代码前先写测试能够防止你犯错误。小规模的、独立的、稳妥的对代码进行结构上的调整,每次调整完后都要进行测 试,确保你没有改变代码的行为特征——功能和以前一样,只是代码上看着不同。重构模式和现代化的IDE里的重构工具使重构变得容易、安全和代价低廉。

不要为了重构而重构

重构可以被当成一种能给你的代码变更带来帮助的措施。代码重构应该在你进行代码变更前进行,这样能让你确信你对代码理解了,使你更容 易、更安全的把变更引入代码。对你的重构动作进行回归测试。然后进行纠正或变更。再次测试。之后可能需要对更多的代码进行重构,使你代码变更的意图变得更 加清晰。再次进行全面测试。重构,再变更。或变更,然后重构。

你不是为了重构而重构,你重构是因为你想做其它的事情,而重构能帮助你完成这些事情。

重构的范围应该受你需要实施的代码变更或代码修正来决定——为了让代码变更更安全和更简洁,你应该做些什么?换句话说:不要为了重构而重构。不要对那些你不打算进行变更或不会变更的代码进行重构。

为理解而做简略重构(Scratch Refactoring)

Michael Feather的《Working Effectively with Legacy Code》这本书里提到了简略重构(Scratch Refactoring)的概念;Martin Fowler称之为“为理解而重构”。这是用来对付那些你不理解的(或不能忍受的)代码,清理它们,这样在你打算真正动手修改它前,你能对它们是干什么的 有了更好的理解,同样也对你debug这些代码有帮助。一旦你能清楚了一个变量或方法的真正意图,重命名它们,给它们一个更合适的名称,删除那些你不喜欢 看的(或觉得没有用的)代码,拆解复杂的条件语句,把长程序分解成数个容易理解的小程序。

不要惦记着复查或测试这些改动。这是为了让你的重构快速的推进——这能让这些代码以及它们的运行原理在你的大脑里产生一个快速但不完备 的原型。从中学习,然后丢掉它们。简略重构还能让你尝试各种不同的重构途径,学到更多的重构技巧。Michael Feathers建议说,在这个过程中要留意那些看起来没什么用处、或者特别有用的东西,这样当你完成此练习后、要真正修改它们时,才能把事情做正确—— 修改时一点一点来,讲究方法,边修改边测试。

什么是“大规模”重构?

对代码进行简单的但又明显的重构:消除重复,修改变量和方法名称使其更有意义,提炼方法使代码更易懂、更易复用,简化条件逻辑,把无意义的数字换成命名的变量,把相似的代码集中到一起。通过这些重构,在代码的可理解性和可维护性上,你能得到巨大的回报。

相对于这些较小的、行内的重构,更加重大的设计上的重构与之有明显差异——这就是Martin Fowler所指的”大型重构”。大的、代价很高的变动,附带有大量的技术风险。这不是你编程过程中的清理代码和设计改进:这是根本性的重新设计。

有些人喜欢把对一个系统的重新设计或重写或重新搭建平台或返工叫“大规模重构”。因为技术上讲,这些并不改变软件功能特征——业务逻 辑、软件输入和输出仍和以前一样,“只是”设计和代码实现变了。它和常规重构的区别看起来就是:一个是重写了一段代码,一个是重写了一个系统,只要你是一 步一步做下来的,你都可以称之为“重构”——不管你是长年累月被困于将一个老系统换成新代码,还是对系统架构进行大规模的改造。

“大规模重构”会变的很糟糕。你可能需要花数周、数月(甚至数年)才能完成,需要你对软件的很多部分进行改动。软件会因此不能运行,需 要分多次发布这些变更,需要你做临时的台架(scaffolding)和变通方案——尤其是你采用短周期的敏捷开发方法时。这时Branch by Abstraction这样的实践方法就派上用场了,它能帮你在长周期内管理代码中的变化。

而且在开发新代码的同时你还要维护旧代码,这使得代码版本控制很麻烦,变更起来不方便,致使代码很脆弱,易犯错——这正和重构所预期的 目的背道而驰。有时这样的情况会一直持续下去——这种新旧代码交替的过程永远不能完成,因为能获得最大利益的部分都是最先完成,或者因为最初带来这个想法 的顾问已经干别的去了,或者是预算被消减,而且你也讨厌维护这样一个拖拉的项目。

这些是重构——那些不是

在这种重型的项目开发过程中混入重构的概念是不对的。它们从根本上就是另外一种工作,带有完全不同的开发成本和风险。它混淆了人们对什么是重构、重构能干什么的认识。

重构可以、也应该融入到你写代码或维护代码的过程中——作为日常开发/质量管理的组成部分,就像写测试和代码审查一样。重构应该被安静 的,持续的和低调的完成。它需要我们把工作精力分出一部分给它,它需要在我们的工期评估和风险评估中考虑到它的存在。如果做的正确,你不需要去解释或向外 人验证这部分工作。

花几分钟、一两个小时做重构,就像是你开发过程中的一种修改,是工作的一部分。如果它让你花了数天时间,或者更长,那不是重构;那是重 写,或重新设计。如果你需要明确的留出一部分时间(或整个sprint周期)来重构代码,如果需要为清理代码而申请批准,或把清理代码作为一个开发需求, 那你不是在重构——即使你用了重构的技术和工具,你仍然做的是另外一种工作。

有些程序员认为对代码进行根本的、重大的修改是他们的权利和义务,在重构的名义下进行重新设计、重写,为了将来,也不辜负自己的技艺。重新设计和重写有时候是你正确的该做的事情。但出于坦诚和表述清楚,请不要把这些活动赋以重构的名义。

71 Responses to “ 什么是重构,什么不是重构 ”

  1. chocopie说道:

    NqOzss I’а†ve read several excellent stuff here. Certainly value bookmarking for revisiting. I wonder how a lot attempt you put to make this type of magnificent informative site.

  2. chocopie说道:

    GzAyKh Thank you for your article post.Really thank you! Awesome.

  3. chocopie说道:

    pvX7ri What a funny blog! I truly loved watching this comic video with my family unit as well as with my mates.

  4. You have made some really good points there. I looked on the net for additional information about the issue and found most individuals will go along with your views on this site.

  5. free movies说道:

    Spot on with this write-up, I honestly think this website needs far more attention. I all probably be back again to see more, thanks for the info.

  6. terrific website But wanna state which kind of is traditionally genuinely useful, Regards to consider your time and effort you should this program.

  7. colors for kids说道:

    short training method quite a lot to me and also also near our position technicians. Thanks; on or after all people of us.

  8. Im obliged for the article post.Much thanks again. Want more.

  9. Wow, great article post.Thanks Again. Keep writing.

  10. Oluwadamilare说道:

    Very neat article post.Much thanks again.

  11. visit说道:

    Thanks for the blog.Really looking forward to read more. Will read on

  12. I truly appreciate this blog.Really looking forward to read more. Great.

  13. web page说道:

    This website was how do you say it? Relevant!! Finally I ave found something which helped me. Many thanks!

  14. w88 world说道:

    This awesome blog is definitely awesome and diverting. I have discovered helluva handy things out of this blog. I ad love to return over and over again. Thanks a lot!

  15. woking cab说道:

    Thanks, I ave been searching for details about this subject for ages and yours is the best I ave found so far.

  16. Very informative article post.Really thank you! Really Cool.

  17. Muchos Gracias for your article post. Much obliged.

  18. Major thankies for the post. Keep writing.

  19. There is noticeably a lot of funds comprehend this. I assume you have made certain good points in functions also.

  20. Nice blog here! Also your site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol

  21. klm voucher说道:

    I value the post.Really thank you! Fantastic.

  22. Super-Duper website! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also

  23. P. Diddy说道:

    This excellent website certainly has all the information and facts I needed about this subject and didn at know who to ask.

  24. This can be exactly what I was looking for, thanks

  25. Your style is so unique in comparison to other folks I ave read stuff from. Thank you for posting when you have the opportunity, Guess I all just bookmark this page.

  26. tools for sales说道:

    Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn at show up. Grrrr well I am not writing all that over again. Anyway, just wanted to say great blog!

  27. I value the post.Really looking forward to read more. Great.

  28. Kickboxing说道:

    Wonderful post! We are linking to this great post on our site. Keep up the good writing.

  29. Your style is really unique in comparison to other people I ave read stuff from. Thank you for posting when you have the opportunity, Guess I will just book mark this web site.

  30. Blogging说道:

    I was looking for the report in Yandex and suddenly came across this page. I found a little information on my topic of my report. I would like more, and thanks for that..!

  31. womensfit说道:

    Thank you for sharing this great post. Very interesting ideas! (as always, btw)

  32. Just what I was looking for, regards for posting.

  33. Really appreciate you sharing this blog article.Really looking forward to read more. Great.

  34. Thanks for the blog post.Really looking forward to read more. Much obliged.

  35. Crochet basico说道:

    Wow! Thank you! I continually needed to write on my blog something like that. Can I include a part of your post to my site?

  36. This awesome blog is definitely interesting additionally diverting. I have found a bunch of interesting things out of it. I ad love to return over and over again. Cheers!

  37. online说道:

    Thanks for the article post.Much thanks again. Will read on

  38. white shoes说道:

    Many thanks for sharing this good write-up. Very interesting ideas! (as always, btw)

  39. It as not that I want to replicate your internet site, but I really like the design. Could you let me know which style are you using? Or was it especially designed?

  40. School news说道:

    Your style is so unique in comparison to other people I have read stuff from. Thank you for posting when you have the opportunity, Guess I will just book mark this web site.

  41. Exams说道:

    That is a great tip especially to those fresh to the blogosphere. Short but very precise info Thanks for sharing this one. A must read post!

  42. This is a really great study for me, Ought to admit that you just are a single of the best bloggers I ever saw.Thanks for posting this informative post.

  43. It seems that you are doing any distinctive trick.

  44. Moskito说道:

    Precisely what I was looking for, appreciate it for posting.

  45. Wow, superb blog layout! How lengthy have you been blogging for? you make blogging look straightforward. The all round look of one as webpage is excellent, let alone the content material!

  46. Very good information. Lucky me I found your site by accident (stumbleupon). I ave bookmarked it for later!

  47. Muchos Gracias for your article.Much thanks again. Keep writing.

  48. Apple Reparatur说道:

    say it. You make it entertaining and you still care for to keep it smart.

  49. Thanks for the post.Thanks Again. Much obliged.

  50. Real estate说道:

    The issue is something which too few people are speaking intelligently about.

  51. Open Heavens说道:

    Thanks for writing such a good article, I stumbled onto your site and read a few posts. I like your style of writing

  52. Say, you got a nice article post.Much thanks again. Much obliged.

  53. find more here说道:

    Nice post. I learn something totally new and challenging on sites I stumbleupon everyday. It as always useful to read articles from other authors and practice something from their websites.

  54. You made some clear points there. I did a search on the topic and found most guys will approve with your site.

  55. This site was how do you say it? Relevant!! Finally I have found something that helped me. Thanks a lot!

  56. Thank you for your blog post.Really looking forward to read more. Really Cool.

  57. know more说道:

    Very interesting information!Perfect just what I was looking for! The only gift is a portion of thyself. by Ralph Waldo Emerson.

  58. check out说道:

    Some truly wonderful work on behalf of the owner of this web site , absolutely outstanding subject matter.

  59. The loans may also be given at very strict terms as well as any violations will attract huge penalties super real property tax

  60. to learn more说道:

    Really enjoyed this blog article.Much thanks again. Great.

  61. I’а†ve recently started a website, the info you offer on this site has helped me greatly. Thank you for all of your time & work.

  62. This is one awesome article.Much thanks again. Really Cool.

  63. Say, you got a nice post.Really thank you! Really Cool.

  64. premium ads说道:

    the home as value, homeowners are obligated to spend banks the real difference.

  65. I simply could not depart your web site before suggesting that I actually enjoyed the usual info a person provide for your guests? Is gonna be again regularly to investigate cross-check new posts

  66. You have brought up a very fantastic points, appreciate it for the post.

  67. I will right away grab your rss feed as I can not find your email subscription link or e-newsletter service. Do you ave any? Please let me know in order that I could subscribe. Thanks.

  68. dong ho seiko 5说道:

    Thank you for your article.Much thanks again.

  69. go there说道:

    Very good post. I absolutely love this website. Continue the good work!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>