10

In my code i do this a lot:

myfunction (parameter p)
{
  if(p == null)
   return;
}

How would I replace this with a code contract?

I'm interested in finding out if a null has been passed in and have it caught by static checking.

I'm interested in having a contract exception be thrown if null is passed in during our testing

For production I want to exit out of the function.

Can code contracts do this at all? Is this a good use for code contracts?

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Xander
  • 9,069
  • 14
  • 70
  • 129
  • 3
    Why would you want to effectively turn off the safety checks when running with real data? Isn't it just as important (if not more so) to stop and abort whatever you're doing if your code has bugs in when running in production? – Jon Skeet Jan 14 '11 at 20:01
  • @Jon Skeet: there was a recent .Net Rocks pod cast in which they talked to one of the main designers of C# 4.0's code contracts, and they asked that same question. I wish I could explain it as well as he did, but I'm afraid I'd get something wrong. He made a pretty compelling case (when you have contract checking on during runtime anyways) as to why code contracts were better overall. – Neil N Jan 14 '11 at 20:05
  • @Neil: I have nothing against Code Contracts... but I'd leave them on in production as well. – Jon Skeet Jan 14 '11 at 20:07
  • @Jon Skeet: you mean you would leave safety checks on as well? It is my understanding that the contracts can serve both purposes of safety checks, and expressing the allowed values of the params, something you can't discern from the singature alone, since the method signature cannot tell you if a param cannot be null, or greater than zeo, etc. – Neil N Jan 14 '11 at 20:10
  • 2
    @Neil: I would leave the contracts active, so that they'd throw an exception if you passed in an invalid argument. – Jon Skeet Jan 14 '11 at 20:18
  • @Jon Skeet: so then in effect that is NOT turning off safety checks? Im confused as to what your concern was. – Neil N Jan 14 '11 at 20:36
  • 1
    @Neil: Yes, I *wouldn't* turn off the safety checks. That's why I was asking why the OP *would* turn them off (just returning instead of throwing an exception). – Jon Skeet Jan 14 '11 at 20:50
  • I deal with unexpected nulls in production by having the program silently exit out of the logic loop. The reason being that many times the program can continue to run fine even if an unexpected null was passed in. I agree that in theory the best thing to do is to throw a null exception (my fellow developers look at me like I am crazy when I say this). However in practice I don't want to be the programmer who introduced the critical bug because I threw an exception. Is there a better way to handle this? I am open for ideas... – Xander Jan 19 '11 at 16:21
  • In reply to Jon: it depends on whether your data is coming from external sources or not. If all your code is CC'd and the static checker passes 100%, you'll be okay to turn off at runtime. You can also leave only public-surface contracts enabled, so that external sources can't break your preconditions. This will also mean it's safe to turn off internal (non-public) runtime checking. – porges Feb 02 '11 at 22:04

3 Answers3

7

The syntax for this is:

Contract.Requires(p != null);

This needs to be at the top of the method. All Contract.Requires (and other contract statements) must precede all other statements in the method.

Timwi
  • 65,159
  • 33
  • 165
  • 230
3

Stumbled across this question while researching code contracts for our codebase. The answers here are incomplete. Posting this for future reference.

How would I replace this with a code contract?

The code is fairly simple. Replace your runtime null check with this line. This will throw an exception of type System.Diagnostics.Contracts.ContractException (which you apparently cannot catch explicitly, unless you catch all exceptions; it's not designed to be caught at runtime).

Contract.Requires(p != null);

I'm interested in finding out if a null has been passed in and have it caught by static checking.

You can use the Microsoft plugin for Code Contract static analysis, found here.

I'm interested in having a contract exception be thrown if null is passed in during our testing

Use the following, from above:

Contract.Requires(p != null);

Alternatively, if you want to throw a different exception (such as an ArgumentNullException) you can do the following:

Contract.Requires<ArgumentNullException>(p != null, "Hey developer, p is null! That's a bug!");

This, however, requires the Code Contracts Binary Rewriter, which is included with the Code Contract plugin.

For production I want to exit out of the function.

From what I understand, building your project in Release mode will disable the Code Contract checking (although you can override this in your project properties). So yes, you can do this.

Can code contracts do this at all? Is this a good use for code contracts?

Short answer - Yes.

1

These posts here and here have a lot of great options.

Edit: I misread your post the first time, thinking you were throwing an ArgumentNullException or something, until I saw Jon Skeet's comment. I would definitely suggest using at least one of the many approaches provided in the linked questions.

Reproducing one of the answers by John Feminella here:

[Pure]
public static double GetDistance(Point p1, Point p2)
{
    CodeContract.RequiresAlways(p1 != null);
    CodeContract.RequiresAlways(p2 != null); 
    // ...
}
Community
  • 1
  • 1
arcain
  • 14,920
  • 6
  • 55
  • 75