• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Enhance Your Code Quality with Code Contracts

Enhance Your Code Quality with Code Contracts



http://stiller.co.il/blog/2013/03/wf-4-5-and-code-contracts-slide-decks-and-code/ ...


If you ever used a 3rd party API, you surely know the importance of proper documentation - Can this method return "null"? Do I have to check this return value? What are the valid values of this argument? You also probably know that the only way to discover if your assumptions are correct is to execute the code and pray that exceptions won't be flying around. There must be a better way!

Enter "Code Contracts". Code Contracts is an experimental project from Microsoft Research which provides a language-agnostic way to express coding assumptions in .NET programs, thus allowing for improved testability, static verification at compile time and automatic API documentation.

In this session we'll take a tour around Code Contracts, understand when and how we can use them, and show how correct usage of these constructs can significantly increase the quality of your code.



Total Views
Views on SlideShare
Embed Views



11 Embeds 997

http://stiller.co.il 855
http://blogs.microsoft.co.il 114
http://outdoers8.veritise.com 9
http://feeds.feedburner.com 6
http://www.mybestcv.co.il 3
http://israel840.rssing.com 2
http://loose-barbed5.meelabo.com 2
http://www.365dailyjournal.com 2
http://newsblur.com 2
http://beta.blogs.microsoft.co.il 1
http://reader.aol.com 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Ask the crowd what do they think is the contract
  • Coined by Bertrand Meyer – the creator of Eiffel

Enhance Your Code Quality with Code Contracts Enhance Your Code Quality with Code Contracts Presentation Transcript

  • Code Contracts Enhance Your Code Quality with Code ContractsEran StillerSenior SoftwareArchitect, FounderMarch 20, 2013
  • About Me• Software architect, consultant and instructor• Technology addict• 10 years of experience• .NET and Native Windows Programming• Specializes in large-scale, server-side, highly-concurrent systems• Co-Founder at CodeValue Ltd.• http://stiller.co.il/blog
  • Agenda• Back to Basics• Design by Contract• Code Contracts – Preconditions – Postconditions – Invariants• Internals• External Tools
  • Back to Basics
  • The If…Throw Approachpublic class MyClass{ public void SomeMethod(string s, int i) { if (s == null) throw new ArgumentNullException( "s", "Your input string cannot be null!"); if (i < 0) throw new ArgumentException( "Your input must be non-negative!", "i"); // Your code here }}
  • The If…Throw Approach• Issues – Cumbersome – lots of code – Indistinguishable from method code – Does not apply to interfaces – Does not apply across object inheritance – Not visible at call site – Not part of the contract
  • Design by Contract“Design by contract (DbC) (…) is an approach fordesigning software. It prescribes that softwaredesigners should define formal, precise and verifiable interfacespecifications for software components, which extend theordinary definition of abstract data types with preconditions,postconditions and invariants. These specifications are referredto as "contracts", in accordance with a conceptual metaphorwith the conditions and obligations of business contracts.”(Wikipedia)
  • Enter Code Contracts• Microsoft’s Design-by-Contract implementation for .NET – A subset of Spec#• Language-agnostic way to express coding assumptions• Checked documentation of your API – Improved testability – Static verification – API documentation
  • Requirements• .NET 4.0/4.5• Visual Studio 2010/2012 – Any edition except “Express” edition• Download and install from Microsoft Research – http://research.microsoft.com/en-us/projects/contracts/• Allowed for commercial use• In addition – Support exists for .NET 3.5 and VS 2008 as well – Contracts API is part of an external assembly
  • Preconditions• Define what your API expects – Parameter validation – Object state – Environment condition – Anything the caller can check!
  • Postconditions• Define what the caller can expect from your API – Return value constraints – Exception constraints – Object state after call
  • Invariants• Define the rules which govern your object’s behavior – Object state – Field & property values – Things that always hold true
  • Enough Talking. Show Me the Code!
  • Should I Use It? It Depends…• Pros – Reduce bugs – Static verification – API documentation – Improved testability – Shallow learning curve• Cons – Experimental project – Static verifier less than perfect – Some issues with VS2012 – Compilation time increase
  • There is a Middle Way!public class MyClass{ public void SomeMethod(string s, int i) { if (s == null) throw new ArgumentNullException( "s", "Your input string cannot be null!"); if (i < 0) throw new ArgumentException( "Your input must be non-negative!", "i"); Contract.EndContractBlock(); // Your code here }}
  • How Can I Choose? From the User Manual
  • Code Contracts Internals• Contract Rewriter – ccrewrite• Static Verifier – cccheck• API Documentation – ccdoc• Ships with contract assemblies for many BCL types & methods
  • Code Contracts Internals
  • Code Contracts Editor Extension• Visual Studio 2010 Only – 2012 support still pending – http://tinyurl.com/CCEditorExtensions• Has some stability issues
  • Summary• A contract is more than an Interface• Design by Contract is a design approach• Code Contracts enables Design by Contract on .NET – Preconditions, Postconditions & Invariants• An experimental project from Microsoft Research• Rewrites your application’s IL code• Integrates with Editor Extensions & Pex• Give it a try!
  • Resources• My Blog – http://stiller.co.il/blog• Code Contracts – http://research.microsoft.com/en-us/projects/contracts/ – http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf – http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads• Design by Contract – http://en.wikipedia.org/wiki/Design_by_contract• Pex – http://research.microsoft.com/en-us/projects/pex/
  • Presenter contact detailsc: +972-54-6160646e: erans@codevalue.netb: stiller.co.il/blogw: www.codevalue.net