[转] 編程風格要素-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 ...
随机推荐
- Win XP 如何禁用屏保
如果你试过 “在桌面空白处点击右键-[属性]-[屏幕保护程序],选择[无],点击[确定]”后,当时是可以去掉屏保.但如果重启计算机或者从待机状态唤醒后,屏保依然会出现,那么你可以试试下面的方法. 首先 ...
- UVA 11427 Expect the Expected (期望)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&pa ...
- Hadoop学习过程知识积累
1. Hadoop 64位centos 下编译 hadoop 2.6.0 源码 2. 学习问题汇总 hadoop多次格式化后,导致datanode启动不了 3. HDFS Hadoop中NameNod ...
- oracle数据库导入导出命令!
Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 大多情况都可以用Oracle数据导入导出完成数据的备份和还原(不会造成数据的丢 ...
- [转载]流式大数据处理的三种框架:Storm,Spark和Samza
许多分布式计算系统都可以实时或接近实时地处理大数据流.本文将对三种Apache框架分别进行简单介绍,然后尝试快速.高度概述其异同. Apache Storm 在Storm中,先要设计一个用于实时计算的 ...
- bzoj2396: 神奇的矩阵
与51nod1140一样.不过这题是多组数据的...坑.... #include<cstdio> #include<cstring> #include<cctype> ...
- volley(2) 参数code : or_barcode, pr_ismsd:false , method:GET
1. 来自于WHCombineBatchFragment.java /** * 当编辑框里面的内容完成的时候,自动的,同时获取服务器的批量数 */private void barcodeEnterEv ...
- JAVA 日志管理
http://www.360doc.com/content/10/0704/05/2017726_36768082.shtml http://javacrazyer.iteye.com/blog/11 ...
- PhpStorm 获取注册码
另一种方法是:直接用浏览器打开 http://idea.lanyus.com/ ,点击页面中的“获得注册码”,然后在注册时切换至Activation Code选项,输入获得的注册码一长串字符串,便可以 ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...