博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
code style--The Elements Of Programming Style
阅读量:5053 次
发布时间:2019-06-12

本文共 3734 字,大约阅读时间需要 12 分钟。

INTRODUCTION

  1. Write clearly — don’t be too clever.

EXPRESSIONS

  1. Say what you mean, simply and directly.
  2. Use library functions.
  3. Avoid temporary variables.
  4. Write clearly — don’t sacrifice clarity for “efficiency”.
  5. Let the machine do the dirty work.
  6. Replace repetitive expressions by calls to a common function.
  7. Parenthesize to avoid ambiguity.
  8. Choose variable names that won’t be confused.
  9. Avoid the Fortran arithmetic IF.
  10. Avoid unnecessary branches.
  11. Use the good features of a language; avoid the bad ones.
  12. Don’t use conditional branches as a substitute for a logical expression.
  13. Use the “telephone test” for readability.

CONTROL STRUCTURE

  1. Use DO-END and indenting to delimit groups of statements.
  2. Use IF-ELSE to emphasize that only one of two actions is to be performed.
  3. Use DO and DO-WHILE to emphasize the presence of loops.
  4. Make your programs read from top to bottom.
  5. Use IF … ELSE IF … ELSE IF … ELSE … to implement multi-way branches.
  6. Use the fundamental control flow structures.
  7. Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use.
  8. Avoid THEN-IF and null ELSE.
  9. Avoid ELSE GOTO and ELSE RETURN.
  10. Follow each decision as closely as possible with its associated action.
  11. Use data arrays to avoid repetitive control sequences.
  12. Choose a data representation that makes your program simple.
  13. Don’t stop with your first draft.

PROGRAM STRUCTURE

  1. Modularize. Use subroutines.
  2. Make the coupling between modules visible.
  3. Each module should do one thing well.
  4. Make sure every module hides something.
  5. Let the data structure the program.
  6. Don’t patch bad code — rewrite it.
  7. Write and test a big program in small pieces.
  8. Use recursive procedures for recursively-defined data structures.

INPUT AND OUTPUT

  1. Test input for validity and plausibility.
  2. Make sure input cannot violate the limits of your program.
  3. Terminate input by end-of-file or marker, not by count.
  4. Identify bad input; recover if possible.
  5. Treat end of file conditions in a uniform manner.
  6. Make input easy to prepare and output self-explanatory.
  7. Use uniform input formats.
  8. Make input easy to proofread.
  9. Use free-form input when possible.
  10. Use self-identifying input. Allow defaults. Echo both on output.
  11. Localize input and output in subroutines.

COMMON BLUNDERS

  1. Make sure all variables are initialized before use.
  2. Don’t stop at one bug.
  3. Use debugging compilers.
  4. Initialize constants with DATA statements or INITIAL attributes; initialize variables with executable code.
  5. Watch out for off-by-one errors.
  6. Take care to branch the right way on equality.
  7. Avoid multiple exits from loops.
  8. Make sure your code “does nothing” gracefully.
  9. Test programs at their boundary values.
  10. Program defensively.
  11. 10.0 times 0.1 is hardly ever 1.0
  12. Don’t compare floating point numbers just for equality.

EFFICIENCY AND INSTRUMENTATION

  1. Make it right before you make it faster.
  2. Keep it right when you make it faster.
  3. Make it clear before you make it faster.
  4. Don’t sacrifice clarity for small gains in “efficiency”.
  5. Let your compiler do the simple optimizations.
  6. Don’t strain to re-use code; reorganize instead.
  7. Make sure special cases are truly special.
  8. Keep it simple to make it faster.
  9. Don’t diddle code to make it faster — find a better algorithm.
  10. Instrument your programs. Measure before making “efficiency” changes.

DOCUMENTATION

  1. Make sure comments and code agree.
  2. Don’t just echo the code with comments — make every comment count.
  3. Don’t comment bad code — rewrite it.
  4. Use variable names that mean something.
  5. Use statement labels that mean something.
  6. Format a program to help the reader understand it.
  7. Indent to show the logical structure of your program.
  8. Document your data layouts.
  9. Don’t over-comment.

转载于:https://www.cnblogs.com/vinurs/p/3540834.html

你可能感兴趣的文章
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
代码整洁
查看>>
蓝桥杯-分小组-java
查看>>
Java基础--面向对象编程1(类与对象)
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>
javascript:二叉搜索树 实现
查看>>
网络爬虫Heritrix源码分析(一) 包介绍
查看>>
__int128的实现
查看>>
R 读取clipboard内容 (MAC)
查看>>