Add Contract
- 4 minutes to read
Inserts code into a method to check that a field, property, variable or parameter satisfies the required conditions. The code provider supports the following contracts.
Use Assertions
Adds the Debug.Assert method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Debug.Assert(obj != null, "obj is null.");
Debug.Assert(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Assert Contract
Adds the Contract.Assert method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Contract.Assert(obj != null, "obj is null.");
Contract.Assert(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Assume Contract
Adds the Contract.Assume method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Contract.Assume(obj != null, "obj is null.");
Contract.Assume(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Ensures Contract
Adds the Contract.Ensures method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Contract.Ensures(obj != null, "obj is null.");
Contract.Ensures(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Invariant Contract
Adds the Contract.Invariant method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Contract.Invariant(obj != null, "obj is null.");
Contract.Invariant(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Requires Contract
Adds the Contract.Requires method call to the initial method.
private string MyTestMethod(object obj, string str)
{
Contract.Requires(obj != null, "obj is null.");
Contract.Requires(!String.IsNullOrEmpty(str), "str is null or empty.");
...
Exit Method
Generates code that exits the initial method if any of its arguments have not been initialized.
private string MyTestMethod(object obj, string str)
{
string result = String.Empty;
if (obj == null || String.IsNullOrEmpty(str))
return result;
...
Throw Exceptions
Generates code that throws exceptions if any of the initial method arguments have not been initialized.
private string MyTestMethod(object obj, string str)
{
if (obj == null)
throw new ArgumentNullException("obj", "obj is null.");
if (String.IsNullOrEmpty(str))
throw new ArgumentException("str is null or empty.", "str");
...
#Purpose
- When creating a method, you often need to check if the input parameters have been initialized. Create Method Contract automates this very common task.
- Having all the parameter values checked for initialization all in one place at the top of the method body results in clearer code. This also allows you to provide a centralized response to invalid parameter values if needed.
#Availability
Available from the context menu or using shortcuts:
- when the cursor is on an empty line above the rest of the method body;
- when the cursor is on a reference or declaration within a method body.
#Notes
- You can choose what you wish to do if the inserted conditions are met - exit the method, throw exceptions or use assertions.
- For a Sub in Visual Basic (void functions in C#), the Create Method Contract inserts a condition to check parameters for null or empty values, and returns if any of parameters aren’t initialized.
- For functions that have return values, the Create Method Contract creates a local variable initialized with an empty value of the appropriate type. The code for checking the parameters, returns this variable’s value if one of the parameters isn’t initialized. At the same time, the Create Method Contract inserts the code that returns this variable’s value to the end of the method, providing an additional benefit to organizing the program flow of the method.
- If the Create Method Contract declares a local variable. Rename is automatically invoked for this local, so you can provide an appropriate name for it.
#Example
private string MyMethod(MyClass param1, string param2)
{│ return param2 + param1.ToString();
}
Private Function MyMethod(ByVal param1 As TestClass, ByVal param2 As String) As String│
Return param2 + param1.ToString()
End Function
Result:
private string MyMethod(MyClass param1, string param2)
{│string result = String.Empty;
if (param1 == null || String.IsNullOrEmpty(param2))
return result;
return param2 + param1.ToString();
}
Private Function MyMethod(ByVal param1 As TestClass, ByVal param2 As String) As String│Dim lResult As String = String.Empty
If param1 Is Nothing OrElse String.IsNullOrEmpty(param2) Then
Return lResult
End If
Return param2 + param1.ToString()
End Function