分解/重构程序

发布于 2024-10-31 09:50:54 字数 48 浏览 2 评论 0原文

术语“分解不良”和“重构”程序是什么意思?你能举一个简单的例子来理解基本的区别吗?

What does the term 'poorly factored' and 'refactoring' a program mean? Can you give a simple example to understand the basic difference ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

花开柳相依 2024-11-07 09:50:54

重构是一种通用技术,可以涉及许多任务。它通常意味着清理代码、消除冗余、提高代码质量和可读性。

一个非常简单的分解不良代码示例:

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 行是如何一遍又一遍地重复的?

重构此代码可能会给出:

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 中完成像以前一样分开的地方。

还有其他方法可以重构它,当然,如果您需要对 do_taskn 函数进行更改,这可能行不通,但这通常是您开始的方式...

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);

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);

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.

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

以往的大感动 2024-11-07 09:50:54

分解不良意味着包含冗余,或者组织方式使核心依赖关系难以看到。该术语最初来自数学:

Factoring: Finding what to multiply together to get an expression.

有多种方法可以分解表达式,就像有多种方法可以编写实现相同结果的程序一样。众所周知,代数中,找到合适的因式分解可以使整个方程更容易求解。

Poorly factored means containing redundancies, or organized in a way that makes core dependencies difficult to see. The term initially comes from math:

Factoring: Finding what to multiply together to get an expression.

There are many ways to factor an expression, just as there are many ways to write a program achieving the same result. As we all know form algebra, finding a suitable factoring can make the whole equation much easier to solve.

罗罗贝儿 2024-11-07 09:50:54

分解不良的代码是指以难以理解、维护、重用等方式编写的代码。

重构通常意味着改变一段代码/模块/程序的编写方式,而不实际改变其功能。重构的一些目标包括提高可读性、减少耦合和更轻松地重用代码(即修复分解不良的代码的问题)。

Poorly factored code is code that has been written in such a way as to be difficult to understand, maintain, reuse, etc.

Refactoring generally means to alter the way a piece of code/module/program is written without actually changing its functionality. Some of the goals of refactoring include increased readability, decreased coupling, and easier code reuse (i.e. fixing what's wrong with poorly factored code.)

那些过往 2024-11-07 09:50:54

考虑不周意味着它的制作不精良。而重构是指审查和改进代码。

Poorly factored means it was not well crafted. While refactoring refers to review and improve code.

恰似旧人归 2024-11-07 09:50:54

代码重构包括:

* Clarity: the meaning of the code should be obvious to anyone reading it.  Comments can be refactored as well for adding additional explaination for hairy code, but one should try to make the code itself clear enough to understand

* Eliminate redundant code:  refactoring is the time to recognize patterns and factor them out of your code.  Similar and redundant classes and code blocks should be combined, and unnecessary functions and variables eliminated.

* Modularity and Scalability:  code should be easy to extend or modify, and be able to scale without adding complexity

* Simplicity: Reduce Unnecessary complexity. Complexity should only be considered if it considerably increases readability or modularity ( hence reducing future efforts)

Refactoring of Code Include:

* Clarity: the meaning of the code should be obvious to anyone reading it.  Comments can be refactored as well for adding additional explaination for hairy code, but one should try to make the code itself clear enough to understand

* Eliminate redundant code:  refactoring is the time to recognize patterns and factor them out of your code.  Similar and redundant classes and code blocks should be combined, and unnecessary functions and variables eliminated.

* Modularity and Scalability:  code should be easy to extend or modify, and be able to scale without adding complexity

* Simplicity: Reduce Unnecessary complexity. Complexity should only be considered if it considerably increases readability or modularity ( hence reducing future efforts)
巷雨优美回忆 2024-11-07 09:50:54

基本上使您的代码更加优雅:消除重复,使代码更易于维护且更具可读性。

但是,代码重构可能会过度。过多的时间可能会花在清理代码、使其更具可读性、更健壮等上。如果您在紧迫的期限内工作,这可能会很糟糕,例如一周可能会花在重构代码上。结果是一段代码,它可能“功能上”做同样的事情,但只是更具可读性,或者看起来更漂亮一些。

我发现最好是边重构边重构,偶尔重命名变量、方法等,并在以下情况下进行更大的重构方法:
a) 我知道我会有足够的时间。和
b) 通过重构这部分代码,将获得显着的功能/性能优势。 (超过了时间缺点。)

只是我的经验。

我的同事告诉我阅读 Robert Martin 的《清洁代码》:- http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882 /ref=sr_1_1?s=books&ie=UTF8&qid=1346277262&sr=1-1

Basically making your code more elegant: removing repetition, making code more maintainable, and more readable.

However, it is possible to refactor code too far. Excess amounts of time can be spend cleaning up code, making it more readable, more robust, etc. This can be bad if you are working under tight deadlines for example a week could be spend refactoring code. With the result being a segment of code which may 'functionally' do the same thing, but is only more readable, or looks a little prettier.

