The elements of programming style,好程式的要素
軟體工程經驗
本是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 少說廢話
相關文章
- 好遊戲應具備的10個要素遊戲
- 好程式設計師web前端精講 web前端三要素程式設計師Web前端
- go 程式基本要素Go
- WPF style BasedOn base style
- 本地開發好的 SAP Fiori Elements 應用,如何部署到 ABAP 伺服器上?伺服器
- list-style與list-style-type的區別
- Declarative programming宣告性程式設計程式設計
- Blocking ElementsBloC
- 高質量程式碼的三要素(一)
- 高質量程式碼的三要素(二)
- 抓住這些要素,或許能做出一款好遊戲遊戲
- html中list-style-type與list-style的區別HTML
- "reactive programming"的概念React
- weex-style-loader:給你熟悉的style開發體驗
- "sideEffects": [ "dist/*", "esm/**/style/*", "lib/**/style/*", "*.less" ], 的作用是什麼IDE
- 函數語言程式設計functional programming的特點函數程式設計Function
- vue 的 class 與 style 使用Vue
- ArcGIS API for JS 中的styleAPIJS
- 設計一款兒童遊戲 請把控好氛圍塑造的4點要素遊戲
- 面向橫切面程式設計(Aspect Oriented Programming)。程式設計
- 極端程式設計(eXtreme Programming)小結程式設計REM
- GO語言————4.2 Go程式的基本結構和要素Go
- Ruby Code & Style
- API(Application Programming Interface,應用程式程式設計介面)APIAPP程式設計
- 讓你的Qt桌面程式看上去更加native(二):StyleQT
- 小程式與Vue對比·class與style繫結Vue
- 極限程式設計 (Extreme Programming, XP) 的一些想法程式設計REM
- 面向切面程式設計 ( Aspect Oriented Programming with Spring )程式設計Spring
- JavaScript征服Style的五種武器JavaScript
- 好程式碼的定義
- [論文速覽] Separating Style and Content for Generalized Style TransferZed
- list-style-position
- JavaScript style 屬性JavaScript
- vim Google style formatGoORM
- HTML style 屬性HTML
- 極端程式設計(eXtreme Programming,XP)的特點及討論程式設計REM
- 347. Top K Frequent Elements
- [Javascript] Refactor blocking style code to stream style for fetching the stream dataJavaScriptBloC