【发布时间】:2011-02-02 03:04:03
【问题描述】:
我打算在未来的开发中使用新的 .NET 4 代码合同 功能。这让我想知道我们是否必须在方法链中冗余地指定等效的 Contract.Requires(...) 语句。
我认为一个代码示例值一千字:
public bool CrushGodzilla(string weapon, int velocity)
{
Contract.Requires(weapon != null);
// long code
return false;
}
public bool CrushGodzilla(string weapon)
{
Contract.Requires(weapon != null); // specify contract requirement here
// as well???
return this.CrushGodzilla(weapon, int.MaxValue);
}
对于运行时检查这并不重要,因为我们最终总会遇到需求检查,如果它失败了我们会得到一个错误。
但是,当我们在第二次重载中没有在此处再次指定合同要求时,是否被认为是不好的做法?
另外,还会有编译时检查的功能,可能还有代码合约的设计时检查。似乎它在 Visual Studio 2010 中还不能用于 C#,但我认为像 Spec# 这样的一些语言已经可以使用。当我们编写代码来调用这样的方法时,这些引擎可能会给我们提示,而我们的参数目前可以或将是null。
所以我想知道这些引擎是否会一直分析调用堆栈,直到找到当前不满足合约的方法?
此外,here I learned about the difference between Contract.Requires(...) and Contract.Assume(...)。我想这个区别也应该在这个问题的背景下考虑?
【问题讨论】:
标签: c# .net .net-4.0 code-contracts