CodeContracts 应该取代常规的 ArgumentExceptions 吗?

发布于 2024-10-08 07:41:51 字数 166 浏览 4 评论 0原文

我可以开始使用 CodeContracts 而不是:

if (XXX == Y)
    throw new ArgumentException("bla bla");

如果我开发一个库并且我的库用户不使用 CodeContracts,它会如何工作?

Could I start using CodeContracts instead of:

if (XXX == Y)
    throw new ArgumentException("bla bla");

How does it work if I develop a library and my library users do not use CodeContracts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2024-10-15 07:41:51

假设使用代码契约的代码是通过二进制重写器运行的,它会抛出像您发布的代码一样的异常。重写者检查代码并用参数检查等替换合约代码。这有点像面向方面的编程。它会在您编译后注入代码来为您处理情况。

如果他们不使用代码合约,他们将无法获得执行静态分析的好处,静态分析旨在查看合约并警告他们可能会根据合约及其代码收到错误。

代码合同

Assuming that the code using code contracts is run through the binary rewriter, it will throw exceptions like code you posted. The re-writer goes through the code and replaces the contract code with argument checking etc. It's kinda like aspect oriented programming. It injects code to handle situations for you after you've compiled it.

If they don't use Code Contracts they will not get the benefit of having static analysis performed which is designed to look at the contract and warn them they might get an error based on the contract and their code.

Code Contracts

羁绊已千年 2024-10-15 07:41:51

要点:可以编写这样的语句

Contract.Requires<ArgumentNullException>(argumentToCheck, "argumentToCheck");

如果在构建配置中启用运行时检查器,重写器会将这样的先决条件重写为普通的 ArgumentNullException。

代码的调用者将显示 ArgumentNullException 或您提供的任何异常,无论他们是否安装了代码契约。

To the point: It's possible to write statements like this

Contract.Requires<ArgumentNullException>(argumentToCheck, "argumentToCheck");

If you enable the Runtime checker in your build configuration, the rewriter will rewrite preconditions like this to an ordinary ArgumentNullException.

Callers of your code will be shown an ArgumentNullException, or whatever exception you provide, regardless of whether or not they have Code Contracts installed.

最舍不得你 2024-10-15 07:41:51

我一直在使用代码契约并且为了临时使用,它们为我解决了两个代码问题,这些问题之前没有很好的解决方案:

  • 检查返回值检查
  • 不变量似乎是一个命名不佳的组合参数检查/入口点和返回值/出口点断言。

我可以为返回值声明一个临时变量,并在返回它之前断言一些内容,但这会带来额外的麻烦。

检查参数已经有解决方案:抛出ArgumentException。

代码契约在 ArgumentException 中添加了一个小东西——它让你很早就检查参数,在我看来这是一件好事。

代码契约还有更多内容,但我刚刚涉足其中,而且还没有可以进行超全面静态检查的版本。我计划更加彻底地使用代码契约,一旦我这样做了——使用相同的框架检查参数将更加优雅,而不是在代码契约和 if/then/throw ArgumentException 之间来回切换

I have been using code contracts and for casual use, they address 2 code problems for me that didn't have nice solutions earlier:

  • checking return values checking
  • invariants appear to be a poorly named combined parameter check/entry point and return value/exit point assertion.

I could declare a temp variable for the return value and assert some things before return it, but it's extra friction.

checking parameters already had a solution: throw ArgumentException.

Code contracts adds one tiny thing to ArgumentException-- it makes you check arguments very early, which in my opinion is a good thing.

There is much more going on with Code Contracts, but I've just dipped my toe into and I don't have the edition that does super-comprehensive static checks. I plan to work my way up to using Code Contracts more completely, and once I have done so-- it will be more elegant to check parameters using the same framework instead of switching back and forth between Code Contracts and if/then/throw ArgumentException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文