Throwing an exception vs Contract.Requires?

后端 未结 2 928
我在风中等你
我在风中等你 2020-12-14 09:28

I\'m wondering whether should I throw exceptions or call Contract.Requires

For example:

public static void Function(String str)
{
          


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 09:55

    The basic trade-off between if-then-throw and Requires as documented in the CodeContract user guide is how you build with your release bits.

    Case 1: You only use if-then-throw, no Requires. In this case you can build your release bits without running the contract tools on your dll/exe. The advantage is that you have faster builds and no risk that the tool introduces bugs. A second advantage is that team members can opt out of using the CodeContract tools. Disadvantages are that you get no contract inheritance of requires, and your contracts are not necessarily visible to the tools (unless you use EndContract). You specify this case by using assembly mode: Custom Parameter Validation

    Case2: You decide to run the CodeContract tools on your release bits always. This lets you use Requires and you get inheritance of contracts, including instrumentation of interfaces etc. Your contracts are clean and tool recognizable. The disadvantage is that everyone building your code must have the CodeContracts tools installed. You specify this case by using assembly mode: Standard in the Contract property pane.

    Hope this clear things up.

提交回复
热议问题