The Clean Code Blog

The Clean Code Blog

干净代码博客

First

19 May 2014

2014年5月19日

In the first Is TDD Dead? hangout, at time 30:25 @dhh makes a remarkable statement:

首先TDD死了吗?等下,在 30:25时候,@dhh发表了一个引人注目的声明:

“…you’re not done until you also have tests for a piece of functionality – I’m completely on board with that.”

“……只有在对某个功能进行测试之后,才能完成测试——我完全同意这一点。”

I think we can extrapolate from @dhh’s statement that he believes that having tests for a piece of functionality is a matter of professionalism.

我想我们可以从@dhh的声明中推断出,他认为对一个功能进行测试是一个专业的问题。
 

It’s not hard to understand why. The benefits provided by a good test suite are enormous. Let’s just consider a few.

不难理解为什么。一个好的测试套件带来的好处是巨大的。让我们考虑一下。

  • Well designed tests are small isolated snippets of code that call into the system being tested, expecting certain results. A programmer can read the tests to understand what the tested code is supposed to do. So the tests are documents. They are written in a language you understand. They are utterly unambiguous. They are so formal that they execute. And they cannot get out of sync with the application.
  • 精心设计的测试是一小段独立的代码,它们调用正在测试的系统,期望得到某些结果。程序员可以阅读测试来理解测试代码应该做什么。所以测试是文档。它们是用你能听懂的语言写的。它们是完全明确的。他们是如此的正式以至于他们执行。它们不能与应用程序不同步。

That’s pretty close to documentation nirvana. I’ve certainly seen my share of documents that were hard to read, ambiguous, informal, and out of sync with the application. That fact that a good suite of tests cures those ills makes the tests pretty important.

这很接近文档涅盘。当然,我也看到过一些难以阅读、模棱两可、非正式且与应用程序不同步的文档。一套好的测试可以治愈这些疾病,这一事实使得测试非常重要。

Another benefit is design:

另一个好处是设计:

  • Well designed tests force a certain degree of decoupling. Often that decoupling is beneficial to the design of the system.

@dhh has rightly suggested that too much indiscriminate and gratuitous decoupling is deleterious the the design of the code. On the other hand, no one can doubt that well-considered decoupling is beneficial. Tests provide an opportunity for that consideration; and that adds to the importance of the tests.

精心设计的测试会强制一定程度的解耦。通常,这种解耦有利于系统的设计。
@dhh正确地指出,过多的不加区分和不必要的分离对代码的设计是有害的。另一方面,没有人可以怀疑,经过深思熟虑的脱钩是有益的。测试为这种考虑提供了机会;这增加了测试的重要性。

But without a doubt the most important benefit of a good test suite is:

但毫无疑问,一个好的测试套件最重要的好处是:

  • Confidence. A well designed test suite with a high degree of coverage eliminates, or at least strongly mitigates the fear of change. And when you aren’t afraid to change your code, you will clean it. And if you clean it, it won’t rot. And if it doesn’t rot, then the software team can go fast.
  • 自信。一个设计良好、覆盖率高的测试套件可以消除或至少大大减轻对更改的恐惧。当你不怕改变你的代码时,你会清理它。如果你把它清理干净,它就不会腐烂。如果它不腐烂,那么软件团队就可以走得很快。

Whenever I teach a class, no matter the topic, I always ask my students this question:

每当我教一门课,不管是什么话题,我总是问我的学生这个问题:

“Have you ever been significantly slowed down by bad code?”

“你有没有因为糟糕的代码而明显减速?”

The vast majority of programmers say that they have indeed been significantly slowed down by bad code. I mean, honestly, who hasn’t?

绝大多数程序员说,他们确实被糟糕的代码大大减慢了速度。我是说,老实说,谁没有呢?

So it stands to reason that if we keep the code clean, we won’t be slowed down by bad code. And that means a suite of tests is a key to going fast.

因此,如果我们保持代码的干净,就不会因为糟糕的代码而慢下来。这意味着一套测试是快速发展的关键。

Let me state that more strongly. If you have a suite of tests that you can execute quickly, and if you trust that suite of tests enough, then you will not be afraid to change the code. That makes the code flexible.

让我更有力地说明这一点。如果您有一套可以快速执行的测试,并且您足够信任这套测试,那么您就不会害怕更改代码。这使得代码灵活。

For years we’ve thought that flexibility of code was a function of it’s design. We thought that poorly designed code was rigid and hard to change; and that well designed code was flexible and easy to change. And, for what it’s worth, this is true. But nothing makes code easier to change than a quickly executing suite of tests that you trust – nothing.

多年来,我们一直认为代码的灵活性是其设计的一个功能。我们认为设计得不好的代码是刚性的,难以更改,而且设计良好的代码是灵活的,易于更改。值得一提的是,这是真的。但是,没有什么比您信任的快速执行的测试套件更容易更改代码了——没有什么。

How important is that? How important is it that, at all times, you have the confidence that changes to your code haven’t broken anything? How important is it that you keep enough control over your code so that you aren’t afraid to clean it? And how irresponsible is it to have lost that control and to be afraid to make changes – afraid to clean?

这有多重要?在任何时候,你都有信心代码的更改没有破坏任何东西,这有多重要?对代码保持足够的控制以便不害怕清理代码有多重要?失去这种控制,害怕做出改变,害怕清洁,这是多么不负责任的行为?

It seems to me; and apparently it seems to @dhh, that this is pretty important. Indeed, I think it’s critically important. So linking professionalism to a quickly executing suite of trustworthy tests is probably not out of line.

在我看来,这似乎很重要。事实上,我认为这非常重要。因此,将专业性与一套快速执行的可信赖测试联系起来,可能并不过分。

But that brings us to an issue:  Order. When something is critically important, when do you do it? The answer to that is simple and obvious. When something is critically important, you do it first.

但这就引出了一个问题:秩序。当事情非常重要时,你什么时候做?答案简单明了。当某件事非常重要时,你要先做。

发布了88 篇原创文章 · 获赞 33 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/ccmedu/article/details/103929585
今日推荐