mini-training: code contract

16
01/2014 Code Contracts Mini-Training

Upload: betclic-everest-group-tech-team

Post on 10-May-2015

911 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Mini-training: Code Contract

01/2014

Code ContractsMini-Training

Page 2: Mini-training: Code Contract

Agenda

What is this?Are they different from Unit-Tests?How that works?When should we use them?Going further…

2

Page 3: Mini-training: Code Contract

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

Page 4: Mini-training: Code Contract

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

Page 5: Mini-training: Code Contract

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 testsWhen using massively contracts and unit-tests, maintenance cost is cut by an order of magnitude

5

Page 6: Mini-training: Code Contract

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

Page 8: Mini-training: Code Contract

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

Page 9: Mini-training: Code Contract

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

Page 10: Mini-training: Code Contract

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

Page 11: Mini-training: Code Contract

Give a try to…

11http://pexforfun.com/default.aspx?language=CSharp&sample=StringTrimSuffix

Page 12: Mini-training: Code Contract

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.

12

• Code Contracts Editor Extensions– provides contract support for

programmers as they edit code

Page 13: Mini-training: Code Contract

Questions?

13

Page 14: Mini-training: Code Contract

Design by contract on WikipediaCode Contracts @MicrosoftCode 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) @TwitterMichael Nygard @ThinkRelevance & @TwitterCode Contracts for .NET VisualStudio GalleryPretty 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

Some links…

Page 16: Mini-training: Code Contract

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