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 Nativ...
Agenda• Back to Basics• Design by Contract• Code Contracts  – Preconditions  – Postconditions  – Invariants• Internals• Ex...
Back to Basics
The If…Throw Approachpublic class MyClass{       public void SomeMethod(string s, int i)       {              if (s == nul...
The If…Throw Approach• Issues  – Cumbersome – lots of code  – Indistinguishable from method code  – Does not apply to inte...
Design by Contract“Design by contract (DbC) (…) is an approach fordesigning software. It prescribes that softwaredesigners...
Enter Code Contracts• Microsoft’s Design-by-Contract  implementation for .NET  – A subset of Spec#• Language-agnostic way ...
Requirements• .NET 4.0/4.5• Visual Studio 2010/2012  – Any edition except “Express” edition• Download and install from Mic...
Preconditions• Define what your API expects  – Parameter validation  – Object state  – Environment condition  – Anything t...
Postconditions• Define what the caller can expect from your API  – Return value constraints  – Exception constraints  – Ob...
Invariants• Define the rules which govern your object’s behavior  – Object state  – Field & property values  – Things that...
Enough Talking. Show Me the Code!
Should I Use It? It Depends…• Pros  –   Reduce bugs  –   Static verification  –   API documentation  –   Improved testabil...
There is a Middle Way!public class MyClass{        public void SomeMethod(string s, int i)        {                if (s =...
How Can I Choose? From the User Manual
Code Contracts Internals• Contract Rewriter   – ccrewrite• Static Verifier   – cccheck• API Documentation   – ccdoc• Ships...
Code Contracts Internals
Code Contracts Editor Extension• Visual Studio 2010 Only  – 2012 support still pending  – http://tinyurl.com/CCEditorExten...
Summary• A contract is more than an Interface• Design by Contract is a design approach• Code Contracts enables Design by C...
Resources• My Blog  – http://stiller.co.il/blog• Code Contracts  – http://research.microsoft.com/en-us/projects/contracts/...
Presenter contact detailsc: +972-54-6160646e: erans@codevalue.netb: stiller.co.il/blogw: www.codevalue.net
Upcoming SlideShare
Loading in …5
×

Enhance Your Code Quality with Code Contracts

3,437 views

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,437
On SlideShare
0
From Embeds
0
Number of Embeds
2,519
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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

    1. 1. Code Contracts Enhance Your Code Quality with Code ContractsEran StillerSenior SoftwareArchitect, FounderMarch 20, 2013
    2. 2. 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
    3. 3. Agenda• Back to Basics• Design by Contract• Code Contracts – Preconditions – Postconditions – Invariants• Internals• External Tools
    4. 4. Back to Basics
    5. 5. 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 }}
    6. 6. 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
    7. 7. 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)
    8. 8. 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
    9. 9. 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
    10. 10. Preconditions• Define what your API expects – Parameter validation – Object state – Environment condition – Anything the caller can check!
    11. 11. Postconditions• Define what the caller can expect from your API – Return value constraints – Exception constraints – Object state after call
    12. 12. Invariants• Define the rules which govern your object’s behavior – Object state – Field & property values – Things that always hold true
    13. 13. Enough Talking. Show Me the Code!
    14. 14. 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
    15. 15. 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 }}
    16. 16. How Can I Choose? From the User Manual
    17. 17. Code Contracts Internals• Contract Rewriter – ccrewrite• Static Verifier – cccheck• API Documentation – ccdoc• Ships with contract assemblies for many BCL types & methods
    18. 18. Code Contracts Internals
    19. 19. Code Contracts Editor Extension• Visual Studio 2010 Only – 2012 support still pending – http://tinyurl.com/CCEditorExtensions• Has some stability issues
    20. 20. 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!
    21. 21. 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/
    22. 22. Presenter contact detailsc: +972-54-6160646e: erans@codevalue.netb: stiller.co.il/blogw: www.codevalue.net

    ×