The elements of programming style,好程式的要素

weixin_42849849發表於2020-09-26

軟體工程經驗

本是Fortran語言時代的軟體工程經驗總結,感覺多數條目也適合使用任何語言開發專案。

條目

Write clearly - don’t be too clever 清晰是最重要的,不要耍聰明

Say waht you mean, simply and directly 詞要達意

Use library functions 不要重造輪子

Avoid temprorary variables 避免濫用臨時變數

Write clearly - don’t sacrifice clarity for “efficiency” 清晰性總是第一位的,比效能重要

Let the machine do the dirty work 又髒又累、繁瑣的工作讓機器做

Replace repetitive expressions by calls to a common function 程式碼複用、重構

Parenthesize to avoid ambiguity 使用括號消除歧義

Choos variable names that won’t be confused 選擇變數名稱

Avoid the Fortran arithmetic IF

Avoid unnecessary branches

Use the good freatures of a language; avoid the bad ones 擇善而用

Don’t use conditional braches as a substitute for a logical expression

Use the “telephone test” for readability 選用清晰的邏輯表達

Use DO-END and indenting to delimit groups of statements

Use IF-ELSE to emphasize that only one of two actions is to be performed

Used DO and DO-WHILE to emphasize the presence of loops

Make your programs read from top to bottom 保證程式從上至下閱讀,慎用GOTO

Use IF…ELSE IF…ELSE IF…ELSE… to implement multi-way branches

Use the fundamental control flow constructs 使用語言提供的本徵控制結構

Write first in an easy-to-understand pseudo-language’ then translate into whatever language you have to use 易於理解的虛擬碼

Avoid THEN-IF and null ELSE

Avoid ELSE GOTO and ELSE RETURN

Follow each decision as closely as possible with its associated action 將功能和語義相近的程式碼儘可能的放在一起

Use data arrays to avoid repetitive control sequences 使用迴圈簡化重複

Choose a data representation that makes the program simple 選擇和調整資料結構,以使程式簡單

Don’t stop with your first draft 第一個草稿不能用

Modularize. Use subroutines 模組化

Make the coupling between modules visible 模組耦合清晰

Each module should do one thing well 一次只做好一件事

Make sure eavery module hides something 資訊隱藏

Let the data structure the program 資料決定程式整體結構,以資料為中心

Don’t patch bad code - rewrite it 重寫爛程式碼

Write and test a big program in small pieces

Use recursive procedures for recursively-defined data structures

Test input for validity and plausibility 一定要檢查輸入

Make sure input cannot violate the limits of the program 檢查輸入在合理範圍內

Terminate input by end-of-file or marker, not by count

Identify bad input; recover if possible

Treat end of file condtions in a uniform manner

Make input easy to prepare and output self-explanatory

Use uniform input formats

Make input easy to proofread

Use free-form input when possible

Use sefl-indentifying input. Allow defaults. Echo both on output

Localize input and output in subroutines

Make sure all variables are initialized before use 保證所有變數使用前初始化

Don’t stop at one bug BUG多多

Use debugging compilers

Initialize contants with DATA statements or INITIAL attributes; initialize variables with executable code 顯式的初始化常量

Watch out for off-by-one errors 小心多一錯誤

Take care to branch the right way on equality 小心處理變數相等情形

Avoid mutiple exits from loops

Make sure your code “does nothing” gracefully

Test programs at their boundary values 測試程式碼邊界

Program defensively 防禦程式設計

10.0 times 0.1 is hardly ever 1.0

Don’t compare floating point numbers just for equality 避免比較兩個浮點數是否相等

Make it right before you make it faster 調優之前要保證程式碼正確

Keep it right when you make it faster 調優過程中要保證程式碼正確

Make it clear before you make it faster 調優過程中要保證程式碼清晰

Don’t sacrifice clarity for small gains in “efficiency” 不要為蠅頭小利犧牲清晰性

Let your compiler do the simple optimizations

Don’t strain to re-use code; reorganize instead 處處重構

Make sure special cases are truly special

Keep it simple to make it faster 優化過程中要保持簡單性

Don’t diddle code to make it faster - find a better algorithm 找一個更好的演算法

Instrument your program. Measure before making “efficiency” changes 測量是優化的基礎

Make sure comments and code agree 註釋避免驢脣不對馬嘴

Don’t just echo the code with comments - make every comment count

Don’t comment bad code - rewrite it 不註釋爛程式碼,重寫之

Use variable names that mean something 變數名字要選好,望文生義最好

Use statement labels that mean something

Format a program to help the reader understand it 美化程式碼

Indent to show the logical structure of a program 美化程式碼

Document your data layouts

Don’t over-comment 少說廢話

相關文章