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.

但這就引出了一個問題:秩序。當事情非常重要時,你什麼時候做?答案簡單明瞭。當某件事非常重要時,你要先做。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章