Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I implementing this simple contract incorrectly?

This is my code:

public class RegularPolygon
{
    public int VertexCount;
    public double SideLength;

    public RegularPolygon(int vertexCount, double sideLength)
    {
        Contract.Requires(vertexCount >= 3);
        VertexCount = vertexCount;
        SideLength = sideLength;
    }

    [ContractInvariantMethod]
    private void RegularPolygonInvariants()
    {
        Contract.Invariant(VertexCount>=3);
    }

}

I have tried with both the Contract.Requires and Contract.Invariant methods to prevent the vertexCount variable from becoming less than or equal to 2; however I am still able to initialise a RegularPolygon with 2 or fewer sides. My (simplified) NUnit test looks like this:

[TestFixture]
class TestRegularPolygon
{
    private RegularPolygon _polygon;

    [SetUp]
    public void Init()
    {
        _polygon = new RegularPolygon(1, 50);
    }

    [Test]
    public void Constructor()
    {
        Assert.That(_polygon.VertexCount,Is.GreaterThanOrEqualTo(3));
    }

}

The above test also passes and I cannot figure out why!

At first I thought ReSharper might have been messing something up because it greys out the line and displays this message whenever I try to use a method in the Contract namespace:

Method invocation is skipped. Compiler will not generate method invocation because the method is conditional, or it is partial method without implementation.

But suspending R# and running the tests in NUnit has the same result with no errors or warnings in VS either. So I assume that is just because ReSharper does not have highlighting compatibility for code contracts yet.

I have looked at the documentation and as far as I can tell I shouldn't be having this problem.

Am I using code contracts incorrectly or is my environment preventing it from working somehow?

Thank you.

like image 678
Nobody Avatar asked Aug 11 '10 19:08

Nobody


People also ask

What are the three 3 types of mistakes in a contract?

Common law has identified three different types of mistake in contract: the 'unilateral mistake', the 'mutual mistake' and the 'common mistake'. The distinction between the 'common mistake' and the 'mutual mistake' is important.

What makes an invalid contract?

If the subject matter is illegal, the contract will not be valid. All terms of your contract must not contravene any federal or state law. If the formation or performance of the contract will require a party to break the law, the contract is invalid.

What are the types of contract mistakes?

There are three types of mistakes in contract law: unilateral mistakes, mutual mistakes, and common mistakes.

What are three common mistakes that are often made when creating a contract?

There are three common mistakes in contract law namely unilateral, mutual, and common mistakes. Unilateral mistake befalls when one party to an agreement is misguided as to the terms contained in an agreement. Unilateral mistakes occur often than any other mistake.


1 Answers

First thing to check - have you actually got contract checking turned on? If not, none of your contracts will do anything. That would explain the R# warning, too. Look under "Code Contracts" in the build properties, and see what it says under "Runtime Checking".

As per comments, ensure you have CONTRACTS_FULL defined as a compilation symbol - that appears to be what R# requires.

Second point: you've got public mutable fields, which means your invariant can be violated at any moment by someone writing

polygon.VertexCount = 0;

Please don't use public fields, particularly not writable ones. :)

like image 200
Jon Skeet Avatar answered Oct 25 '22 22:10

Jon Skeet