Better contracts better code - august 2010
-
Upload
kevin-hazzard -
Category
Documents
-
view
783 -
download
0
Transcript of Better contracts better code - august 2010
![Page 1: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/1.jpg)
Better Contracts. Better Code.
An Introduction to Microsoft Code Contracts in the .NET 4.0 Framework
August 2010W. Kevin Hazzard, C# MVP
![Page 2: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/2.jpg)
What is code?
PromiseGuaranteeObligation
IntentAssumptionBinding
http://www.asu.edu/clas/shs/aald/
![Page 3: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/3.jpg)
A Simple Illustration
public int[] ApplyDiscounts(int maxPercentageAllowed,Customer[] customers,Promotion[][] promotions )
{// apply the promotions here// and return the results
}
![Page 4: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/4.jpg)
C# Does OK with Types & Structure
public int[] ApplyDiscounts(
int maxPercentageAllowed,
Customer[] customers,
Promotion[][] promotions ) {}
Customer
Customer
Customerint
PromotionPromotionPromotion Promotion
Promotion
int int int
Promotion
![Page 5: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/5.jpg)
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.
![Page 6: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/6.jpg)
Samples Survey
Sample DescriptionBinarySearch Basics of using the static checkerGCD Studying implicit arithmetic obligationsInterfaceContracts Implementing contracts categoricallyChunker Using invariants for managing edge casesApiProtocols Establishing patterns for state machines
Let's take a look at the Code Contract samples which are excellent for learning how this stuff works.
![Page 7: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/7.jpg)
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
![Page 8: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/8.jpg)
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
![Page 9: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/9.jpg)
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
![Page 10: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/10.jpg)
Demos
![Page 11: Better contracts better code - august 2010](https://reader036.fdocuments.in/reader036/viewer/2022082606/558fc91d1a28ab64198b45c6/html5/thumbnails/11.jpg)
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