20

I'm reading up on Code Contracts, which at first glance seem to be pretty revolutionary, but I can't seem to get them working. I'm running Windows 8 and Visual Studio 2012 Premium (Release versions of both). I then installed Code Contracts from here by clicking on the Download Code Contracts link.

I then wrote the following code in a brand new console app:

class Program
{
   static void Main(string[] args)
   {
      var answer = Add(0, 5);
      Console.Write(answer);

      Console.ReadLine();
   }

   static int Add(int x, int y)
   {
      Contract.Requires(x > 0 && y > 0);

      return x + y;
   }
}

I expect the compilation to fail, since the first parameter of Add is 0, but the program succeeds and prints 5 out to the console.

I've tried with the default Code Contract settings, and also mucked with stuff a bit to no avail. My current settings look like this:

enter image description here

Any ideas what I'm doing wrong?

UPDATE:

Here's the results from the Build window. It appears it's doing something, but just raising warnings instead of errors. In the video I watched, these things got flagged as compile errors and the program wouldn't even run.

1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------
1>  DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe
1>  CodeContracts: Task manager is unavailable.
1>  CodeContracts: DeleteMe: Run static contract analysis.
1>  CodeContracts: Suggested requires: Contract.Requires(false);
1>  CodeContracts: DeleteMe: Validated:  0.0 %
1>  CodeContracts: DeleteMe: Contract density: 0.87
1>  CodeContracts: DeleteMe: Total methods analyzed 4
1>  CodeContracts: DeleteMe: Methods with 0 warnings 3
1>  CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method
1>  CodeContracts: DeleteMe: Methods with necessary preconditions: 1
1>  CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843
1>  CodeContracts: DeleteMe: Retained 1 preconditions after filtering
1>  CodeContracts: DeleteMe: Inferred 0 object invariants
1>  CodeContracts: DeleteMe: Retained 0 object invariants after filtering
1>  CodeContracts: DeleteMe: Detected 0 code fixes
1>  CodeContracts: DeleteMe: Proof obligations with a code fix: 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning
1>  CodeContracts: Checked 1 assertion: 1 false
1>  CodeContracts: DeleteMe: 
1>  CodeContracts: DeleteMe: Static contract analysis done.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Did you try moving that warning level slider to "hi"? It could be it requires a higher warning level to cause compile errors. – TyCobb Nov 17 '12 at 21:21
  • @TyCobb - Yup, I tried moving it all the way over to the right. No effect.. – Mike Christensen Nov 17 '12 at 21:22
  • @TyCobb - Also, you'd think even if they were just warnings, I'd still see squiggly underlines in the IDE. I'm stating to think Code Contracts isn't compatible with VS2012, or maybe the update hasn't been released yet. – Mike Christensen Nov 17 '12 at 21:27
  • Yea, could be a compatibility issue with it. I watched the video on the link you provided and he even makes it sound that it would cause compile errors, but then realizes that it can't and doesn't in the demo so I no longer would expect compile errors. However, you should at least get some sort of warning and the squiggle since you have it checked. I am with you and think there just needs to be update released. – TyCobb Nov 17 '12 at 21:40
  • Also, here's this: http://connect.microsoft.com/VisualStudio/feedback/details/646880/code-contracts-dont-listen-to-treat-warnings-as-errors-setting Sounds like it would be too much work for them. – TyCobb Nov 17 '12 at 21:43
  • Yea I was watching [this video](http://www.infoq.com/presentations/Contracts-Library), at about 10:30min in. I think I might have misunderstood it, since he never actually *says* the build fails. However, he gets nice little squigglies under his errors, which I don't get. Maybe that's not supported in VS2012 yet. – Mike Christensen Nov 17 '12 at 21:58

2 Answers2

13

So, the issue seemed to be a combination of several limitations and gotchas with Code Contracts. Hopefully this answer will help people like me just starting out.

First off, Code Contracts does support Visual Studio 2012 (any version other than Express) since build 1.4.50327.0, though you have to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more information.

The first issue I was having was that the "Cache Results" check box was checked in the "Static Checking" section of the Code Contracts properties tab. This option is on by default, and also requires SQL Server CE to store its cached data, which is not installed by Windows 8, VS2012 or Code Contracts. Unfortunately, your program will continue to compile just fine, and you'd have to manually go dig through the Build output to see the error:

CodeContracts: xxx: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

Unchecking the "Cache Results" checkbox will fix this issue, as would installing SQL Server CE.

The second issue is that Code Contract violations are treated as warnings, and not compile errors. Even if you have "Treat Warnings as Errors" enabled, your program will continue to compile and run successfully. If you have a larger project with tons of Warnings you ignore, it could potentially be difficult to notice these new Code Contract warnings. In the demo video I saw, these warnings were also reflected in the Visual Studio IDE (the calling code had a blue underline), however I don't seem to get this behavior in Visual Studio 2012.

This design decision disturbs me. If I define a contract within my code, that a function must take an integer greater than 0, and I blatantly pass in a 0, this is an error. Not a warning. I broke that contract, plain and simple.

Overall, I'd say Code Contracts is extremely powerful, and could potentially change the way we test software. MS Research definitely did great work. However, I don't think it's really ready for mainstream yet. It takes some tweaking to get working, it doesn't seamlessly integrate into the Visual Studio build process, and it's also pretty slow. On smaller projects, it worked as expected, but when I plugged it in to a larger project, it took a good ten minutes to analyze all the code.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Do you have ReSharper installed? I tried the example in your first post and couldn't get the squiggles to appear. After suspending ReSharper, closing VS, deleting the bin and obj folders from the project, and then reopening VS, I could get the static checking to work as expected and show a squiggle under the call to `Add(0, 5)'`. I then enabled ReSharper and static checking still works as expected. – WarrenG Nov 17 '12 at 22:50
  • @WarrenG - I *do* have Resharper installed! However, I suspended it, closed VS, deleted the bin/obj folders, then re-opened VS, loaded the project, built the solution, but still no squigglies. Oh well.. – Mike Christensen Nov 18 '12 at 01:17
  • I just tried this with a fresh install of VS2012 with the latest update. Only nuget, library extensions for windows library for js, and nuget installed. I still get the task manager unavail error. So there is something more subtle here?? – Gordon Dec 01 '12 at 14:46
  • 1
    @Gordon - Yea, the technology seems flaky to me and takes a lot of tweaking to get working. I actually just stopped using it, I'm gonna wait until it gets a bit more mainstream. – Mike Christensen Dec 01 '12 at 18:31
  • @Mike, yes, I was coming to a similar conclusion. I was thinking of, for now, turning on the features in my teamcity building of the projects, but during my debugging and testing keeping it off... (my teamcity is still running vs2010). They do seem to be actively working on it, so perhaps in a few updates. – Gordon Dec 02 '12 at 19:53
2

Sir, you will have to enable runtime checking. Under your in the window you posted as an image.

"Full" will mean that all conditions will be checked. The rest are self explanatory.

  • Static checking only runs in the background and checks for any thing it can improve within your code. Caution - it slows your builds down somewhat.