什么是重构,什么是只修改代码? [英] What is refactoring and what is only modifying code?

查看:54
本文介绍了什么是重构,什么是只修改代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道重构是改变程序的结构,使其功能不变".我正在与我在大学最后一年的项目中与我一起工作的一些人交谈,我很惊讶他们对重构有更广泛的(为了更好的词)观点.

I know that refactoring is "changing the structure of a program so that the functionality is not changed". I was talking with some of the guys I'm working with on my final year project at university and I was surprised that they have a much more expansive (for want of a better word) view of refactoring.

我认为重构就是提取方法和重命名类之类的事情.他们还建议更改数据结构(如 Java LinkedListArrayList)、更改算法(使用归并排序而不是冒泡排序),甚至重写大块代码作为重构.

I consider refactoring to be things like extracting methods and renaming classes. They also suggested things like changing data structures (like a Java LinkedList to an ArrayList), changing algorithms (using merge sort instead of bubble sort), and even rewriting large chunks of code as refactoring.

我很确定他们是错的,但我无法给出一个很好的理由,因为他们的建议确实改变了程序(并且可能使它变得更好)而没有改变其行为.我是对的,更重要的是,为什么?

I was quite sure that they were wrong, but I wasn't able to give a good reason why because what they were suggesting did change the program (and presumably make it better) without changing its behaviour. Am I right, and more importantly, why?

推荐答案

Martin Fowler 的 "重构:改进现有代码的设计" 可能是参考:

Martin Fowler's "Refactoring: Improving the Design of Existing Code" is perhaps THE reference:

重构是一种受控技术用于改进设计现有的代码库.它的本质是应用一系列小行为保留转换,每一个太小,不值得做".但是累积效应这些转换中的每一个是相当重要.通过在您可以降低风险的小步骤引入错误.你也避免当你在的时候系统坏了进行重组 - 其中允许您逐步重构系统在很长一段时间内时间.

Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which "too small to be worth doing". However the cumulative effect of each of these transformations is quite significant. By doing them in small steps you reduce the risk of introducing errors. You also avoid having the system broken while you are carrying out the restructuring - which allows you to gradually refactor a system over an extended period of time.

重构与单元测试密切相关.在重构之前编写测试,然后您对重构有信心(与测试的覆盖率成正比).

Refactoring goes hand-in-hand with unit testing. Write tests before you refactor and then you have a confidence level in the refactoring (proportional to the coverage of the tests).

一个很好的参考是:关于重构的信息

这篇关于什么是重构,什么是只修改代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