[转] 編程風格要素-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 ...
随机推荐
- 如何实现上下左右键盘控制焦点使之落在相邻文本框或下拉框中-Web开发/JavaScript
我用jquery只实现了文本框的移动(暂时上下移动等同于左右移动) $(function () { var cols = 1;//按一下跳几个控件 var obj = $("input[id ...
- Android实现网络多线程文件下载
实现原理 (1)首先获得下载文件的长度,然后设置本地文件的长度. (2)根据文件长度和线程数计算每条线程下载的数据长度和下载位置. 如:文件的长度为6M,线程数为3,那么,每条线程下载的数据长度为2M ...
- Ubuntu 14.04怎样升级到Ubuntu 14.10
Ubuntu 14.04怎样升级到Ubuntu 14.10 Ubuntu 14.10 Utopic Unicorn 将在10月23日正式发布,9月25日最终测试版本已经发布,Ubuntu 14 ...
- Xcode使用版本
Xcode6中创建分类.协议等文件的方法 Xcode 5.1.1 与 Xcode 6.0.1 的共存之路 http://jingyan.baidu.com/article/1612d500457df1 ...
- Tomcat遇到”Error listenerStart”或”Error filterStart”问题且无详细日志时的log配置.
昨天部署web应用到Tomcat之后,无法成功启动,并且控制台没有详细的错误信息,顶多就两行提示信息,例如:严重: Error listenerStart严重: Context [/lizongbo] ...
- Unable to locate package update
碰到这个问题后找到这个帖子就转了过来 当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是: ...
- 配置域名服务器报错named[822]: dns_rdata_fromtext /etc/bind/db.asertest.com mail not a valid number
问题描述: 为了配置邮件服务器,更改了相关域名,改完后,重启bind9报错 Mar 17 14:39:39 DnsServer2 named[822]: dns_rdata_fromtext: /et ...
- mac出现一个白条
mac出现一个白条,除了finder没有任何程序运行,出现好几次了,怎么解决? 打开finder输中文出现 按esc键
- PHP5.3, PHP5.4, PHP5.5新特性
PHP 5.3中的新特性 1. 支持命名空间 (Namespace) 2. 支持延迟静态绑定(Late Static Binding) 3. 支持goto语句 4. 支持闭包.Lambda/Anony ...
- PHP实现站点pv,uv统计(三)
数据分析脚本如下: //error_reporting(0);date_default_timezone_set("PRC");$environment = get_cfg_var ...