This post was originally published on Medium
這篇文章最初發表于Medium
Writing tests represents one of those few stages of software development that is usually overlooked, even though it may be one of the most important one. Developers mention it and usually are either uncomfortable and prefer not to write tests for their code, or have many excuses for not writing them at all.
編寫測試代表了通常被忽略的那幾個軟件開發階段之一,盡管它可能是最重要的階段之一。 開發人員會提到它,通常會感到不舒服,他們不愿意為他們的代碼編寫測試,或者有很多借口根本不編寫代碼。
Well, tests are important:
好吧,測試很重要:
A software bug in a Therac-25 radiation therapy machine caused the death of five patients after receiving a massive dose of X-rays.
Therac-25放射治療機中的軟件錯誤在接受大量X射線照射后導致五名患者死亡。
Knight Capital lost half a billion dollars in half an hour when a software bug allowed computers to sell and buy millions of shares with no human oversight.
騎士資本(Knight Capital)在半小時內損失了10億美元,當時一個軟件漏洞允許計算機在無人監督的情況下買賣計算機。
These and many other stories tell how seemingly unimportant bugs can actually cause so many tragic disasters.
這些故事以及其他許多故事都說明,看似不重要的錯誤實際上會導致如此多的悲劇性災難。
We know how critical software is becoming to every area of our lives. We know that the security, stability and correctness of this software has a crucial importance in our lives. So the question is: why do developers still avoid writing tests?
我們知道關鍵軟件在我們生活的各個領域都變得越來越重要。 我們知道,該軟件的安全性,穩定性和正確性對我們的生活至關重要。 所以問題是:為什么開發人員仍然避免編寫測試?
借口1:“筆試只會花太多時間。” (Excuse #1: “Writing tests just takes too much time.”)
It’s easy to perceive writing tests as taking too much time, as opposed to jumping directly into the implementation part.
很容易將編寫測試視為花費大量時間,而不是直接跳到實現部分。
This may be true in the short term, but if we take into consideration the time you may potentially need to fix all the bugs you could avoided by writing tests, then writing tests can actually save time — and even money — along the way.
在短期內這可能是正確的,但是如果考慮到時間,您可能需要修復通過編寫測試可以避免的所有錯誤,那么編寫測試實際上可以節省時間,甚至節省金錢。
Tests do not eliminate bugs completely (nothing can do that), but they can dramatically reduce them. They can even make you feel more confident that you’re not breaking any existing functionality when you add new features and refactor old ones.
測試不能完全消除錯誤(沒有辦法做到),但是可以大大減少錯誤。 它們甚至可以使您更有信心,在添加新功能并重構舊功能時不會破壞任何現有功能。
Tests also help you save time and protect your existing implementations from inexperienced programmers who recently joined your team. If these newcomers introduce bugs, your tests will fail. And when your tests fail, you become aware that something went wrong.
測試還可以幫助您節省時間,并保護您的現有實現免受最近沒有經驗的程序員(加入您的團隊)的侵害。 如果這些新手引入了錯誤,則您的測試將失敗。 當測試失敗時,您會意識到出了點問題。
Project managers who lack a programming background — and thus may not understand the programming complexities that may arise all of a sudden — tend to have high expectations of the developers on their teams. They want to things done quickly, and fast production-ready code. They may consider it unreasonable to postpone a strict deadline.
缺乏編程背景的項目經理(因此可能無法理解突然出現的編程復雜性)往往對團隊中的開發人員抱有很高的期望。 他們希望快速完成事情,并快速準備生產代碼。 他們可能認為推遲嚴格的截止日期是不合理的。
These situations may put you into tough situations — where you have to choose between following best practices or getting things done quick-and-dirty.
這些情況可能會使您陷入困境–在這種情況下,您必須選擇遵循最佳實踐或快速完成工作。
You should try and find a few professional principles you can stick to no matter what. You should try your best to convince your manager of the importance of best practices, and how they pay off in the long run. And if your manager can’t be convinced, then you may consider changing jobs.
您應該嘗試找到一些無論如何都可以堅持的專業原則。 您應該盡力使您的經理相信最佳做法的重要性,以及從長遠來看它們如何產生收益。 而且,如果無法說服您的經理,那么您可以考慮換工作。
借口2:害怕編寫測試 (Excuse #2: the fear of writing tests)
It’s no surprise that developers spend most of their working hours either reading code, writing code, or discussing new problems to solve with code. As a result, they have a very strong emotional attachment to it, and prefer to treat it as their precious property.
開發人員將大部分工作時間花費在閱讀代碼,編寫代碼或討論要用代碼解決的新問題上也就不足為奇了。 結果,他們對它有著強烈的情感依戀,并喜歡將其視為自己的寶貴財產。
Many are confident that they’re already experienced enough, and that they are capable of covering every possible scenario without much effort.
許多人相信自己已經有足夠的經驗,并且能夠輕松應對各種可能的情況。
On the inside, they may harbor a sense of insecurity. And subjecting their code to tests may cause that insecurity to flare up.
在內部,他們可能懷有不安全感。 對他們的代碼進行測試可能會導致這種不安全感加劇。
Maybe they pushed their code changes to the master branch faster than they should have, because they want to look like a productive employee to their manager. And now they’re afraid that writing tests might reveal bugs in their code. They’re afraid of being exposed as average programmers who cannot write bug-free code.
也許他們將代碼更改推送到master分支的速度比他們應有的速度快,因為他們希望在經理中看起來像是富有成效的員工。 現在,他們擔心編寫測試可能會發現其代碼中的錯誤。 他們害怕被無法編寫無錯誤代碼的普通程序員所接受。
Well, we should all do ourself a favor and never let our own sense of ego delude us into not writing tests. As uncomfortable as it may feel, we should take responsibility for our own work. This is one of the best ways to avoid bugs — bugs which can often have tragic consequences.
好吧,我們都應該幫自己一個忙,不要讓我們自己的自我意識欺騙我們不編寫測試。 盡管感覺不舒服,我們應該為自己的工作負責。 這是避免bug的最佳方法之一,而bug往往會帶來悲劇性的后果。
Tests allow you to make big changes in your code quickly, since you can be confident that everything is working correctly. When you’re used to writing tests for your code, you will then usually be able to finish off your work a lot faster, as you will have instant visual feedback when something is failing as you will see a red light. As a result, you will also write better code, feel less stress, and eventually get promoted, as you’re ultimately bringing in more value with the work that you are doing.
測試使您可以快速地對代碼進行重大更改,因為您可以確信一切正常。 當您習慣于為代碼編寫測試時,通常可以更快地完成工作,因為當出現故障時您會立即獲得視覺反饋,因為您會看到紅燈。 結果,您還將編寫更好的代碼,減輕壓力,并最終獲得晉升,因為您最終將通過所做的工作帶來更多價值。
抽出時間進行測試 (Make time for testing)
Tests will never be able to catch 100% of bugs, but they will contribute to the security, stability and correctness of your code. Make time for them.
測試將永遠無法捕獲100%的錯誤,但是它們將有助于代碼的安全性,穩定性和正確性。 為他們騰出時間。
This post was originally published on Medium
這篇文章最初發表于Medium
翻譯自: https://www.freecodecamp.org/news/2-most-frequent-reasons-why-developers-avoid-writing-tests-e13fc74ee2ab/