转自: 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 中文英文中英對照的更多相关文章

  1. 【转载】The Elements of Programming Style之代码风格金科玉律

    原始日期: 2017-02-06 16:20 <The Elements of Programming Style >是一本很古老的书.尽管 Fortran 我们不太使用,尽管新奇的语言层 ...

  2. C++編程札記「基礎」

    一直以為自己最擅長的編程語言是C++,那時自己的水平停留在使用C++來實現數據結構中的各種ADT和ACM算法. 創建一個類,必須實現的成員函數 explicit構造函數 對於單參數構造函數,添加exp ...

  3. THE ELEMENTS OF C# STYLE

    |前言 程序员其实艺术家,灵动的双手如行云流水般在键盘上创造着生命的奇迹,我认为代码是有灵魂的.同一个模块,在每个程序员手中所缔造出来的是不相同的. 最终,这个模块或者实现了最初的业务,但是回过头看看 ...

  4. JavaScript DOM 編程藝術(2版) 綜合實例Band js代碼

    function addLoadEvent(func){ var oldonload=window.onload; if(typeof window.onload!='function') { win ...

  5. 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 ...

  6. 浅谈rest風格的接口开发

    简单描述:因为前后端分离,开发完模块之后,接到team leader的指令,我这个渣渣javaer需要给前端人员返回一个接口,具体内容是课程列表json和分类列表json.emmmm,第一次写接口,心 ...

  7. gym101522 [小熊骑士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017

    西瓜队(划掉),Kuma Rider久违的第一场训练,四小时瞎打.jpg A.水题,排序 #include<cstdio> #include<iostream> #includ ...

  8. Linux下串口編程遇到的接收数据错误及原因(0x0d,0x11接收错误)

    摘要:Linux下串口编程遇到的接收数据错误及原因 来源:https://dotblogs.com.tw/k/2012/07/24/73572 近日在调试串口的时候发现,另一设备向我ARM板的串口发送 ...

  9. 再次理解WCF以及其通信(附加一個編程小經驗)

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

随机推荐

  1. RPC 原理的前生今世

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 在校期间大家都写过不少程序, ...

  2. linux系统的 suid/guid简单介绍 linux suid guid

    我们在前面曾经提到过s u i d和g u i d.这种权限位近年来成为一个棘手的问题.很多系统供应商不允许实现这一位,或者即使它被置位,也完全忽略它的存在,因为它会带来安全性风险.那么人们为何如此大 ...

  3. 10.10 dos试验

    一. 实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法: (4)掌握C语言编程初步. 二. 实验内容和要求 编写类似于DOS,UNIX的命令行解释程序 (1 ...

  4. Linux 查看端口占用并杀掉

    1. 查看端口号占用情况: netstat -apn|grep 80 tcp        0      0 10.65.42.27:80              172.22.142.20:627 ...

  5. 转:CSS布局:Float布局过程与老生常谈的三栏布局

    使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloat属性布局.前者适合布局首页,因为 ...

  6. win7 64位系统 Oracle32bit + PL/SQL访问Orale服务,Oracle 11g的安装,中文乱码问题的解决

    前几天装了个Oracle32bit客户端 + PL/SQL连接不上oracle,我安装完打开PL/SQL登录界面跟正常的界面不一样,没有那个连接为Normal.SYSDBA的选项,下面有解释,至于我为 ...

  7. 纠结和郁闷的存在感-关于DirectX与HLSL的矩阵存储方式---转载好文章

    我常常这么大胆的认为,搞科学的人总是喜欢用各种让常人难以理解的复杂方式去处理某些其实可能很简单的事情,这种情况在他自身擅长的.有着诸多对手竞争的领域上极为常见.比如吧,搞DirectX的人用了左手坐标 ...

  8. 【Todo】所有Locked的题目的分析解答

    下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/probl ...

  9. Android实现图片裁切

    介绍 在应用开发中,如果涉及到个人信息,头像一般是不可避免的,类似这种情况,我们就需要用到图片裁切的功能,实现头像裁切,然后上传给服务器. 一般裁切的做法就是图层叠加选取框,然后根据坐标,计算裁切区域 ...

  10. 51nod1678 lyk与gcd

    容斥定理所以可以用莫比乌斯函数来搞.逆向思维答案等于总和减去和他互质的.那么设f[i]=∑a[j] i|j.ans[i]=sum- ∑mo[j]*f[j] 跟bzoj2440那道题挺像的都是利用莫比乌 ...