编程的一般规则

设计过程
1 编程的本质是:没有意外,最小化耦合,最大化内聚

2 根除复杂性这个恶魔(Part 1)


2.1 不要解决不存在的问题
2.2 解决一个问题,而不要解决一类问题


3 A user interface should not look like a computer program (the transparency principle)

3 用户接口看起来不应该象一个计算机程序(透明原则)
4 Don't confuse ease of learning with ease of use

4 不要混淆易于使用和易于掌握这两个概念
5 Productivity can be measured in the number of keystrokes

5 生产率可以通过击键次数来衡量
6 If you can't say it in English, you can't say it in C/C++

6 如果你不能用自然语言表达,也就不能用C/C++表达
6.1 Do the comments first

6.1 先写注释
7 Read code

7 阅读代码


7.1 There's no room for prima donnas in a contemporary programming shop


8 Decompose complex problems into smaller tasks

8 将复杂的问题分解为多个小任务
9 Use the whole language (Use the appropriate tool for the job)
10 A problem must be thought through before it can be solved

10 解决问题之前应该深思熟虑
11 Computer programming is a service industry

11 计算机编程是服务行业
12 Involve users in the development process

12 开发过程应该有用户参与
13 The customer is always right

13 客户永远是正确的
14 Small is Beautiful. (Big == slow)

14 小即是美(大==慢)


一般开发问题
15 First, do no harm

15 第一,不要有害
16 Edit your code

16 编辑你的代码
17 A program must be written at least twice

17 一个程序应该至少写两次
18 You can't measure productivity by volume

18 不能用代码量衡量生产率
19 You can't program in isolation

19 编程不能与世隔绝
20 Goof off

20 别把编程太当回事
21 Write code with maintenance in mind梩he maintenance programmer is you

21 写代码的时候应该时刻想着维护,假设你自己就是维护者


21.1 Efficiency is often a bugaboo

21.1 追求效率往往导致其它问题


格式化和文档
22 Uncommented code has no value

22 没有注释的代码没有价值
23 Put the code and the documentation in the same place

23 将代码和文档放在一起
24 Comments should be sentences

24 注释应该是完整的句子
25 Run your code through a spelling checker

25 你的代码应该能通过拼写检查
26 A comment shouldn't restate the obvious

26 不要注释显而易见的东西
27 A comment should provide only information needed for maintenance

27 注释中应该只提供对维护有帮助的内容
28 Comments should be in blocks

28 使用块注释
29 Comments should align vertically

29 注释应该垂直对齐
30 Use neat columns as much as possible

30 代码应该尽可能的整齐的分列
31 Don't put comments between the function name and the open brace

31 不要在函数名和第一个花括号之间写注释
32 Mark the ends of long compound statements with something reasonable

32 在较长的代码块之后做标记性注释
33 Put only one statement per line

33 每行只写一条语句
34 Put argument names in function prototypes

34 函数原型中应该写上参数名
35 Use a 損redicate?form to split up long expressions
36 A subroutine should fit on a screen

36 一个子函数的长度应该在一屏以内
37 All code should be printable

37 所有的代码都应该是可打印的
38 Use lines of dashes for visual separation between subroutines

38 使用整行连字符来隔离子函数代码
39 White space is one of the most effective comments

39 空白是最好的注释之一
40 Use four-space indents

40 使用四个空格做缩进
41 Indent statements associated with a flow-control statement

41 缩进所有与一个流程控制语句相关的语句


41.1.Comments should be at the same indent level as the surrounding code

41.1 注释应该与其对应的代码一同缩进


42 Align braces vertically at the outer level

42 在外层垂直对齐花括号
43 Use braces when more than one line is present under a flow-control statement

43 在流程控制语句下面如果有多于一条的语句,应该使用花括号

名字和标识
44 Names should be common English words, descriptive of what the function, argument, or
variable does

44 名字应该是普通英文单词,用来描述这个函数、参数或者变量的作用

44.1.Do not clutter names with gibberish

