CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, October 12, 2009

Beautiful post on Code Contracts

Introduction

Code contracts allow one to specify the behavior of an algorithm by explicitly adhering to a well defined contract. Commonly, these contracts are in the form of pre- and post-conditions. Further, one can also specify behavioral contracts for a type in the form of a type invariant. For now you need not worry about how such contracts can be defined (we will discuss that later), however, it is important to understand some of the benefits of using code contracts, these include:

  • provide high-level behavioral description of an algorithm;
  • force programmers to be explicit about the behavior of their algorithms;
  • help catch bugs early.

For many readers code contracts will not be a new concept, most likely you will have seen them before in texts on data structures and algorithms [2]. However, most readers will be new to applying similar concepts to their actual codebase.

In this article we will cover code contracts using Spec#, the code contracts library shipping with .NET 4.0, and finish with a lo ok at VC++ source code annotations. Continue

Post a Comment