Chapter 15. Letting your code speak more clearly with Code Contracts

 

This chapter covers

  • The objectives of Code Contracts
  • Writing contracts in your code
  • Binary rewriting
  • Static checking
  • Contract documentation
  • Applying contracts

This book is about C#, the language. I haven’t attempted to cover the whole of the .NET framework or even just the Base Class Library (BCL). Apart from when we were looking at different LINQ providers in chapter 12, I’ve been rigidly focused on explaining the features introduced in each version of C#. This chapter is somewhat different. It’s not about a new ability of the language itself, but a combination of tools and types (collectively called Code Contracts) that can radically change your code’s appearance and how much information it conveys. If this were a book about the English language, this would be a chapter on poetry. Even though poems use the same words as prose, they look and feel different. You need to read them in a particular way to understand what they’re trying to express, but they can convey a lot of meaning in a powerful way.

15.1. Life before Code Contracts

15.2. Introducing Code Contracts

15.3. Rewriting binaries with ccrewrite and ccrefgen

15.4. Static checking

15.5. Documenting contracts with ccdocgen

15.6. Practical contracts

15.7. Summary