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項必修法則
24
|
第三章:少寫一些程式碼!
複製程式碼並非都是不好的行為或懶惰程式員犯下的錯誤。複製也有可能是意外情況下
造成的,因為某人不知道程式已經存在,而重新發明車輪。或者,也有可能是有人重新
建構一個第三方程式庫已經擁有的函式。不好的地方在於,比起新寫下的程式,既有的
程式庫更有可能是正確的,而且已經 debug 過了。使用共用的程式庫可以節省你的精
力,並防止你進入一個錯誤隱伏的世界。
有一些微程式碼等級的重複行為。例如:
if (foo) do something();
if (foo) do_something_else()
if (foo) do_more();
全都可以整潔地放在一個
if
陳述式裡面。通常許多迴圈都可以刪減為一個迴圈。例如下
列程式:
for (int a = 0; a < MAX; ++a)
{
// 處理事情
}
// 烤奶油吐司
for (int a = 0; a < MAX; ++a)
{
// 處理其他事情
}
或許可以改為:[Page-22]
for (int a = 0; a < MAX; ++a)
{
// 處理事情
// 處理其他事情
}
// 烤奶油吐司
如果烤奶油吐司不依賴任何一個迴圈的話,你不但更容易閱讀與理解,可能也會有更好
的效能,因為它只需要執行一個迴圈。另外,考慮以下重複的條件式:
if (foo)
{
if (foo && some_other_reason)
{
// 2 次檢查 foo 是多餘的
}
}
死碼
|
25
或許你不是刻意寫出這種程式,但是經過維護之後,許多程式碼會變成這種草率的結 ...
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