1. Design by Contract for Mainstream .NET Code Contracts AshicMahtabhttp://www.heartysoft.com@ashic
2.
3. Static Languages Why do we code in C#? So type errors don’t manifest at runtime? Intellisense? Fear of learning Ruby?
4. Contract A promise that something will behave in a certain way. (Sounds like an interface, right?)
5. A joint account can be opened only if the number of holders is greater than two. public void OpenJointAccount(AccountHolder[] holders) An account holder’s balance may never fall below -100. Go ahead…try writing interfaces for them.
6. Design by Contract Based on the work of Alan Turing, Tony Hoare, EdsgerDijkstra, David Gries Popularized / advocated by Bertrand Meyer and originally implemented in Eiffel. Works on the basis of constraints like preconditions, post conditions and invariants which the supplier provides.
7. Preconditions: I require this to be true before I execute. Post condition: I ensure this to be true when I am done executing. Invariant: A condition that holds true during the lifetime of an object. It may not hold during the execution of a method but will do so before and after the method executes.
8. Demo Basics Preconditions, Post conditions, Invariants Subtle edge case violations Contract Blocks, [Pure] Contracts on Interfaces and Abstract Classes [ContractArgumentValidator], [ContractAbbreviator]
10. Drawbacks Slower build with static checking Public properties Interface contracts have weird syntax Currently no contracts on delegates Static checker can’t understand collections Algorithmic performance may be compromised IL mangling…no edit and continue Must have buy in – something whole team uses or nobody does