• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mini-training: Code Contract
 

Mini-training: Code Contract

on

  • 921 views

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. ...

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.

Statistics

Views

Total Views
921
Views on SlideShare
599
Embed Views
322

Actions

Likes
0
Downloads
1
Comments
0

2 Embeds 322

https://techblog.betclicgroup.com 320
http://feedly.com 2

Accessibility

Categories

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.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Mini-training: Code Contract Mini-training: Code Contract Presentation Transcript

    • Code Contracts Mini-Training 01/2014
    • Agenda What is this? Are they different from Unit-Tests? How that works? When should we use them? Going further… 2
    • 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
    • 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
    • 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
    • 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
    • How that works? In .Net, there’s 2 way to do it: • Microsoft Code Contracts Framework • System.Diagnostics Debug.Assert() 7
    • 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
    • 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
    • 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
    • Give a try to… http://pexforfun.com/default.aspx?language=CSharp&sample=StringTrimSuffix 11
    • 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
    • Questions? 13
    • 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
    • Find out more • On https://techblog.betclicgroup.com/ • @__MaxS__
    • 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, bet-athome.com, 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.