Code Complete, Chapter 8:Defensive Programming
来源:互联网 发布:排列组合公式算法c50 编辑:程序博客网 时间:2024/05/17 04:58
Designing how to handle errors and exception is a kind of "high level design", which should be performed during the phase of system design. There are basically three types of attitude to handle the invalid inputs for the programming viewpoint.
1 Garbage in, garbage out
2 Garbage in, message out.
3 No garbage allowed in.
From the softwate viewpoint, when it meets a error, there are two ways to handle it.
1 Correctness. It means never returning an inaccurate result. The system would stop to proceed.
2 Robustness. It means always trying to do something to allow the system keep operating. (Mirosoft XP)
When design the general apporach of handling errors, it is useful to try to find out a "saved room", outside the room are the preconditions that if entering the room, all the precondition should be meets. Inside the room , error should be handled by "exception mechanism". Assertions are used to build the "Barricade", something like firewall. "try-catch" blocks are used to build the "Exception mechanism". One approach to handle exception is making "Exception mechanism" centerlized.
Error-handling typically checks the bad input data; Assertions check for bugs in the code where the preconditions are not satisfied.
To find out the "saved room", identifying which are the public interfaces is important, as the public interfaces are the place begin to get input and thus probably get bad input. In a class, the private methods can be always assumed that "no garbage in".
There are two types of code existing during the software life, development code and production code. In the development code both the debug codes and exception-handling error are existing; But in the production code, most of the debug code need to be removed, at least all the "assertions" need to be switched off. So don't make any function depending on assertions.
Tips: It is useful to create some scripts or precompiler to handle switching on and off the debug codes.
Tips: Generally speaking, for any given error, aroutine will use either an assertion or error-handling code, but not both.
Tips: Remember assertion never handle an error, it just make warning when error taking place.
Tips: The decison of how to handle error is designed, then this way should be undertaken consistently.
Tips: Private methods don't do any type of error handling, so assertions normally used heavily in this kind of methods.
Tips: Too mcuh defensive programming creates problems of it own.
Tips: Production code should handle erros in a more sophisticated than "garbage in, garbage out".
Here are error handling techniques, which should be depending on the software is required to be correctness or robustness.
-Return a neutral value.
-Substitute the next piece of valid data.
-Return the same answer as the previous time.
-Substitute the closest legal value.
-Log a warning message to a file.
-Return an error code.
-Call an error processing routine/object.
-Display an error message wherever the error is encountered.
-Handle the error in whatever way works best locally.
-Shutdown.
- Code Complete, Chapter 8:Defensive Programming
- the thinker of code complete -- Defensive Programming
- [Code Complete] CH8 Defensive Programming 防御式编程
- CODE COMPLETE 2e Chapter 8&9
- Defensive Programming
- Defensive programming
- assert vs Defensive programming
- defensive programming 工业标准
- Code Complete 读书笔记 (Chapter 1)
- Code Complete读书笔记(Chapter 2)
- CODE COMPLETE 2e Chapter 6
- CODE COMPLETE 2e Chapter 7
- CODE COMPLETE 2e Chapter 5
- CODE COMPLETE 2e Chapter 10
- CODE COMPLETE 2e Chapter 11.1
- Code Complete 读书笔记(Chapter 14 Organizing Straight-line Code)
- Code Complete 读书笔记 (Chapter 15 Using Controls)
- Code Complete 读书笔记 (Chapter 16 Controlling Loops)
- VS2005中文输入法自动转换为全角的两种解决方法
- 【HTML】一个计算器
- C#制作WinForm控件
- 修改EhLib4.5,表格线颜色
- 启用PAE机制下虚拟地址到物理地址的转换
- Code Complete, Chapter 8:Defensive Programming
- 今天开通了博客
- REST的架构设计
- NET开发电信短信网关(1)!
- DataGridView
- 两点内容警戒自己
- http://space.itpub.net/95429/viewspace-433133
- 学生选课数据管理系统V0.1.0
- 在Ehlib中显示序号