Can I leave contracts in code that I'm merging with a codebase used by non-code contracts developers?

前端 未结 3 816
小蘑菇
小蘑菇 2021-01-11 11:30

For the last few months I\'ve been developing a side project for my company, but the higher-ups have now decided it would be a good fit in an existing product.

I\'ve

3条回答
  •  伪装坚强ぢ
    2021-01-11 11:43

    The short answer is: Yes. If you check in the code with Code Contracts, then all developers that might build that code must also have Code Contracts installed in order to build the code.

    In contradiction to what @CBauer wrote in his answer, there is a "blessed" package for Code Contracts. No, it's not a NuGet package—it's an MSI installer based installation.

    Finally, if you are in a Continuous Integration environment for debug builds (e.g. Development, QA/QC and/or Testing), then those build servers will also need to have Code Contracts installed.

    When you use Code Contracts, Debug builds always require the use of Code Contracts. Please note that this is not necessarily the case for Release builds. It depends on what form of contract checking you're using and the options specified in the Project Properties.

    The Code Contracts manual has all the details. It's quite good and I highly recommend taking the time to read and understand it.

    It should be noted that if you use the Contract.Requires(bool condition) form of preconditions, you must have Code Contracts enabled for Release builds (see Section 5: Usage Guidelines, specifically, page 20, Usage 2 scenario).

    Since you are integrating this code into an existing codebase that has not been developed with Code Contracts, you should consider revising your Code Contracts Project Property settings to conform to Usage Scenario 3 outlined on page 20 of the Code Contracts manual, and reformulate your contracts using the "legacy" if-then-throw pattern. This will enable your team to best tranisition the code base to using Code Contracts everywhere, allowing you to eventually replace "legacy" if-then-throw precondition checks with actual Code Contracts Contract.Requires(bool condition) checks, and if you like, Contract.Requires(bool condition) checks.

    UPDATE: There Will Soon Be A NuGet Package For Code Contracts I was at the new GitHub repository for Code Contracts today. For those of you who don't know, Microsoft has open-sourced this and it is now a community-driven effort.

    They recently (back in January) announced v1.10.xxxx.RC1 release. You can find information about it here on their GitHub repository.

提交回复
热议问题