Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Contracts in C# and null checking

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?

like image 577
zachary Avatar asked Jan 14 '11 19:01

zachary


People also ask

What are code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

What is a contract in C++?

Contracts are part of the interface of an operation, but not part of its type. A contract expresses what the caller of a function must do to satisfy the expectations the callee places on its arguments. Consequently, the expression of a contract must logically be part of the declaration of the operation.

What is Design by Contract C#?

C# 4.0 Code Contracts. Microsoft has released a library for design by contract in version 4.0 of the . net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code.

What is assembly in .NET framework?

An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.


1 Answers

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.

like image 51
jedd.ahyoung Avatar answered Sep 19 '22 03:09

jedd.ahyoung