Skip to Content
成為卓越程式設計師的38項必修法則
book

成為卓越程式設計師的38項必修法則

by Pete Goodliffe
May 2015
Intermediate to advanced
376 pages
5h 48m
Chinese
GoTop Information, Inc.
Content preview from 成為卓越程式設計師的38項必修法則
86
| 第十章:獵殺 Bug
這些測試程式也是很好的除錯階段起始點。比起完全運行程式,而且有一系列的動作要
執行才能重現錯誤,簡單、可重現的單位測試案例容易多了。因此,建議你編寫單位測
試來證明 bug,而不要執行“整個系統”來獵殺它。
當你有一套測試項時,考慮採用
程式碼覆蓋
工具來檢視測試程式覆蓋了多少程式碼。你
可能會很驚訝。簡單的法則是:如果你的測試套件無法執行它,你就不能相信它可以動
作。就算它目前看起來 OK,沒有測試套件的話,之後它也很有可能會出錯。[Page-80]
重點
測試的程式碼是 bug 的溫床。測試是你的消毒水。
當你最後發現 bug 的原因時,考慮編寫一個簡單的測試程式,清楚地描述問題,並在你
真正修程式碼之前,將它添加到測試套件。這真的需要紀律,因為當你發現罪魁禍首的
程式碼時,自然會想要盡快解決它,並發布修改版。請先編寫測試控管來說明問題,並
使用這個套件來證明你已經修正它。測試程式有助於避免 bug 未來再度出現。
投資尖端工具
有很多工具值得你使用,包括記憶體檢查程式,如 Electric Fence,及瑞士刀工具,如
Valgrind。它們都值得
馬上
學習,而不是到了火燒屁股才去學。如果你在遇到問題之前
就已經知道如何使用工具,會更有效率。
學習一系列的工具可避免你用鑽子打開堅果。
當然,除錯工具冠軍是
除錯程式
。這種工具可讓你闖入正在執行的程式,單步執行,或
進入及離開函式。其他方便的功能包括觀察變數的改變、設定條件中斷點(例如 "break
if x > y"),及即時改變變數值來快速試驗不同的程式路徑。有一些先進的除錯程式甚至 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習|使用Python

精通機器學習|使用Python

Sarah Guido, Andreas C. Mueller
Java性能优化实践:JVM调优策略、工具与技巧

Java性能优化实践:JVM调优策略、工具与技巧

Benjamin J. Evans, James Gough, Chris Newland

Publisher Resources

ISBN: 9789863475699