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 少说废话

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页