且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

避免空引用异常

更新时间:2023-02-25 16:15:09

在一个空引用异常显示给用户,这表明在code从开发商的部分错误造成的缺陷。下面是关于如何以prevent这些错误的一些想法。

When a null reference exception is displayed to the user, this indicates a defect in the code resulting from an error on the part of the developer. Here are some ideas on how to prevent these errors.

我对谁关心软件的质量,​​还使用the.net编程平台上面的人的建议,是安装和使用Microsoft code合同(http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx )。它包括的功能做运行时检查和静态验证。建立这些合同到code的基本能力包括在4.0版本the.net框架。如果你有兴趣在code质量,这听起来像你,你可以真正享受使用Microsoft code合同。

My top recommendation for people who care about software quality, and are also using the.net programming platform, is to install and use Microsoft code contracts ( http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx ). It includes capabilities to do run-time checking and static verification. The essential capability to build these contracts into your code is being included in the 4.0 version of the.net framework. If you are interested in code quality, and it sounds like you are, you may really enjoy using Microsoft code contracts.

随着微软code合同,您可以通过添加preconditions这样的后卫来自空值你的方法Contract.Requires(客户!= NULL);。添加precondition这样相当于将很多人在上面他们的意见,建议的做法。此前code合同,我会建议你做这样的事情。

With Microsoft code contracts, you can guard your method from null values by adding preconditions like this "Contract.Requires(customer != null);". Adding a precondition like this is equivalent to the practice recommended by many others in their comments above. Prior to code contracts, I would have recommended you do something like this

if (customer == null) {throw new ArgumentNullException("customer");}

现在我建议

Contract.Requires(customer != null);

您可以启用运行时检查系统,这将尽可能早地捕捉这些缺陷,导致你走向有缺陷的code的诊断和纠正。但是,不要让我给你的IM pression是code合同仅仅是更换空参数异常的奇特的方式。他们是比这更强大。
随着微软code合同,你也可以运行静态检查,并要求它调查在code可能发生空引用异常可能的地点。静态检查需要更多的经验,轻松使用。我不会先推荐它适合初学者。但随意尝试一下,看看自己。

You can then enable the run-time checking system which will catch these defects as early as possible, leading you towards diagnosis and correction of the defective code. But don't let me give you the impression that code contracts are simply a fancy way to replace argument null exceptions. They are much more powerful than that. With Microsoft code contracts, you can also run the static checker, and ask it to investigate possible sites in your code where null reference exceptions might occur. The static checker requires a bit more experience to use easily. I would not recommend it first for beginners. But feel free to try it out and see for yourself.

有已经在此线程的空引用错误是否是一个显著的问题一些争论。啰嗦的回答如下。对于人谁不想通过韦德,我会总结。

There has been some debate in this thread on whether null reference errors are a significant problem. A long-winded answer is below. For people who don't want to wade through that, I will summarize.


  • 微软领先的研究人员在
    在规格#程序的正确性和
    code合同项目,认为这是
    值得来解决这一问题。

  • 博士。伯特兰·迈耶和团队的
    软件工程师ISE,谁
    开发和支持艾菲尔
    编程语言,也相信它
    值得一来解决这一问题。

  • 在我自己的商业开发经验的普通软件,我所看到的空引用错误往往不够,我想在我自己的产品和做法来解决这个问题。

多年来,微软已经投资于研究,旨在提高软件质量。他们的一个努力是规格#项目。一个在我的the.net 4.0框架的意见是最令人振奋的发展,更是推出微软code的合同,这是由规格#研究团队所做的早期工作的副产品的。

For years, Microsoft has invested in research designed to improve software quality. One of their efforts was the Spec# project. One of the most exciting developments in my opinion with the.net 4.0 framework, is the introduction of Microsoft code contracts, which is an outgrowth of the earlier work done by the Spec# research team.

至于你说的绝大多数code错误都空引用异常,我相信这是限定词绝大多数,这将导致一些分歧。短语绝大多数表明,故障可能是70-90%有一个空引用异常的根本原因。这似乎太高了我。我preFER从Microsoft规格#的研究引用。在他们的文章中规格#编程系统:概述,由麦克·巴尼特,K Rustan M. Leino和钨舒尔特。 2004年CASSIS,LNCS卷。 3362,施普林格,2004年,他们写了

Regarding your remark "the vast majority of errors in code are null reference exceptions", I believe it is the qualifier "the vast majority" that will cause some disagreements. The phrase "Vast majority" suggests that perhaps 70-90% of faults have a null reference exception as the root cause. This seems far too high to me. I prefer to quote from the research of the Microsoft Spec#. In their article The Spec# programming system: An overview, by Mike Barnett, K. Rustan M. Leino, and Wolfram Schulte. In CASSIS 2004, LNCS vol. 3362, Springer, 2004, they wrote

