CS106A 编程方法学(三)

第三课

原来死循环的英文名字不是“Dead Loop”,而是“Infinite Loop”。

Mehran Sahami 讲了一个关于死循环的段子,说曾经有一个芯片公司(名称不可考)号称自己开发出了当时世界上最快的芯片,广告词里说:“……执行一个死循环只需要 2.5 秒……”

而真实情况是,一旦执行死循环,那么芯片的温度会迅速的升高,以至于融掉……

OBOE, off-by-one error/bug is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many of or too few. Usually this problem arise when a programmer fails to take into account that a sequence starts at zero rather than one, or makes mistakes such as using “is less than or equal to” where  “is less than” should have been used in a comparison.

… part of good programming style is all of your programs up at the top should have a comment that say what the name of the file is and has a little bit of an explanation about what your program actually does.

以前我也觉得应该写详细的注释,后来的确遇到了维护注释和代码一致性的问题;后来接触了敏捷方法论,开始认为好的代码比注释更能说明问题。不过最近又开始觉得,在写“好”代码的同时,加一些简单明确的注释,应该也是不错的选择。

在程序文件的顶部写清楚文件的名字,代码段所想要完成的功能,前置条件和后置条件,以及修改的人员和名称应该是比较合适的。当然这部分内容可以在版本管理工具中注明,但是感觉上不如直接写在代码文件上更加清晰。

如何分解 decomposition 问题?primitives ,逐步求精stepwise refinement,自顶向下 top down design

其实我一般比较习惯 bottom up design,比如喜欢先把底层的数据库访问代码写好,然后再写业务逻辑层。其实也许自顶向下也是不错的选择,这样可以避免在开始阶段写无用的代码(“以后可能会用的着”)。

Mehran Sahami 说有心理研究表明,从自底向上的思维方式转为自顶向下需要 100 个小时。

one of methods should basically solve one problem

guide line for good methods:

  1. solve 1 program
  2. 1- 15 line per method
  3. good name
  4. comments

posted on 2012-03-11 19:20  zhaorui  阅读(463)  评论(0编辑  收藏  举报

导航