Home > Code Providers > Code Providers – Add Contract

Code Providers – Add Contract

February 7th, 2011

Add Contract code provider adds conditions (contracts or guard code) for a validation of the active method parameters. You can choose the preferred contract via the sub-menu of the Add Contract:

Refactor! Add Contract with sub-menu

Contracts represent some kind of obligations for the method parameters and its return value. Consider this Main function written in CSharp:

public static int Main(string[] args)
{
  Console.WriteLine("Amount of args passed: " + args.Length);
  return args.Length;
}

or in Visual Basic:

Public Shared Function Main(ByVal args As String()) As Integer
  Console.WriteLine("Amount of args passed: " + args.Length)
  Return args.Length
End Function

The method accepts a string array and returns the integer as a result. These obligations can be treated as contracts:

  • you have to pass a single array of strings as an argument to this method: you can’t pass additional arguments, and you can’t pass an array of different type, e.g. an array of integer type.
  • you are not able to treat return result as a different type other than integer, for example, as a string type.

Now, additional questions can arise to this method that may represent other contracts:

  • Can the method take ‘null’ (CSharp) or ‘Nothing’ (Visual Basic) as an argument?
  • Can the method take an empty array that don’t contain elements at all?
  • How many items in the array the method expects?
  • Can item of an array argument be “nullness”?
  • Can item of an array argument be an empty string?
  • Can the return result of the method be ignored?

The Add Contract code provider is intended to help developers to add these additional contracts. Here’s the list of available contracts:

Contract name

Description

Assert Contract Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer and displays the specified message.
Assume Contract Instructs code analysis tools to assume that a condition is true, even if it cannot be statically proven to always be true, and displays a message if the assumption fails.
Ensures Contract Specifies a post condition contract for a provided exit condition and a message to display if the condition is false.
Invariant Contract Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.
Requires Contract Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.
Exit Method Generates return statements to validate active method parameters in runtime.
Throw Exceptions Generates throw exception statements to validate active method parameters in runtime.
Use Assertion Generates debug assertions to validate active method parameters in runtime.

The first five contracts (Assert, Assume, Ensures, Invariant, Requires) are available in Visual Studio 2010 only (the project must target the .NET 4.0 (and higher) version as well). These contracts provide a language-agnostic way to express coding assumptions in programs. They take the form of pre-conditions, post-conditions, and object invariants, and are used to improve testing via runtime checking.

These contracts generate the following code for the array parameter correspondingly:

CSharp:

Contract.Assert(args != null && args.Length != 0, "args is null or empty.");
Contract.Assume(args != null && args.Length != 0, "args is null or empty.");
Contract.Ensures(args != null && args.Length != 0, "args is null or empty.");
Contract.Invariant(args != null && args.Length != 0, "args is null or empty.");
Contract.Requires(args != null && args.Length != 0, "args is null or empty.");

Visual Basic:

Contract.Assert(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")
Contract.Assume(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")
Contract.Ensures(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")
Contract.Invariant(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")
Contract.Requires(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")

The three other contracts (Exit Method, Throw Exceptions, Use Assertion) represent defensive statements called “guard code”. They act like a shield, keeping us from making obvious mistakes later in the method. For example, by checking that the array isn’t null and that it’s at least one element in length, we believe that the code in the remainder of the method will run smoothly or at least without exceptions. These tests will allow us to check that the array passed to the method is structured and populated just as we require.

These contracts generate the following code for the array parameter:

Exit Method:

CSharp:

int result = 0;
if (args == null || args.Length == 0)
return result;

Visual Basic:

Dim result As Integer = 0
If args Is Nothing OrElse args.Length = 0 Then
  Return result
End If

Throw Exceptions:

CSharp:

if (args == null || args.Length == 0)
throw new ArgumentException("args is null or empty.", "args");

Visual Basic:

If args Is Nothing OrElse args.Length = 0 Then
  Throw New ArgumentException("args is nothing or empty.", "args")
End If

Use Assertion:

CSharp:

Debug.Assert(args != null && args.Length != 0, "args is null or empty.");

Visual Basic:

Debug.Assert(args IsNot Nothing AndAlso args.Length <> 0, "args is nothing or empty.")

You are able to extend the list of available contracts by adding your own using the ContractProvider DXCore component via creating a new IDE Tools plug-in.

—–
Products: Refactor! Pro, CodeRush Xpress, Refactor! for C++, Refactor! for ASP.NET
Versions: 10.2 and up
VS IDEs: any
Updated: Apr/06/2011
ID: R020

Similar Posts:

  1. No comments yet. Be the first and leave a comment!