際際滷

際際滷Share a Scribd company logo
Better Contracts. Better Code.An Introduction to Microsoft Code Contracts in the .NET 4.0 FrameworkAugust 2010W. Kevin Hazzard, C# MVP
What is code?IntentAssumptionBindingPromiseGuaranteeObligationhttp://www.asu.edu/clas/shs/aald/
A Simple Illustrationpublic int[] ApplyDiscounts(intmaxPercentageAllowed,	Customer[] customers,	Promotion[][] promotions ){// apply the promotions here// and return the results}
C# Does OK with Types & StructureCustomerintintintpublic int[] ApplyDiscounts(intmaxPercentageAllowed,	Customer[] customers,	Promotion[][] promotions ) {}CustomerintCustomerPromotionPromotionPromotionPromotionPromotionPromotion
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?
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.
Samples SurveyLet's take a look at the Code Contract samples which are excellent for learning how this stuff works.
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
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
Useful URLsMSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontractsMicrosoft Code Contractsresearch.microsoft.com/en-us/projects/contractsKevin's Blogdevjourney.com
Demos

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
  • 11. Demos
  • 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

  1. 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?