Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mini-training: Code Contract


Published on

Code Contract is a pretty easy way to reinforce the overall quality of your code. The following slides give you an overview of what Code Contracts are and how to make it work in a .Net environment. You’ll find a lot of links to let you go further, as well.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Mini-training: Code Contract

  1. 1. Code Contracts Mini-Training 01/2014
  2. 2. Agenda What is this? Are they different from Unit-Tests? How that works? When should we use them? Going further… 2
  3. 3. What is this? • Design by Contract has been invented by B. Meyer in 1986 when he specified the Eiffel language • The principle is to specify what must be true at some point in the execution of a program • In many programming languages, contracts are implemented with assert • Assertions represent a contract specifying the responsibilities between the customer and the provider in a piece of code • Design by contract does not replace regular testing strategies… we’ll see that later. 3
  4. 4. What is this? • Code Contracts is a coding practice • A contract is an assertion in the code • Contracts take the form of preconditions, postconditions, and object invariants (a condition which is always true) • Contracts are expressed using static method calls at method entries • A contract is a form of documentation • Runtime checking • Works well with static analyzer tool like Resharper ;-) But remember… • A contract is not a behavior! 4
  5. 5. Are they different from Unit-Tests? Not really  As Patrick Smacchia said: “Code Contracts and Automated Testing are pretty much the same thing” – They check often for code behavior correctness – They trigger obvious failure in case of broken correctness found – They document code behavior Code Contracts assertions raise obvious failure when violated during the execution of some automated tests When using massively contracts and unit-tests, maintenance cost is cut by an order of magnitude 5
  6. 6. Are they different from Unit-Tests? Contracts are not a replacement for Tests and vice-versa: both should be practiced seriously: • Contracts need Automated Tests to be exercised often in a repeatable way. • Automated Tests need Contracts as a much finer way to check code behavior correctness, inside the code itself. Chances that a bug remains undetected are pretty close to 0 when the code is both 100% covered by tests and contains an optimal set of contract assertions. Read full Patrick Smacchia article. 6
  7. 7. How that works? In .Net, there’s 2 way to do it: • Microsoft Code Contracts Framework • System.Diagnostics Debug.Assert() 7
  8. 8. When should we use them? Contracts in C# are more than 60% about avoiding NullReferenceException but there are other situations when it’s cool to do so: – – – – – str.Length > 2 str[0] == ‘.’ i > 10 validate an invariant state inside a loop … But as Patrick Smacchia says: remember that null reference check represent like half of the bugs on earth! (aka the Billion $ mistake) 8
  9. 9. When should we use them? Code Contract vs. Debug.Assert()? Code Contract is a really nice framework that allows you a lot of things (contract on interfaces members, Require(), Ensure(), Invariant(), ValueAtReturn(), …) But it doubles compilation time! Code Contracts assemblies get rewritten once compiled by the C#. (but it might change in C# 6!) 9
  10. 10. When should we use them? In practice use contracts on the public surface API of your application and Debug.Assert() everywhere else! Stephen Cleary suggests that: Set this on your Release project properties (code contracts tab) This will output an “<applicationName>.Contracts.dll That way: Projects consuming your library should reference your Release build. In their Debug configuration, they should check "Perform Runtime Contract Checking" and "Call-site Requires Checking"; this will ensure that the code contracts for your library's public API are enforced at runtime (the "Call-site" option uses the ".Contracts.dll" assembly that you built). In their Release configuration, they should leave code contracts disabled, which allows all assemblies to run at full speed. 10
  11. 11. Give a try to… 11
  12. 12. Give a try to… • Microsoft Code Digger – analyzes possible execution paths through your .NET code. The result is a table where each row shows a unique behavior of your code. The table helps you understand the behavior of the code, and it may also uncover hidden bugs. • Code Contracts Editor Extensions – provides contract support programmers as they edit code for 12
  13. 13. Questions? 13
  14. 14. Some links… Design by contract on Wikipedia Code Contracts @Microsoft Code Contracts User Manual (pdf) Code Contracts is the next coding practice you should learn and use Better Than Unit Tests (to go further with other test techniques, a bit off-topic but worth a read) Patrick Smacchia (NDepend) @Twitter Michael Nygard @ThinkRelevance & @Twitter Code Contracts for .NET VisualStudio Gallery Pretty interesting question on SO and nice answer by Jon Skeet himself! Simple and Easy Code Contracts - Stephen Cleary’s guide to get started with some useful information about build settings 14
  15. 15. Find out more • On • @__MaxS__
  16. 16. About Betclic • • • Betclic Everest Group, one of the world leaders in online gaming, has a unique portfolio comprising various complementary international brands: Betclic, Everest Gaming,, Expekt… Active in 100 countries with more than 12 million customers worldwide, the Group is committed to promoting secure and responsible gaming and is a member of several international professional associations including the EGBA (European Gaming and Betting Association) and the ESSA (European Sports Security Association). Through our brands, Betclic Everest Group places expertise, technological know-how and security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion of our players.