I find it best to refactor as I go, the occasional renaming of variables, methods etc and doing the larger methods of refactoring when:
a) I know I'll have enough time. AND
b) By refactoring this section of code, significant functional/ performance benefits will be gained. (Outweighing the time drawback.)

Just my experience.

I have been told by my colleagues to read Clean Code by Robert Martin :- http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?s=books&ie=UTF8&qid=1346277262&sr=1-1

街道布景 2024-11-07 09:50:54

分解不佳意味着它没有经过足够的迭代重构来删除不良的设计模式(即反模式)。

一般来说,这意味着它只是糟糕的代码。

poorly factored means that it has not undergone enough iterative refactorings to remove bad design patterns (ie. anti-patterns).

In general it means it is just poor code.

π浅易 2024-11-07 09:50:54

我喜欢将其视为一个数学隐喻(无论它与字面对应有多么接近,我都不需要说):假设

我在一家名为 504 Industries 的虚构公司工作,我的所有产品都会生成整数 504。我的旗舰产品叫做 Cruncher,每当我的客户运行它时,屏幕上就会打印出数字 504。

有一天,我让审计员检查源代码,发现它有一些技术债务。源代码如下所示: 113+412-(11+9+1)

这确实给出了正确的答案,但我发现很难吸引新人才到我的公司来处理这个源代码代码。我最初对 504 的设想非常简单,那么它是如何实现的呢?

多年来,我的开发人员可以自由地提出这个算法,而无需进行大量的设计监督,并且客户喜欢答案始终是 504。因此,我们不断赚钱,但创建新版本变得更加困难。

最后,我们决定重构我们的代码。因为113+412-(11+9+1)并不直观,而且我们知道原来有一个更简单的过程,我们可以通过一些仔细的发现来重新建立。

当我们的重构完成后,Cruncher 2.0 就准备好了,并且源代码更容易阅读:7*8*9。我们的招聘过程取得了成功,我们甚至创建了一家新公司 3024 Industries,该公司非常巧妙地重用了 504 Industries 的代码。 (我们使用 Cruncher 2.0 的解决方案作为库,将答案乘以 6,并得到我们的新产品 3024,您瞧。我们不需要再发明 7*8*9 代码时间;我们重用它以节省时间和金钱。这是可能的,因为我们可以将任何代码重构为可重用的库,这是今天重构的第二个例子)

正如我们所看到的,尽管我们有一个完全有效的产品,但维护成本却很高。原来是能够以可选的一次性固定工作主体的成本来减少。重构存在着我们可能会失败的风险,因此重构始终是必须谨慎评估的事件。

秉承“越差越好”的理念(或者说,现有且人们可以使用的平庸解决方案优于想象中的完美解决方案),如果我的开发人员愿意的话,我们 504 产品的原始版本实际上可能是更好的解决方案我们永远无法将 113+412-(11+9+1) 重构为 7*8*9

I like to think of it as a math metaphor (and however close it is to a literal correspondence, is not for me to say):

Let's say I work at a fictitious company called 504 Industries and all of my products generate the integer 504. My flagship product is called Cruncher and whenever my customers run it, the number 504 prints up on the screen.

One day, I have auditors inspect the source code and discover that it has some tech debt. The source code looks like this: 113+412-(11+9+1)

This does give the correct answer, but I find it is difficult to attract new talent to my company to work on this source code. My original vision for 504 was quite simple, so how did this come to be?

Over the years, my developers were free to come up with this algorithm without a lot of design oversight, and customers loved that the answer was always 504. So we kept making money, but it became more difficult to create new releases.

Finally, we decided to refactor our code. Because 113+412-(11+9+1) was not intuitive, and we knew there was originally a more simple process, that we could re-establish with some careful discovery.

When our refactor was done, Cruncher 2.0 was ready, and the source code was much easier to read: 7*8*9. Our hiring process became successful, and we even created a new company, 3024 Industries that made some very clever reuse of our code from 504 Industries. (We use our solution for Cruncher 2.0 as a library, multiply the answer by 6, and have our new product 3024, you see. We do not need to invent the 7*8*9 code a second time; we reuse it to save time and money. This is possible because we can refactor any code into a reusable library, a second example of refactoring for today)

As we can see, although we had a perfectly valid product, the cost of maintaining it was able to be reduced at the cost of an optional one time fixed body of work. There was a risk in refactoring it that we may have failed at, and so a refactor is always an event that must be cautiously evaluated.

With the notion that 'Worse is Better' (or, that mediocre solutions that exist and people can use, are superior to perfect solutions that are imaginary), our original version of the 504 product in fact may have been the superior solution if my developers were never able to refactor 113+412-(11+9+1) as 7*8*9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文