This document introduces Microsoft Code Contracts in the .NET 4.0 framework. It discusses what code contracts are, how they can specify intent, assumptions, promises and guarantees in code. It provides a simple example of applying discounts and questions it leaves unanswered. It then introduces Code Contracts features like static verification, runtime checking, cross-language support and enhancing documentation. It surveys the Code Contract samples and discusses contract reference assemblies, patching Sandcastle and provides useful URLs and demos.
1 of 11
Downloaded 10 times
More Related Content
Better contracts better code - august 2010
1. Better Contracts. Better Code.An Introduction to Microsoft Code Contracts in the .NET 4.0 FrameworkAugust 2010W. Kevin Hazzard, C# MVP
3. A Simple Illustrationpublic int[] ApplyDiscounts(intmaxPercentageAllowed, Customer[] customers, Promotion[][] promotions ){// apply the promotions here// and return the results}
4. C# Does OK with Types & StructureCustomerintintintpublic int[] ApplyDiscounts(intmaxPercentageAllowed, Customer[] customers, Promotion[][] promotions ) {}CustomerintCustomerPromotionPromotionPromotionPromotionPromotionPromotion
5. Questions Left UnansweredWhat is the acceptable range for the integer maxPercentageAllowed parameter?Can the customers or promotions arrays references be null or contain nulls?The promotions array is jagged. If an associated customer has no promotions, should the second-dimension array be null or empty?What range will the integers in the array result have?
6. Microsoft Code ContractsStatic code verificationRuntime checking, tooCross-language supportEnhances documentationWorks with .NET Fx 3.5; built into 4.0Supports pre/post-conditions & invariantsHelper classes for iteration, assertion, etc.
7. Samples SurveyLet's take a look at the Code Contract samples which are excellent for learning how this stuff works.
8. Contract Reference AssembliesSomething like "buddy classes" for contractsContain visible interface and contracts but no code bodiesccrefgen.exe tool can be used to generateSome have been pre-built and ship with the Code Contracts installerSilverlight 3.0 and 4.0.NET Framework 3.5 and 4.0
9. Patching SandcastleOpen the SandCastle zip file in the Code Contracts installation directoryCopy the templates into your installed SandCastle folderNow help builds from XML comment documentation will include contract informationA future release of SandCastle should remove the need for patching
10. Useful URLsMSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontractsMicrosoft Code Contractsresearch.microsoft.com/en-us/projects/contractsKevin's Blogdevjourney.com
12. Useful URLsMSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontractsMicrosoft Code Contractsresearch.microsoft.com/en-us/projects/contractsKevin's Blogdevjourney.com
Editor's Notes
What is the acceptable range for the integer maxPercentageAllowed parameter?Can the customers or promotions arrays references be null or contain nulls?The promotions array is jagged. If an associated customer has no promotions, should the second-dimension array be null or empty?What range will the integers in the array result have?