[转] 編程風格要素-The Elements of Programming Style 中文英文中英對照
转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php
下面的程序風格規則提煉自Brian Kernighan和P. J. Plauger的經典著作《The Elements of Programming Style》(由McGraw Hill出版)。由於原書出版較早並主要針對Fortran語言,本文僅提煉適用於當今主流編程語言的風格要素。
——Clive Studio
1.Write clearly - don‘‘t be too clever.
要寫清楚,不要自作聰明
2.Say what you mean, simply and directly.
簡單、直截了當地表達你的意思
3.Use library functions whenever feasible.
盡可能使用庫函數
4.Avoid too many temporary variables.
不要使用太多的臨時變量
5.Write clearly - don‘‘t sacrifice clarity for “efficiency.”
要寫清楚-不要為所謂的“效率”犧牲清晰度
6.Let the machine do the dirty work.
讓機器去做麻煩的事情
7.Replace repetitive expressions by calls to a common function.
將重復代碼替代為調用通用函數
8.Parenthesize to avoid ambiguity.
使用括號避免歧義
9.Choose variable names that won‘‘t be confused.
選擇沒有歧義的變量名
10.Avoid unnecessary branches.
避免使用不必要的分支
11.If a logical expression is hard to understand, try transforming it.
如果邏輯表達式不好理解,就試著做下變形
12.Choose a data representation that makes the program simple.
采用讓程序更簡潔的數據表達形式
13.Write first in easy-to-understand pseudo language; then translate into whatever language you have to use.
先用易於理解的偽代碼寫;再翻譯成你使用的語言
14.Modularize. Use procedures and functions.
模塊化.使用過程和函數
15.Avoid gotos completely if you can keep the program readable.
只要你能保証程序的可讀性,能不用goto就別用
16.Don‘‘t patch bad code - rewrite it.
不要給糟糕的代碼打補丁 - 重寫就是了.
17.Write and test a big program in small pieces.
編寫以及測試一個大型程序的時候,分塊進行
18.Use recursive procedures for recursively-defined data structures.
使用遞歸過程來處理遞歸定義的數據結構
19.Test input for plausibility and validity.
始終要測試輸入的正確性和有效性
20.Make sure input doesn‘‘t violate the limits of the program.
確保輸入不會超出程序的限制
21.Terminate input by end-of-file marker, not by count.
通過文件結束符來終止輸入,而不是通過記數
22.Identify bad input; recover if possible.
識別錯誤輸入;並盡可能修復
23.Make input easy to prepare and output self-explanatory.
讓輸入容易構造;讓輸出表述清楚
24.Use uniform input formats.
使用統一的輸入格式
25.Make input easy to proofread.
讓輸入容易校對
26.Use self-identifying input. Allow defaults. Echo both on output.
使用輸入提示.接受默認值.並將其顯示
27.Make sure all variable are initialized before use.
確保所有的變量在使用前都被初始化
28.Don‘‘t stop at one bug.
不要因某一bug而停止不前
29.Use debugging compilers.
打開編譯程序的調試選項
30.watch out for off-by-one errors.
小心off-by-one錯誤(http://en.wikipedia.org/wiki/Off-by-one_error)
31.Take care to branch the right way on equality.
注意等價比較的分支
32.Be careful if a loop exits to the same place from the middle and the bottom.
當循環中有多個跳出點時要小心
33.Make sure your code does “nothing” gracefully.
如果什麼都不做,那麼也要優雅地表達這個意思
34.Test programs at their boundary values.
用邊界值測試程序
35.Check some answers by hand.
手工檢查一些答案
36.10.0 times 0.1 is hardly ever 1.0.
10.0乘以0.1很難保証永遠是1.0
37.7/8 is zero while 7.0/8.0 is not zero.
7/8 等於0,而7.0/8.0不等於0
38.Don‘‘t compare floating point numbers solely for equality.
不要簡單地判斷兩個浮點數是否相等
39.Make it right before you make it faster.
先做對,再做快
40.Make it fail-safe before you make it faster.
先使其可靠,再讓其更快
41.Make it clear before you make it faster.
先把代碼弄干淨,再讓它變快
42.Don‘‘t sacrifice clarity for small gains in “efficiency.”
別為一丁點”性能”就犧牲掉整潔
43.Let your compiler do the simple optimizations.
簡單的優化讓編譯器去做
44.Don‘‘t strain to re-use code; reorganize instead.
不要過分追求重用代碼;下次用的時候重新組織一下即可
45.Make sure special cases are truly special.
確保特殊的情況的確是特殊的
46.Keep it simple to make it faster.
保持簡單以使其更快
47.Don‘‘t diddle code to make it faster - find a better algorithm.
不要死磕代碼來加快速度 - 找個更好的算法
48.Instrument your programs. Measure before making “efficiency” changes.
用工具分析你的程序.在做”性能”改進前先評測一下
49.Make sure comments and code agree.
確保注釋和代碼一致
50.Don‘‘t just echo the code with comments - make every comment count.
不要在注釋里僅僅重復代碼 - 讓每處注釋都有價值
51.Don‘‘t comment bad code - rewrite it.
不要給糟糕的代碼做注釋 - 應該重寫它
52.Use variable names that mean something.
采用有意義的變量名
53.Use statement labels that mean something.
使用有意義的語句標簽
54.Format a program to help the reader understand it.
格式化程序讓閱讀代碼的人更容易理解
55.Document your data layouts.
為數據布局撰寫文檔
56.Don‘‘t over-comment.
不要過分注釋
[转] 編程風格要素-The Elements of Programming Style 中文英文中英對照的更多相关文章
- 【转载】The Elements of Programming Style之代码风格金科玉律
原始日期: 2017-02-06 16:20 <The Elements of Programming Style >是一本很古老的书.尽管 Fortran 我们不太使用,尽管新奇的语言层 ...
- C++編程札記「基礎」
一直以為自己最擅長的編程語言是C++,那時自己的水平停留在使用C++來實現數據結構中的各種ADT和ACM算法. 創建一個類,必須實現的成員函數 explicit構造函數 對於單參數構造函數,添加exp ...
- THE ELEMENTS OF C# STYLE
|前言 程序员其实艺术家,灵动的双手如行云流水般在键盘上创造着生命的奇迹,我认为代码是有灵魂的.同一个模块,在每个程序员手中所缔造出来的是不相同的. 最终,这个模块或者实现了最初的业务,但是回过头看看 ...
- JavaScript DOM 編程藝術(2版) 綜合實例Band js代碼
function addLoadEvent(func){ var oldonload=window.onload; if(typeof window.onload!='function') { win ...
- unix network programming volume1 sorce code build and get(UNIX網絡編程卷1第三版)
source code下载地址:unpv13e.tar.gz下载 (也有放一份在google cloud storage) compile 1. ./configure 2. cd lib make ...
- 浅谈rest風格的接口开发
简单描述:因为前后端分离,开发完模块之后,接到team leader的指令,我这个渣渣javaer需要给前端人员返回一个接口,具体内容是课程列表json和分类列表json.emmmm,第一次写接口,心 ...
- gym101522 [小熊骑士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017
西瓜队(划掉),Kuma Rider久违的第一场训练,四小时瞎打.jpg A.水题,排序 #include<cstdio> #include<iostream> #includ ...
- Linux下串口編程遇到的接收数据错误及原因(0x0d,0x11接收错误)
摘要:Linux下串口编程遇到的接收数据错误及原因 来源:https://dotblogs.com.tw/k/2012/07/24/73572 近日在调试串口的时候发现,另一设备向我ARM板的串口发送 ...
- 再次理解WCF以及其通信(附加一個編程小經驗)
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
随机推荐
- linux RTC 驱动模型分析【转】
转自:http://blog.csdn.net/yaozhenguo2006/article/details/6824970 RTC(real time clock)实时时钟,主要作用是给Linux系 ...
- Linux系统时间与RTC时间【转】
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3637782 Linux的RTC驱动相对还是比较简单的,可以将它作为一个普通的字符 ...
- 运用CodeSmith Studio实现C#项目构架
http://www.cnblogs.com/iCaca/category/80950.html http://www.cnblogs.com/BlueBreeze/archive/2011/07/1 ...
- OSX 升级 vim
善用 Homebrew 神器啊,少年!Homebrew - The missing package manager for OS X安装完成后打开终端输入: brew install vim --wi ...
- POJ-3669 Meteor Shower(bfs)
http://poj.org/problem?id=3669 注意理解题意:有m颗行星将会落在方格中(第一象限),第i颗行星在ti时间会摧毁(xi,yi)这个点和四周相邻的点,一个人开始在原点,然后只 ...
- [Golang]使用自建代理访问指定网站
由于爬虫过于频繁访问某一个网站而被禁ip,只因为贪恋一时爽快而忘记了使用代理,这大概是大多数爬虫初学者遇到的问题吧.但是有一些网站不只是爬虫需要访问,人也是需要访问的.这时候就需要使用代理服务器来访问 ...
- 《OD学算法》常用算法集合
1. 排序 (1)冒泡 (2)选择 (3)插入 (4)归并 2. 位运算 Bitmask provide an efficient way to manipulate a small set of B ...
- linux 多处理器概念
Linux 提出了 Multi-Processing 的概念,它的调度器可以将操作系统的线程均分到各个核(或硬件线程)上去执行,以此达到并行计算的目的,从而也可以极大地提高系统的性能. 实现计数器 1 ...
- 【笨嘴拙舌WINDOWS】键盘消息,鼠标消息
键盘消息 Windows系统无论何时只有一个窗口(可能是子窗口,也就是控件)能获得焦点. 焦点窗口通过windows消息来响应人的键盘操作,与键盘相关的常用消息罗列如下: WM_KEYDOWN 按 ...
- JSP:include的flush属性的作用
JSP 中include 另一个文件时有个很偏的属性,叫flush,默认为 false. 在同一个 JSP 中,如果不断 include 自己(源文件),在逻辑上会形成死循环.若默认情况下,服务器 ...