原题: 4.When programmers add new elements to an enumeration, they sometimes forget to add new cases to the appropriate switch statements. How could you use assertions to help detect this problem? 附录中的答案: . . . default: ASSERT(FALSE); /*We should never…
整理下目录,看了这个文件,幸好未删除. 以下是<write solid code>中的原文摘录. 1.How could I have prevented this bug? 2.How could I have automatically detected this bug? 3.If a programmer believes that a bug can simply "go away", or that fixing bugs "later" w…
Code Complete 读后总结和新的扩展阅读计划 用了一年时间终于将代码大全读完了,在这里做一个简单的总结,并安排下一阶段的扩展阅读计划. 1.选择代码大全作为我程序员职业入门的第一本书,我认为是很正确的.这本书语言生动,擅于用隐喻说明深刻的编程原理,虽然并不是写给新手看的,但作为一个新手,我依然从中学到了很多.    2.这本书有很多一针见血的论述:设计是一个启发式过程:编程的最大难题是解决其复杂性:要深入一种语言编程而不是在一种语言上编程,等等.    3.实践是这本书的核心,它提供了…
Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is used. e.g. Bad code: public List<int[]> getThem(){ List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1…
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return Codes Separate the normal operations with error handlings. e.g. Bad code: public class DeviceController { ... public void sendShutDown() { DeviceHand…
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abstractions and expose function that operate on that data. Data structure expose their data and hava no meaningful functions. Procedural code(code using…
The Purpose of Formatting Code formatting is about communication, and communication is the professional developer's first order of business. Vertical Formatting File size: less than 200~500 lines. The Newspaper Metaphor We would like a source file to…
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments is to compensate for our failure to express ourself in code. Truth can only be found in one place: the code. Comments Do Not Make Up for Bad Code Rath…
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Functions shouldn't be large enough to hold nested structures. The indent…
文中说明memset可以通过操作整形以加速程序执行速度,这一点值得肯定,问题在于unicore或arm中协处理器有地址访问对齐检查,如果我们如此操作,编译器最终使用str指令来完成,那么当地址未对齐时将会出现错误. 上次和同学看了下rtems的编译器对于strncpy的实现,由于rtems的编译器本身做了大量优化,当stncpy的拷贝的长度较小时使用了编译器内嵌的实现版本(str完成),而未使用libc库的实现版本(strb),由于使能了地址对齐访问检查,所以程序运行后总是会运行错误.…