Exit Method Contract
- 2 minutes to read
Purpose
This Code Provider adds the guard precondition to method parameter(s) or a variable assignment. The precondition ensures the passed or assigned values are not empty and exits the method in case they are.
Availability
Available in the following cases.
- When the caret is at the beginning of a method body. In this case, the contract will test all method parameters and strings.
- When the caret is on a method’s parameter name. In this case, the contract will test only the selected parameter.
- When the caret is on a variable assignment.
Note
This Code Provider may be unavailable for variables of a non-nullable type, for instance, Integer.
Usage
Place the caret at the beginning of a method body.
Note
The blinking cursor shows the caret’s position at which the Code Provider is available.
- Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
- Select Add Contract | Exit Method Contract from the menu.
After execution, the Code Provider adds the guard conditional against all the method parameters.
public bool AddRecord(string name, object data) {
if (string.IsNullOrEmpty(name) || data == null)
return false;
//...
return true;
}
You can ask CodeRush to check arrays and collections length in code contracts. Refer to the Code Actions Settings topic for more information.
See Also