1.0非空类型的现代节目很多错误表现为
  空解除引用错误,提示
  一个编程的重要性
  语言提供的能力
  前pressions区分了
  可以评估为null,那些
  肯定不会(对一些实验
  证据,见[24,22])。事实上,我们
  想消除一切无效
  取消引用错误。

1.0 Non-Null Types Many errors in modern programs manifest themselves as null-dereference errors, suggesting the importance of a programming language providing the ability to discriminate between expressions that may evaluate to null and those that are sure not to (for some experimental evidence, see [24, 22]). In fact, we would like to eradicate all null dereference errors.

这是谁是熟悉这个研究人微软可能的来源。这篇文章可在规格#站点。

This is a likely source for people at Microsoft who are familiar with this research. This article is available at the Spec# site.

我已经复制引用22和24的下方,并且包括ISBN为您提供方便。

I've copied references 22 and 24 below, and included the ISBN for your convenience.


  • 曼努埃尔Fahndrich和K. Rustan M. Leino。声明和检查在非空类型
    面向对象的语言。在2003年ACM会议论文集面向对象
    编程,系统,语言和应用,OOPSLA 2003年,第38卷,编号
    11 SIGPLAN声明,302-312页。 ACM,2003年11月ISBN = {} 1-58113-712-5,

  • Manuel Fahndrich and K. Rustan M. Leino. Declaring and checking non-null types in an object-oriented language. In Proceedings of the 2003 ACM Conference on Object-Oriented Programming, Systems, Languages, and Applications, OOPSLA 2003, volume 38, number 11 in SIGPLAN Notices, pages 302–312. ACM, November 2003. isbn = {1-58113-712-5},

科马克那根,K. Rustan M. Leino,马克Lillibridge,格雷格·纳尔逊,詹姆斯·B·萨克斯,
和Raymie Stata的。扩展静态检查的Java。在2002年的ACM论文集
SIGPLAN会议程序设计语言设计与实现(PLDI),体积
37,在SIGPLAN声明,234-245页5号。 ACM,2002年5月。

Cormac Flanagan, K. Rustan M. Leino, Mark Lillibridge, Greg Nelson, James B. Saxe, and Raymie Stata. Extended static checking for Java. In Proceedings of the 2002 ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), volume 37, number 5 in SIGPLAN Notices, pages 234–245. ACM, May 2002.

我回顾这些引用。第一参考表明一些实验他们并检讨自己的code可能的空引用的缺陷。他们不仅发现几种,但在许多情况下,一个潜在的空引用的识别指示与设计一个更大的问题。

I reviewed these references. The first reference indicates some experiments they did reviewing their own code for possible null reference defects. Not only did they find several, but in many cases, the identification of a potential null reference indicated a broader problem with the design.

第二参考文献没有提供该空参考误差问题断言任何具体证据。但作者也指出,根据他们的经验,这些空引用错误是软件缺陷显著来源。本文然后继续解释他们是如何努力消除这些缺陷。

The second reference does not provide any specific evidence for the assertion that null reference errors are problem. But the authors do state that in their experience, these null reference errors are significant source of software defects. The paper then proceeds to explain how they try to eradicate these defects.

我还记得看到一些关于这个从ISE在最近的艾菲尔发布公告。他们提到这个问题称为无效的安全,并像伯特兰·迈耶博士的启发或制定这么多的事情,他们有问题的雄辩和教育的描述,他们如何去$ P $在他们的语言和工具pventing它。我建议你​​阅读他们的文章http://doc.eiffel.com/book/method/void-safety-background-definition-and-tools了解更多信息。

I also remembered seeing something about this in an announcement from ISE on a recent release of Eiffel. They refer to this issue as "void safety", and like so many things inspired or developed by Dr. Bertrand Meyer, they have an eloquent and educational description of the problem and how they go about preventing it in their language and tools. I recommend you read their article http://doc.eiffel.com/book/method/void-safety-background-definition-and-tools to learn more.

如果您想了解更多有关微软code的合同,也有最近出现了大量的文章。您还可以查看我的博客在http:SLASH SLASH codecontracts.info这主要是致力于关于通过使用编程与合同软件质量的交谈

If you want to learn more about Microsoft code contracts, there are tons of articles that have arisen recently. You can also check my blog at http: SLASH SLASH codecontracts.info which is primarily devoted to conversations about software quality through the use of programming with contracts.