Better contracts   better code - august 2010
Upcoming SlideShare
Loading in...5
×
 

Better contracts better code - august 2010

on

  • 998 views

 

Statistics

Views

Total Views
998
Views on SlideShare
992
Embed Views
6

Actions

Likes
0
Downloads
8
Comments
0

2 Embeds 6

http://www.linkedin.com 4
http://speakerrate.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Better contracts better code - august 2010 Presentation Transcript

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