Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1663454
  • 博文数量: 230
  • 博客积分: 10045
  • 博客等级: 上将
  • 技术积分: 3357
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-30 20:40
文章分类

全部博文(230)

文章存档

2011年(7)

2010年(35)

2009年(62)

2008年(126)

我的朋友

分类: 项目管理

2009-12-30 22:18:55

看了一些Design by Contract的资料,不太理解它和Defensive programming的区别。我自己的工作经验都是基于C programming,我编制的程序大部分都是基于defensive programming的方式,写一个函数,要做好多参数检查。写一个逻辑也要做很多异常的判定,是不是Design by Contract就可以省了这些麻烦事呢?

看了一些其他人的观点,我觉得除非语言本身支持,否则可能还是隐形的defensive programming。

摘记一下一些观点。


“Even if Design by Contract is a trademark (Eiffel Software, 2007) the idea behind it is the more general one of “defensive programming”. As software developers, we often concentrate our efforts in the main code of the application, which is the interesting part, and that provides the realization of the use cases identified during the early phases of the project.
......
The idea behind defensive programming is that you should not trust what comes from outside your code, even if it is your own code that calls other pieces of your own code; you could just as well call it “limited applied paranoia”. This is important not only for stability purposes (you do not want to call methods on a null pointer) but also for security; take for example the well known “SQL injection attacks” that happen when you trust too much the inputs of your users… a little verification helps to avoid disasters.”

Mark
"I fully agree. Even if unit testing is done with 100% coverage the key is that unit testing treats each object as a black box. A lot of defensive coding is implementation specific and potentially runtime specific.

Unit tests are exercised in a controlled environment and do not necessarily cover every possible situation that will be encountered at runtime. Defensive coding often catches a problem and pinpoints it. This eventually saves a lot of debugging time trying to trace where and why a particular attribute of an object is null or not within an expected range etc...

Many times I have been very thankful to Jeff and Paul for introducing me to defensive programming. It is easy to make assumptions about a runtime environment and simply ignore possible exceptions to the rules. By placing defensive code at key locations in methods most if not all of 'unanticipated' scenerios are caught prior to release. Things which are missed and fail after release are extremely easy to locate, understand, and fix.

Last point, placing defensive code within a method is far easier than writing a unit test for the method to catch many situations. It requires less effort and is far less likely to get out of synch since it is directly inside the method and directly related to the implementation."


"This is why I prefer . I tend to think of defensive programming as an internal design by contract. If I write my preconditions and postconditions into the method itself, and document that breaking the preconditions will result in exceptions, then I can write my code expecting failure. If there's a subtle problem that could arise in the code, then I'll write the test for it inside the method and document it so it doesn't break my flow. Then, after I'm pretty certain that I have all the state I need and there's no possible way the method could fail, I'll write the success case. It shouldn't be possible to ever fail a unit test in the first place. (And if it does, I have the code heavily logged so I can track down where it broke down and what the state was.)"


"Defensive programming is all about the supplier making sure that their code works when parameters passed in might not be what it expected. And also that the client passes in correct information. So in effect the code is validated on both sides. The immediate advantage of this technique is that is will severely reduce the number of bugs since the chances of incorrect information both passed in and validated are slim. The disadvantage of this is longer CPU time and execution. The computer simply needs to parse and run more lines of code. Not so much a problem in todays world where hardware is getting faster and cheaper, but years ago it might have caused people to stop and think. Defensive Programming is generally used when you do not know exactly how your function call will be executed, ensuring nothing bad will happen.
....................
Design By Contract is different in that the supplier and client know what each is doing, or at least doing what they say they are doing and trusting them to perform that role. The class may say that it validates all country codes to ensure 2 character ISO 3166-1 standards and that the client does not need to, for example, so the client will be able to pass in any value and know that the validation it outside of its scope. Again the pros and cons are immediately noticeable in that it saves processing time, but at the expense that there might possible be bugs and possible security issues."

甚至Embedded.com上把Design by Contract作为defensive programming strategy之一。

阅读(918) | 评论(1) | 转发(0) |
0

上一篇:收音机

下一篇:solution for Excel Open Very Slow

给主人留下些什么吧!~~

chinaunix网友2010-01-07 22:04:28

I think Design by Contract (DC) simply means that it separates the interface from the implementation. Either Ada or Eiffel is a good lanaguage example DC. In contrast, C is not DC. Defensive programming is simply a way of how to programme. To my knowlege, it should have nothing to do with the computer language itself. Hang.