code合同:我们必须在委托方法冗余指定Contract.Requires(...)语句? [英] Code Contracts: Do we have to specify Contract.Requires(...) statements redundantly in delegating methods?

查看:127
本文介绍了code合同:我们必须在委托方法冗余指定Contract.Requires(...)语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用新的.NET 4的 code合约的功能,为今后的发展。这使我想知道,如果我们要在一连串的方法冗余指定相当于 Contract.Requires(...)语句。

I'm intending to use the new .NET 4 Code Contracts feature for future development. This made me wonder if we have to specify equivalent Contract.Requires(...) statements redundantly in a chain of methods.

我觉得code例子是胜过千言万语:

I think a code example is worth a thousand words:

    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);
    }

有关的运行时检查的也没多大关系,因为我们最终会一直打到要求检查,如果失败,我们将得到一个错误。

For runtime checking it doesn't matter much, as we will eventually always hit the requirement check, and we will get an error if it fails.

然而,是它认为不好的做法时,我们并没有在这里指定的第二个重载的合同要求了吗?

However, is it considered bad practice when we don't specify the contract requirement here in the second overload again?

此外,会出现的编译时检查的,也可能的的设计时间检查code合约的功能。这似乎是没有可用于C#在Visual Studio 2010,但我觉得有一些语言,如规格#已经做的。这些发动机可能会给我们一些提示,当我们写code调用这种方法和我们的观点目前还或将

Also, there will be the feature of compile time checking, and possibly also design time checking of code contracts. It seems it's not yet available for C# in Visual Studio 2010, but I think there are some languages like Spec# that already do. These engines will probably give us hints when we write code to call such a method and our argument currently can or will be null.

所以我想如果这些发动机将一如既往分析调用堆栈,直到他们找到一个合约是目前的方法不满意?

So I wonder if these engines will always analyze a call stack until they find a method with a contract that is currently not satisfied?

此外,<一个href="http://stackoverflow.com/questions/1980689/c-$c$c-contracts-vs-normal-parameter-validation">here我了解 Contract.Requires之间的差值(...) Contract.Assume(...) 。我想,这种差异也是这个问题的背景下考虑呢?

Furthermore, here I learned about the difference between Contract.Requires(...) and Contract.Assume(...). I suppose that difference is also to consider in the context of this question then?

推荐答案

我认为这将是用来指定每一个公共方法所有的合同最佳实践。合同是不仅仅是什么被检查 - 这是文件也有效。如果你调用一个方法,但不知道什么合同被应用,这将是奇怪的,降下得到合同失效:那就表明你调用的方法的错误,而不是在的的方法。

I think it would be best practice to specify all the contracts on each of the public methods. A contract is more than just "what gets checked" - it's documentation too, effectively. If you call a method but don't know what contract is applied, it would be odd to get a contract failure lower down: that would suggest a bug in the method that you're calling, rather than in your method.

请注意,如果您使用的是C#4在整个项目中,你可以考虑使用可选参数和命名参数,以避免有这么多的过载。这不是有用的,如果你需要从它不支持他们,当然语言调用code。

Note that if you're using C# 4 throughout your project, you could consider using optional parameters and named arguments to avoid having so many overloads. That's not useful if you need to call the code from a language which doesn't support them, of course.

我强烈怀疑,如果你不指定合同的违约过载,静态检查(其中的的现在的适用于所有版本的Visual Studio 2010 的)会抱怨,该合同可能会失败,也将的提示的加入合约

I strongly suspect that if you don't specify the contract in the "defaulting" overload, the static checker (which is now available for all versions of VS2010) will complain that the contract might fail, and will also suggest adding the contract in.

这篇关于code合同:我们必须在委托方法冗余指定Contract.Requires(...)语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