Before You Refactor
来源:互联网 发布:java split . 编辑:程序博客网 时间:2024/05/01 13:25
• The best approach for restructuring starts by taking stock of the existing codebase and the tests written against that code. This will help you understand the strengths and weaknesses of the code as it currently stands, so you can ensure that you retain the strong points while avoiding the mistakes. We all think we can do better than the existing system…until we end up with something no better—or even worse—than the previous incarnation because we failed to learn from the existing system’s mistakes.
• Avoid the temptation to rewrite everything. It is best to reuse as much code as possible. No matter how ugly the code is, it has already been tested, reviewed, etc. Throwing away the old code—especially if it was in production—means that you are throwing away months (or years) of tested, battle-hardened code that may have had certain workarounds and bug fixes you aren’t aware of. If you don’t take this into account, the new code you write may end up showing the same mysterious bugs that were fixed in the old code. This will waste a lot of time, effort, and knowledge gained over the years.
• Many incremental changes are better than one massive change. Incremen-tal changes allows you to gauge the impact on the system more easily through feedback, such as from tests. It is no fun to see a hundred test failures after you make a change. This can lead to frustration and pressure that can in turn result in bad decisions. A couple of test failures at a time is easier to deal with, leading to a more manageable approach.
• After each development iteration, it is important to ensure that the existing tests pass. Add new tests if the existing tests are not sufficient to cover the changes you made. Do not throw away the tests from the old code without due consideration. On the surface, some of these tests may not appear to be applicable to your new design, but it would be well worth the effort to dig deep down into the reasons why this particular test was added.
• Personal preferences and ego shouldn’t get in the way. If something isn’t broken, why fix it? That the style or the structure of the code does not meet your personal preference is not a valid reason for restructuring. Thinking you could do a better job than the previous programmer is not a valid reason, either.
• New technology is an insufficient reason to refactor. One of the worst reasons to refactor is because the current code is way behind all the cool technology we have today, and we believe that a new language or framework can do things a lot more elegantly. Unless a cost-benefit analysis shows that a new language or framework will result in significant improvements in functionality, maintainability, or productivity, it is best to leave it as it is.
• Remember that humans make mistakes. Restructuring will not always guarantee that the new code will be better—or even as good as—the previous attempt. I have seen and been a part of several failed restructuring attempts. It wasn’t pretty, but it was human.
- Before You Refactor
- Before You Refactor
- Before You Refactor
- Refactor
- refactor
- Before You Ask
- Before You Begin 开始之前
- CMUSphinx Learn - Before you start
- Before you commit a transaction
- think more before you do!
- Before You Begin phoneME Feature(MR4)
- 106. Creep before you walk. 循序渐进
- Study in Coursera Before you study abroad
- What you should do before you new a project
- 关于refactor
- Code Refactor
- Why you should learn the API before MFC
- Why you should learn the API before MFC
- Collaborative filtering with GraphChi
- 调用程序 submit
- Collaborative filtering with GraphChi
- IOS 预览功能(轻松实现对各种文本、图片等查看)
- Collaborative filtering with GraphChi
- Before You Refactor
- C# GetWindowThreadProcessId用法
- 编程之美 初赛第一场 竞价
- Transformations USACO
- 重定向与跳转的区别
- Webkit对Javascipt的执行优先级
- Collaborative filtering with GraphChi
- 第七周项目一
- 单机模式处理大数据,搜集一些好用的开源利器