44.1 不要乱起名字(无意义的,深奥的,不贴切的)


45 Macro names should be ENTIRELY_CAPITALIZED

45 宏名字应该形如:ENTIRELY_CAPITALIZED


45.1 Do not capitalize members of an enum

45.1 不要将枚举类型成员大写
45.2 Do not capitalize type names created with a typedef

45.2 不要将用typedef定义的类型大写


46 Avoid the ANSI C name space

46 避免ANSI C名字空间
47 Avoid the Microsoft name space

47 避免微软名字空间
48 Avoid unnecessary symbols

48避免不必要的符号
49 Symbolic constants for Boolean values are rarely necessary

49 布尔型符号常量基本上没有用


一般编程原则
50 Don't confuse familiarity with readability

50 熟悉代码与代码的可读性好是两回事
51 A function should do only one thing

51 一个函数只应该完成一件事
52 Too many levels of abstraction or encapsulation are as bad as too few

52 过多或者过少的抽象或者封装层次都不好
53 A function should be called more than once, but?br>

53 一个函数应该被多处调用

53.1 Code used more than once should be put into a function

53.1 在多于一处使用的代码应该变成一个函数


54 A function should have only one exit point

54 一个函数应该只有一个退出点


54.1 Always put a return at the outer level

54.1 总是在外层放一个return


55 Avoid duplication of effort

55 避免费两遍事的事情
56 Don't corrupt the global name space

56 不要污染全局名字空间


56.1 Avoid global symbols

56.1 避免全局符号
56.2 Never require initialization of a global variable to call a function

56.2 对函数的调用不应该依赖全局变量是否被初始化


56.2.1 Make locals static in recursive functions if the value doesn't span a recursive call

56.2.1 在第归函数中,如果一个变量与第归调用无关,则其应该是局部静态变量


56.3 Use instance counts in place of initialization functions

56.3 在初始化函数中使用实例计数
56.4 If an if ends in return, don't use else

56.4 如果if语句中有return,则不要再写else


57 Put the shortest clause of an if/else on top

57 最短的if/else子句在最上面
58 Try to move errors from run time to compile time

58 尽量将运行时刻的错误提前到编译时刻
59 Use C function pointers as selectors

59 使用C 函数指针作为选择器
60 Avoid do/while loops

60 避免do/while循环


60.1 Never use a do/while for a forever loop

60.1 不要用do/while实现无限循环


61 Counting loops should count down if possible

61 循环计数应该从大到小
62 Don't do the same thing in two ways at the same time

62 在同一时间不要用不同的方法完成同一件事
63 Use for if any two of an initialization, test, or increment are present

63 如果初始化、测试、增量三者有两个,就应该用for语句
64 If it doesn't appear in the test, it shouldn't appear in the other parts of for statement

64 没有在for语句的测试部分出现的变量,也不应该出现在其他两个部分
65 Assume that things will go wrong

65 总是假定事情要出错
66 Computers do not know mathematics

66 计算机不懂得数学


66.1 Expect the impossible

66.1 总是会出现不可能的事情
66.2 Always check error-return codes

66.2 检查错误返回代码


67 Avoid explicit temporary variables

67 避免显式临时变量
68 No magic numbers

68 不要出现纯数字
69 Make no assumptions about sizes

69 不要对size做假定
70 Beware of casts (C issues)

70 对类型转换要小心(C语言问题)
71 Handle special cases directly

71 立即处理特殊情况
72 Don't try to make lint happy

72 用不着试图让lint不报告问题
73 Put memory allocation and deallocation code in the same place

73 分配内存和释放内存应该在一起
74 Heap memory is expensive

74 堆内存是昂贵的
75 Test routines should not be interactive

75 测试过程不应该是交互的
76 An error message should tell the user what's right

76 错误信息应该告诉用户什么是正确的
77 Don't print error messages if an error is recoverable

77 如果一个错误是可恢复的,就不要打印错误信息
78 Don't use system-dependent functions for error messages

78 不要在错误信息中使用系统相关的函数


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值