Using contract abbreviator methods

Abbreviator methods are a great addition to the features of code contracts. They allow us to create a single abbreviator method that contains often used or grouped code contracts. This means that we can simplify our code and make it more readable.

Getting ready

We will create two methods with the same code contract requirements. We will then simplify the methods under contract by implementing an abbreviator method to contain the code contracts.

How to do it…

  1. Before you go on, ensure that you have added the code contracts using statement to the top of your Recipes.cs class file:
    using System.Diagnostics.Contracts;
  2. Consider the following methods before you add them. We have two methods here, and each method requires that the parameter passed to it is not equal to zero and that the result is also not zero. The implementation within each method is different, but the code contracts applied are identical. To avoid a situation where code contracts are unnecessarily repeated, we can use abbreviator methods:
    public static int MethodOne(int value)
    {
        Contract.Requires(value > 0, "Parameter must be greater than zero");
        Contract.Ensures(Contract.Result<int>() > 0, "Method result must be greater than zero");
    
        return value - 1;
    }
    
    public static int MethodTwo(int value)
    {
        Contract.Requires(value > 0, "Parameter must be greater than zero");
        Contract.Ensures(Contract.Result<int>() > 0, "Method result must be greater than zero");
    
        return (value * 10) - 10;
    }
  3. Add a new method called StandardMethodContract() to your Recipes class. This method's name can be anything you like, but the signature needs to match the methods it abbreviates. Inside this method, add the required code contracts defined earlier in MethodOne() and MethodTwo():
    private static void StandardMethodContract(int value)
    {
        Contract.Requires(value > 0, "Parameter must be greater than zero");
        Contract.Ensures(Contract.Result<int>() >= 1, "Method result must be greater than zero");
    }
  4. Add the following attribute to the top of the StandardMethodContract() method to identify it as an abbreviator method:
    [ContractAbbreviator]
  5. Once you have done this, your abbreviator method should look like this:
    [ContractAbbreviator]
    private static void StandardMethodContract(int value)
    {
        Contract.Requires(value > 0, "Parameter must be greater than zero");
        Contract.Ensures(Contract.Result<int>() >= 1, "Method result must be greater than zero");
    }
  6. You can now go ahead and simplify MethodOne() and MethodTwo() by simply referencing the abbreviator method in place of the code contracts:
    public static int MethodOne(int value)
    {
        StandardMethodContract(value);
    
        return value - 1;
    }
    
    public static int MethodTwo(int value)
    {
        StandardMethodContract(value);
    
        return (value * 10) - 10;
    }
  7. In the console application, add the relevant using statement to the Program.cs class to bring the static class into scope:
    using static Chapter8.Recipes;
  8. First, call the two methods using the following parameters:
    try
    {
        MethodOne(0);
        MethodTwo(1);                
    }
    catch (Exception ex)
    {
        WriteLine(ex.Message);
    }
    ReadLine();
  9. If you run your console application, you will notice that the code contract throws an exception in the abbreviator contract, telling us that the supplied parameter can't be zero:
    How to do it…
  10. Then, modify your calling code and pass a valid value for MethodOne(), but leave the call to MethodTwo() as is. Run your console application again:
    try
    {
        MethodOne(200);
        MethodTwo(1);                
    }
    catch (Exception ex)
    {
        WriteLine(ex.Message);
    }
    ReadLine();
  11. This time, you will see that the code contract in the abbreviator method throws an exception on the return value that can't be zero:
    How to do it…

How it works…

Abbreviator methods allow us to create more readable code and to group often used code contracts in a common method decorated with the [ContractAbbreviator] attribute. Abbreviator methods are a powerful feature of code contracts that developers can utilize to produce better code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.204.0