我是否可以将代码中的契约与非代码契约开发人员使用的代码库合并?

时间:2022-12-15 23:31:01

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 been developing the side project using Microsoft's Code Contracts for static type checking (partly because I hadn't used them before and was eager to learn).

我一直在开发利用Microsoft的代码契约进行静态类型检查的side项目(部分原因是我以前没有使用过它们,并且渴望学习)。

My problem is that if I check in my code to the code base with Contracts in place, will every other developer need the Code Contracts tools installed to be able to continue developing? I know for a fact that none of them have it installed, and I'm the junior here so I doubt I could convince them all to take it up.

我的问题是,如果我将我的代码签入到代码库中,那么所有其他开发人员都需要安装的代码契约工具来继续开发吗?我知道他们中没有人安装了它,而我是这里的大三学生,所以我怀疑我是否能说服他们全部接受。

I'm using .Net 4.5, so the Code Contract libraries are included, but I'm wondering if Visual Studio will complain that they're not building with CONTRACTS_FULL specified in the build options every time they go to build, or, if I leave CONTRACTS_FULL in the build options, what will happen when another developer tries to build? Additionally I'm wondering how the end-product will act when a Contract fails, but the code has not been built with the Code Contracts Rewriter.

我使用。net 4.5,所以合同库包含的代码,但是我想知道Visual Studio会抱怨他们没有建筑CONTRACTS_FULL构建选项中指定每次他们去建立,或者,如果我离开CONTRACTS_FULL在构建选项,当另一个开发人员试图构建会发生什么事?另外,我想知道当合同失败时,最终产品将如何运作,但是代码并没有建立在代码契约改写器的基础上。

I created a new solution with just one project. Created a simple function that fired a code contract violation, with code contracts uninstalled and CONTRACTS_FULL not specified. Built and ran it and received the following error:

我用一个项目创建了一个新的解决方案。创建了一个简单的函数,它违反了代码契约,没有安装代码契约,也没有指定CONTRACTS_FULL。建立并运行它并收到以下错误:

Run-time exception (line 8): An assembly (probably "hdxticim") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires<TException> and the CONTRACTS_FULL symbol is defined.  Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild.  CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180. 
After the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane.  Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL

I think the error message needs rewriting, as CONTRACTS_FULL is most definitely not specified.

我认为错误消息需要重写,因为CONTRACTS_FULL绝对不是指定的。

Thanks to Matías Fidemraizer, we've worked out that this happens when using Contract.Requires<TException>() and not on Contract.Requires().

由于Matias Fidemraizer,我们已经知道了在使用contract时发生了这种情况。

Ideally, I'd like to modify this behaviour so that the Contract fires the provided exception as if it were a normal guard statement instead of complaining about the rewriter.

理想情况下,我希望修改这种行为,这样合同就可以触发提供的异常,就像它是一个正常的保护语句,而不是抱怨重写程序。

Here's a fiddle demonstrating the problem: https://dotnetfiddle.net/cxrAPe

这是一个展示问题的小提琴:https://dotnetfiddle.net/cxrAPe。

3 个解决方案

#1


4  

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.

与@CBauer在他的回答中所写的矛盾,有一个“有福”的代码契约包。不,它不是一个NuGet包——它是一个基于MSI安装程序的安装。

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.

最后,如果您处于调试构建的持续集成环境中(例如开发、QA/QC和/或测试),那么这些构建服务器也需要安装代码契约。

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<TException>(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).

应该注意的是,如果你使用合同。需要 (bool condition)形式的先决条件,您必须拥有用于发布版本的代码契约(参见第5部分:使用指南,特别是第20页,使用2场景)。

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<TException>(bool condition) checks.

因为你是将这段代码集成到现有的代码库,合同尚未开发代码,您应该考虑修改代码合同项目属性设置符合使用场景3列出了20页的代码合同手册,和你的合同使用“遗留”if-then-throw模式。这将使您的团队能够最好地利用代码库来使用代码契约,从而使您最终能够用实际的代码契约来替换“遗留”的if- - -throw precondition检查。要求(bool条件)检查,如果你喜欢,合同。需要< TException >(bool条件)检查。

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.

更新:很快就会有一个NuGet包,用于代码契约,我现在在新的GitHub代码库中进行代码契约。对于那些不知道的人,微软已经开源了,现在它是一个社区驱动的努力。

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

他们最近(在一月)宣布了v1.10.xxxx。RC1版本。您可以在他们的GitHub存储库中找到有关它的信息。

#2


4  

Unfortunately it's difficult to keep a library that is not installed via nuget up to date without forcing your coworkers to maintain their personal environment. Code contracts does not appear to have an official Microsoft-blessed package out there for use.

不幸的是,要保留一个没有通过nuget来安装的库是很困难的,它不会强迫你的同事维护他们的个人环境。代码契约似乎没有一个官方的微软祝福包可供使用。

fourpastmidnight's post below has a recently updated answer to my original concern above, but the bottom half I think remains relevant even now. Get your buy-in, people!

下面的四篇文章最近更新了我对我最初的关注,但我认为下面的一半仍然是相关的。得到你的支持,人们!

In my (admittedly subjective) experience, tools like this come to be reviled if buy-in is not established among coworkers in advance. You might try broaching the subject slowly to see if they would be receptive to using it.

在我(当然是主观的)经验中,如果事先没有同事的支持,这样的工具就会被唾骂。你可以试着慢慢地把这个问题提出来,看看他们是否能接受。

#3


0  

