问题描述
不好的因子和重构这个术语是什么意思?
What does the term 'poorly factored' and 'refactoring' a program mean? Can you give a simple example to understand the basic difference ?
推荐答案
重构是一种可以引用许多任务的通用技术。
Refactoring is a general technique that can refer to many tasks. It usually means cleaning up code, removing redundancy, improving code quality and readability.
这是一个非常简单的例子:
A very simple example of poorly factored code:
do_task1("abc");
do_task2(123);
do_task3(7.43);
...
//100 lines later:
do_task1("abc");
do_task2(123);
do_task3(7.43);
...
//80 lines later:
do_task1("abc");
do_task2(123);
do_task3(7.43);
查看同一组3行是如何重复重复的?
See how the same set of 3 lines is repeated over and over and over?
重构此代码可能会得到:
Refactoring this code might give:
procedure do_tasks1to3(x,y,z)
do_task1(x);
do_task2(y);
do_task3(z);
end
do_tasks1to3("abc",123,7.43);
...
//100 lines later:
do_tasks1to3("abc",123,7.43);
...
//80 lines later:
do_tasks1to3("abc",123,7.43);
重构代码使用一个过程来执行重复任务,如果 do_task4
需要添加,它只需要在过程中完成,而不是像以前一样在4个单独的位置。
The refactored code makes use of a procedure to perform the repetitive tasks, and if a do_task4
ever needs to be added, it only needs to be done inside the procedure, not in 4 separate places like before.
是重构这个的其他方法,当然如果你需要有变化的 do_taskn
函数这可能不工作,但这通常是如何开始.. 。
There are other ways to refactor this, and of course if you ever needed to have variance to the do_taskn
functions this might not work, but this is usually how you'd start...
这篇关于保理/重构程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!