Better contracts better code - august 2010

987 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
987
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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?
  • Better contracts better code - august 2010

    1. 1. Better Contracts. Better Code.<br />An Introduction to Microsoft Code Contracts in the .NET 4.0 Framework<br />August 2010<br />W. Kevin Hazzard, C# MVP<br />
    2. 2. What is code?<br />Intent<br />Assumption<br />Binding<br />Promise<br />Guarantee<br />Obligation<br />http://www.asu.edu/clas/shs/aald/<br />
    3. 3. A Simple Illustration<br />public int[] ApplyDiscounts(<br />intmaxPercentageAllowed,<br /> Customer[] customers,<br /> Promotion[][] promotions )<br />{<br />// apply the promotions here<br />// and return the results<br />}<br />
    4. 4. C# Does OK with Types & Structure<br />Customer<br />int<br />int<br />int<br />public int[] ApplyDiscounts(<br />intmaxPercentageAllowed,<br /> Customer[] customers,<br /> Promotion[][] promotions ) {}<br />Customer<br />int<br />Customer<br />Promotion<br />Promotion<br />Promotion<br />Promotion<br />Promotion<br />Promotion<br />
    5. 5. Questions Left Unanswered<br />What is the acceptable range for the integer maxPercentageAllowed parameter?<br />Can the customers or promotions arrays references be null or contain nulls?<br />The promotions array is jagged. If an associated customer has no promotions, should the second-dimension array be null or empty?<br />What range will the integers in the array result have?<br />
    6. 6. Microsoft Code Contracts<br />Static code verification<br />Runtime checking, too<br />Cross-language support<br />Enhances documentation<br />Works with .NET Fx 3.5; built into 4.0<br />Supports pre/post-conditions & invariants<br />Helper classes for iteration, assertion, etc.<br />
    7. 7. Samples Survey<br />Let's take a look at the Code Contract samples which are excellent for learning how this stuff works.<br />
    8. 8. Contract Reference Assemblies<br />Something like "buddy classes" for contracts<br />Contain visible interface and contracts but no code bodies<br />ccrefgen.exe tool can be used to generate<br />Some have been pre-built and ship with the Code Contracts installer<br />Silverlight 3.0 and 4.0<br />.NET Framework 3.5 and 4.0<br />
    9. 9. Patching Sandcastle<br />Open the SandCastle zip file in the Code Contracts installation directory<br />Copy the templates into your installed SandCastle folder<br />Now help builds from XML comment documentation will include contract information<br />A future release of SandCastle should remove the need for patching<br />
    10. 10. Useful URLs<br />MSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontracts<br />Microsoft Code Contractsresearch.microsoft.com/en-us/projects/contracts<br />Kevin's Blogdevjourney.com<br />
    11. 11. Demos<br />
    12. 12. Useful URLs<br />MSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontracts<br />Microsoft Code Contractsresearch.microsoft.com/en-us/projects/contracts<br />Kevin's Blogdevjourney.com<br />

    ×