Contract.Requires<TException>() will fail if the code contracts rewriter is not installed, regardless of setting CONTRACTS_FULL or not.

需要 (),如果没有安装代码契约,则将失败,无论是否设置CONTRACTS_FULL。

One solution is to build a separate class FailableContract which tests the preprocessor condition CONTRACTS_FULL, which is set by Code Contracts.

一个解决方案是构建一个单独的类FailableContract,它测试预处理器条件CONTRACTS_FULL,它是由代码契约设置的。

Then all you have to do is remember to remove CONTRACTS_FULL from your build parameters before committing your code to the code base. You still get code contracts, everybody else gets guard statements.

然后,您只需记住在将代码提交到代码库之前从构建参数中删除CONTRACTS_FULL。你仍然得到代码契约,其他人都得到保护声明。

A small portion of FailableContract (which I'm still writing):

失败的一小部分(我还在写):

public class FailableContract
{

    public static void Requires(bool condition)
    {
        #if CONTRACTS_FULL
            Contract.Requires(condition);
        #else
            if (!condition) throw new ArgumentException(); //Just go with an argument exception because we haven't been supplied with an exception type.
        #endif
    }

    public static void Requires<TException>(bool condition) where TException : Exception, new()
    {
        #if CONTRACTS_FULL
            Contract.Requires<TException>(condition);
        #else
            if (!condition) throw new TException();
        #endif
    }
}

Then a simple Find, Replace Contract. to FailableContract.should get most issues.

然后一个简单的发现,代替合同。FailableContract。应该得到大多数问题。

#1


4  

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.

与@CBauer在他的回答中所写的矛盾,有一个“有福”的代码契约包。不,它不是一个NuGet包——它是一个基于MSI安装程序的安装。

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.

最后,如果您处于调试构建的持续集成环境中(例如开发、QA/QC和/或测试),那么这些构建服务器也需要安装代码契约。

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<TException>(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).

应该注意的是,如果你使用合同。需要 (bool condition)形式的先决条件,您必须拥有用于发布版本的代码契约(参见第5部分:使用指南,特别是第20页,使用2场景)。

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<TException>(bool condition) checks.

因为你是将这段代码集成到现有的代码库,合同尚未开发代码,您应该考虑修改代码合同项目属性设置符合使用场景3列出了20页的代码合同手册,和你的合同使用“遗留”if-then-throw模式。这将使您的团队能够最好地利用代码库来使用代码契约,从而使您最终能够用实际的代码契约来替换“遗留”的if- - -throw precondition检查。要求(bool条件)检查,如果你喜欢,合同。需要< TException >(bool条件)检查。

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.

更新:很快就会有一个NuGet包,用于代码契约,我现在在新的GitHub代码库中进行代码契约。对于那些不知道的人,微软已经开源了,现在它是一个社区驱动的努力。

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

他们最近(在一月)宣布了v1.10.xxxx。RC1版本。您可以在他们的GitHub存储库中找到有关它的信息。

#2


4  

Unfortunately it's difficult to keep a library that is not installed via nuget up to date without forcing your coworkers to maintain their personal environment. Code contracts does not appear to have an official Microsoft-blessed package out there for use.

不幸的是,要保留一个没有通过nuget来安装的库是很困难的,它不会强迫你的同事维护他们的个人环境。代码契约似乎没有一个官方的微软祝福包可供使用。

fourpastmidnight's post below has a recently updated answer to my original concern above, but the bottom half I think remains relevant even now. Get your buy-in, people!

下面的四篇文章最近更新了我对我最初的关注,但我认为下面的一半仍然是相关的。得到你的支持,人们!

In my (admittedly subjective) experience, tools like this come to be reviled if buy-in is not established among coworkers in advance. You might try broaching the subject slowly to see if they would be receptive to using it.

在我(当然是主观的)经验中,如果事先没有同事的支持,这样的工具就会被唾骂。你可以试着慢慢地把这个问题提出来,看看他们是否能接受。

#3


0  

Contract.Requires<TException>() will fail if the code contracts rewriter is not installed, regardless of setting CONTRACTS_FULL or not.

需要 (),如果没有安装代码契约,则将失败,无论是否设置CONTRACTS_FULL。

One solution is to build a separate class FailableContract which tests the preprocessor condition CONTRACTS_FULL, which is set by Code Contracts.

一个解决方案是构建一个单独的类FailableContract,它测试预处理器条件CONTRACTS_FULL,它是由代码契约设置的。

Then all you have to do is remember to remove CONTRACTS_FULL from your build parameters before committing your code to the code base. You still get code contracts, everybody else gets guard statements.

然后,您只需记住在将代码提交到代码库之前从构建参数中删除CONTRACTS_FULL。你仍然得到代码契约,其他人都得到保护声明。

A small portion of FailableContract (which I'm still writing):

失败的一小部分(我还在写):

public class FailableContract
{

    public static void Requires(bool condition)
    {
        #if CONTRACTS_FULL
            Contract.Requires(condition);
        #else
            if (!condition) throw new ArgumentException(); //Just go with an argument exception because we haven't been supplied with an exception type.
        #endif
    }

    public static void Requires<TException>(bool condition) where TException : Exception, new()
    {
        #if CONTRACTS_FULL
            Contract.Requires<TException>(condition);
        #else
            if (!condition) throw new TException();
        #endif
    }
}

Then a simple Find, Replace Contract. to FailableContract.should get most issues.

然后一个简单的发现,代替合同。FailableContract。应该得到大多数问题。