<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>CodeContracts</title><link>http://blogger.xs4all.nl/mzetstra/category/36096.aspx</link><description>Combines all posts that talk about Microsoft Code Contracts.</description><managingEditor>Meile Zetstra</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.Exists() (11 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/26/559733.aspx</link><pubDate>Mon, 26 Jul 2010 16:08:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/26/559733.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/559733.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/26/559733.aspx#Feedback</comments><slash:comments>34</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/559733.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/559733.aspx</trackback:ping><description>&lt;p&gt;Like the Contract.ForAll, the Contract.Exists method comes in two flavors, one that takes two integers as lower and upper bound and a Predicate&amp;lt;int&amp;gt;, and a generic one that takes an IEnumerable&amp;lt;T&amp;gt; and a Predicate&amp;lt;T&amp;gt; as input. And where the Contract.ForAll method checks that any entry that is passed to the given Predicate returns true, the Contract.Exists method will return true if at least one of the entries returns true on the Predicate.&lt;/p&gt;  &lt;p&gt;Let’s give a simple example. With the ForAll method&amp;#160; gave an example that would test a list for having the ten prime numbers greater than a given value x. It actually omitted an additional check which is the following:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll(0, primeList.Count - 1, p =&amp;gt; primeList[p] &amp;lt; primeList[p + 1]));&lt;/pre&gt;

&lt;p&gt;This checks that for all items in the list, the next value is actually greater than the preceding one. In order to do something similar with the Contract.Exists method, lets write a method that requires at least one of the given numbers is prime:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; FindAtLeastOnePrimeFollowingX(&lt;span style="color: blue"&gt;int&lt;/span&gt; x, &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; numberList)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(x &amp;gt; 0);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(numberList != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(numberList.Count &amp;gt; 5);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll(0, numberList.Count, p =&amp;gt; numberList[p] &amp;gt; x));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll(0, numberList.Count - 1, p =&amp;gt; numberList[p] &amp;lt; numberList[p + 1]));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Exists(0, numberList.Count, p =&amp;gt; numberList[p].IsPrime()));&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; numberList&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.IsPrime()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).ToList();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;This is just an example showing the working of the Code Contracts methods. I have no intention to bring this as a real world example. I probably will come up which such an example in a later stage. The last precondition in the example checks the requirement that there must be at least one prime number in the list passed as the second parameter. I was going to insert a post condition in here as well, which would ensure that in the result I would also have at least one prime number, but the ensures fails with the warning:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;CodeContracts: ensures unproven: Contract.Exists(Contract.Result&amp;lt;List&amp;lt;int&amp;gt;&amp;gt;(), p =&amp;gt; p.IsPrime())&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even though the predicate on the IsPrime is in the LINQ statement, the static checker is currently not able to project this also onto the post condition that I defined. Maybe this will get better in the near future.&lt;/p&gt;

&lt;p&gt;Next post in the series will be about the Contract.Assume method, which can be used to indicate to the static checker that even though static analysis on its own isn’t able to prove certain facts, the fact is still true and should be treated as proven.&lt;/p&gt;

&lt;p&gt;That’s it for now 
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/559733.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.EnsuresOnThrow&amp;lt;TException&amp;gt;() (10 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/22/558475.aspx</link><pubDate>Thu, 22 Jul 2010 10:17:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/22/558475.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/558475.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/22/558475.aspx#Feedback</comments><slash:comments>25</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/558475.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/558475.aspx</trackback:ping><description>&lt;p&gt;In previous post I dabbled about the post conditions using the Contract.Ensures method. But what if the method body throws an exception? Or any called method throws? How would you ensure certain state without the Code Contracts? Probably using some try-finally construction or just a try-catch. But as with other conditions, pre and post, the try-finally and try-catch will only have impact on the current enclosing method body, and not on any derived or overridden one.&lt;/p&gt;  &lt;p&gt;With the help of the Contract.EnsuresOnThrow&amp;lt;TException&amp;gt; this gets a little bit easier. It will ensure the post condition not only on the current enclosing method body, but also on any derived and overridden member. Let look at an example:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;bool&lt;/span&gt; AddAndProcessItem(T item)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(item != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Count == &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Count) + 1);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.EnsuresOnThrow&amp;lt;&lt;span style="color: #2b91af"&gt;DbException&lt;/span&gt;&amp;gt;(!item.IsProcessed);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// do processing here which may throw a DbException...&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;DbException&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; item.IsProcessed = &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;this&lt;/span&gt;.innerList.Add(item);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;Here the post condition ensures that on a DbExeption being thrown the item’s IsProcessed property will have the value false. The example shown here catches a possible thrown DbException, sets the state property and lets the exception propagate to a higher level. But you could also have code that throws an exception itself, for instance when a certain operation is invalid, you might throw an exception of type InvalidOperationException. If we would have the following just preceding the try-clause:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;.innerList.Contains(item))&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Item cannot be processed when it is already added.&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;we could add the following post condition:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.EnsuresOnThrow&amp;lt;&lt;span style="color: #2b91af"&gt;InvalidOperationException&lt;/span&gt;&amp;gt;(item.IsProcessed == &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue&amp;lt;T&amp;gt;(item).IsProcessed);&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This would ensure that also in case of an InvalidOperationException the IsProcessed value of the item is the same as it was before entering the method. So the method body should not have ‘touched’ this property in case of the InvalidOperationException being thrown.&lt;/p&gt;

&lt;p&gt;This concludes the parts about post conditions. In this I discussed the use of Contract.ForAll() already in &lt;a href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556479.aspx"&gt;this post&lt;/a&gt;, which is not restricted to use in post conditions, but may be used in preconditions as well. And even beyond those. There is also the Contract.Exists call, which will be the subject of the next post. After that I’ll try to post something useful on Contract.Assume and Contract.Assert. Also the object invariant is still left to discuss, with the Contract.Invariant call. And of course there are still a few attributes to dabble about.&lt;/p&gt;

&lt;p&gt;That’s it for now
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/558475.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.ValueAtReturn&amp;lt;T&amp;gt;() (9 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/21/558077.aspx</link><pubDate>Wed, 21 Jul 2010 14:00:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/21/558077.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/558077.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/21/558077.aspx#Feedback</comments><slash:comments>27</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/558077.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/558077.aspx</trackback:ping><description>&lt;P&gt;This post will deal with the Contract.ValueAtReturn&lt;T&gt;() method of the Microsoft Code Contracts. The method enables the contract to specify post conditions on out parameters. Where the Contract.Result is used to get a peek at the final return value, the Contract.ValueAtReturn does almost the same thing on out parameters.&lt;/P&gt;
&lt;P&gt;To make it a bit more clear, I created a simple example:&lt;/P&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; TestingOutParameter(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; x, &lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; y)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Contract&lt;/SPAN&gt;.Ensures(!&lt;SPAN style="COLOR: #2b91af"&gt;Contract&lt;/SPAN&gt;.Result&amp;lt;&lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt;&amp;gt;() ?&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Contract&lt;/SPAN&gt;.ValueAtReturn&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt; y) == -1 :&lt;BR&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;Contract&lt;/SPAN&gt;.ValueAtReturn(&lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt; y) == x + 1);&lt;BR&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (x == &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;.MaxValue)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = -1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = x + 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;/PRE&gt;
&lt;P&gt;This tests the incoming parameter x, when it is int.MaxValue, the method will return false and y will have the value of -1, otherwise it will return true and y will equal x + 1. Please note that in the example I have included the type int in the first call, but not in the second. It would not have been necessary to put it in either call, since the compiler would have inferred the type from the usage anyway. I included it to show that it could be used in both ways. However, if the type can not be inferred, it MUST be stated and can not be omitted.&lt;/P&gt;
&lt;P&gt;Just like the Contract.OldValue&lt;T&gt; the method can only be used in a Contract.Ensures call. And speaking of post conditions, there is just one call left to deal with in that part. And that is the Contract.EnsuresOnThrow&lt;TEXCEPTION&gt; call. And that will be the subject of the next post in this series.&lt;/P&gt;
&lt;P&gt;That&amp;#8217;s it for now &lt;BR&gt;Meile&lt;/P&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/558077.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.OldValue&amp;lt;T&amp;gt;() (8 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/20/557022.aspx</link><pubDate>Tue, 20 Jul 2010 10:47:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/20/557022.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/557022.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/20/557022.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/557022.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/557022.aspx</trackback:ping><description>&lt;p&gt;Continuing our series on Code Contracts. This time with the Contract.OldValue&amp;lt;T&amp;gt;() method. This method will return the value of the given variable at the start of the enclosing method or property. It is however only valid to use this method in a post conditional Contract.Ensures method call.&lt;/p&gt;  &lt;p&gt;And that immediately indicates it’s purpose. It should be used to enable the use of post conditions that depend on the state of the world at method entry, compared to that at the return. For instance, if you would have a class with some collection or list functionality, you could have an Add method. The Contract.OldValue&amp;lt;T&amp;gt;() method enables you to check the count at method exit compared to the count at method entry. The following example shows this:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; Add(T item)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(item != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: blue"&gt;this&lt;/span&gt;.innerList.Count == &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.innerList.Count) + 1);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;this&lt;/span&gt;.innerList.Add(item);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;The count at method exit should match the old count incremented with 1 for the added item. There may be other uses for the Contract.OldValue(). Just remember, it may only be used in Contract.Ensures calls, which limits the use to post conditional checking. Any other checks on pre-entry state should be handcrafted by the programmers themselves. Another way of checking object state is the use of the Contract.Invariant method calls in a private method marked with the ContractInvariantMethod attribute. A description of those will follow in some future post in this series. Next post will deal with the Contract.ValueAtReturn&amp;lt;T&amp;gt;() method call.&lt;/p&gt;

&lt;p&gt;That’s it for now
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/557022.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.ForAll() (7 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556479.aspx</link><pubDate>Mon, 19 Jul 2010 16:59:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556479.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/556479.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556479.aspx#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/556479.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/556479.aspx</trackback:ping><description>&lt;p&gt;This post in the series on Code.Contracts is all about the Contract.ForAll method. It comes in two flavors, one that iterates in a for-loop way, and a generic form that iterates over an IEnumerable of T in a more foreach kind of way.&lt;/p&gt;  &lt;p&gt;In the example I have used so far, I included at one point the check with Contract.ForAll&amp;lt;char&amp;gt; to ensure all the characters in the result must be equal to the first character of the input string. Unfortunately the static checker of Code Contracts is unable to ensure the correctness of this for all test. It probably has got to do something with the fact that the current System.String implementation does not include the contract checks on the constructor we’re using. Hence the checker presents you with a warning like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;CodeContracts: ensures unproven: Contract.ForAll(0, count, p =&amp;gt; Contract.Result&amp;lt;string&amp;gt;()[p].Equals(input[0]))&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I’ve tried a few things to work around this warning, but haven’t had any luck so far. So if anyone has a brilliant idea how to get rid of it, be my guest.&lt;/p&gt;  &lt;p&gt;So I created a different example to explain the Contract.ForAll method, which will be proven nicely by the static checker. Here goes:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;bool&lt;/span&gt; FirstTenPrimesFollowingX(&lt;span style="color: blue"&gt;int&lt;/span&gt; x, &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; primeList)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(x &amp;gt; 0);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(primeList != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(primeList.Count == 10);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll(0, primeList.Count, p =&amp;gt; primeList[p] &amp;gt; x));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(primeList, p =&amp;gt; p.IsPrime()));&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; primeList&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; !p.IsPrime()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).Count() == 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This example tests whether the list of primes in the second parameter will only contain the next ten primes greater than the value in the x input parameter. There is a number of preconditions present, all of them checking a bit of the contract. The first ensures the value of x is greater than 0, the second makes sure the list of primes is not null, the third ensures the count of the given list is 10. The fourth is a call to Contract.ForAll, this is the first flavor mentioned above. It iterates from 0 to the count of the primeList, and ensures each value is greater than x. The fifth and last check is using the second flavor of Contract.ForAll, which is the generic form. In this particular case, the type of int could have been inferred from the usage, but I’ve chosen to put it here explicitly to make it clear we’re calling the generic form of Contract.ForAll. It validates that each entry in the list is indeed prime.&lt;/p&gt;

&lt;p&gt;Let me note that there is no real world purpose to the example’s method body. It simply confirms that there is no non-prime value in the list, which is already ensured by the method’s contract. As may have become clear from the example, the Contract.ForAll is not restricted to Contract.Ensures calls. It’s not even restricted to any Contract calls. However, be aware of how and when to use this.&lt;/p&gt;

&lt;p&gt;The next post I will take the Contract.OldValue&amp;lt;T&amp;gt;() method as the subject. This is a method that is restricted to usage in post conditions. And there is still some ground to cover on the end of the post conditions, as there is also the Contract.ValueAtReturn&amp;lt;T&amp;gt;() method and the other post condition Contract.EnsuresOnThrow&amp;lt;TException&amp;gt; method as well.&lt;/p&gt;

&lt;p&gt;That’s it for now
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/556479.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.Result&amp;lt;T&amp;gt; (6 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556263.aspx</link><pubDate>Mon, 19 Jul 2010 09:55:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556263.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/556263.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/19/556263.aspx#Feedback</comments><slash:comments>24</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/556263.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/556263.aspx</trackback:ping><description>&lt;p&gt;The previous post was about post conditions, and more specific the Contract.Ensures method. In the example I used another method on the Contract class, Contract.Result&amp;lt;T&amp;gt; to get the eventual result of the method. The Contract.Result&amp;lt;T&amp;gt; has been backed in to give the programmer a convenient way to get the result of the method beforehand.&lt;/p&gt;  &lt;p&gt;The T defines the type of the return value, and logically it must match the return type of the enclosing method body. The return value of the Contract.Result&amp;lt;T&amp;gt;() method call is the actual return value of the enclosing method. The method can only be used in Contract.Ensures() method calls. So this is not some convenient way to get the return value beforehand at any place in your method body.&lt;/p&gt;  &lt;p&gt;To refresh the memories a bit, here’s the example again:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;&amp;gt;(input != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(input.Length &amp;gt;= 0, &lt;span style="color: #a31515"&gt;&amp;quot;The input should have at least one character.&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;&amp;gt;(count &amp;gt; 0);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires((0 + 1) &amp;lt;= input.Length);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;().Length == count);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;The Contract.Ensures call now verifies that the return value of the enclosing method has actually got a length that is equal to the count input parameter’s value. I’ve also added another precondition, since the static code checker of Code Contracts proposed another precondition, to ensure the result can be correctly computed. It’s the following line:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires((0 + 1) &amp;lt;= input.Length);&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This ensures the result can use the ToCharArray(0, 1) code without a possible exception being thrown at you at runtime. In my last post I also referred to the Contract.ForAll&amp;lt;T&amp;gt; method. This will actually be the subject of the next post in this series.&lt;/p&gt;

&lt;p&gt;That’s it for now 
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/556263.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.Ensures (5 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/15/555430.aspx</link><pubDate>Thu, 15 Jul 2010 15:44:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/15/555430.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/555430.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/15/555430.aspx#Feedback</comments><slash:comments>28</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/555430.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/555430.aspx</trackback:ping><description>&lt;p&gt;This is the first post that will be about post conditions. So far, I’ve tried to shred some light on defining preconditions with the help of the Code Contracts that are now part of the .NET framework since version 4. In the past, preconditions could and would be defined with the help of the IF-THEN-THROW construction. For a broader explanation on that, see my previous post in this series.&lt;/p&gt;  &lt;p&gt;Now I’ll move on to post conditions. I have not often included post conditions in my code. Of course you would design your code to never return a null value, of that would be a requirement. But how would you go about checking that? A possible construction would be the following:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; Process()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; processable = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;this&lt;/span&gt;.currentList&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Processable&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; p;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt; result = processable.Count();&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// process the bunch....&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (result != processable.Count())&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Count does not match number of processed items&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; result;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;The code above has some points to be cautious about. For instance, the value in the variable that is returned might have been changed on a different thread, before it is returned by the method body. How would you verify that? That would not be easy, especially not when your code is not as succinct as it could be and probably has dependencies you were not even aware of. Of course the code above contains a major flaw, since it uses a LINQ query to determine the count of items. But would every developer be aware of this?&lt;/p&gt;

&lt;p&gt;Luckily now there is the Contract.Ensures method to the rescue. As I’ve stated before, there is no ‘silver bullet’, and this one is no exception. It does not end all bugs in your code, but it goes a long way. The binary rewriter would make sure the code is checked at the end of the method. But that’s something you could implement yourself. However, the static checker goes the other mile, by checking what you’ve stated must be true. And in the above example, if it were written with the use of Contract.Ensures, the checker would warn you about the fact that the currentList’s entries may have their Processable property changed and therefore it can not verify the correctness of your post condition.&lt;/p&gt;

&lt;p&gt;Now back to our main example targeted also in our previous posts. Rewritten with the help of the Contract.Ensures method it will become like this:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;&amp;gt;(input != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(input.Length &amp;gt;= 0, &lt;span style="color: #a31515"&gt;&amp;quot;The input should have at least one character.&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;&amp;gt;(count &amp;gt; 0);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;().Length == count);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This verifies that the length of the returned string value is equal to the int count parameter. An additional check could be the following:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.ForAll&amp;lt;&lt;span style="color: blue"&gt;char&lt;/span&gt;&amp;gt;(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;().ToCharArray(), p =&amp;gt; p == input[0]));&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This verifies that result only contains the first character of the string input parameter. You may have been wondering what this construction Contract.Result&amp;lt;&amp;gt; is used for. Well, that will be the topic of the next post in this series. And the Contract.ForAll&amp;lt;&amp;gt; will follow in another post as well. Just stay tuned.&lt;/p&gt;

&lt;p&gt;That’s it for now. 
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/555430.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.EndContractBlock() (4 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/13/555310.aspx</link><pubDate>Tue, 13 Jul 2010 14:45:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/13/555310.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/555310.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/13/555310.aspx#Feedback</comments><slash:comments>38</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/555310.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/555310.aspx</trackback:ping><description>&lt;p&gt;In this post of the series on Code Contracts, it&amp;#8217;s all about the Contract.EndContractBlock method. This method could be seen as some magical marker to get the benefits of Code Contracts and all the checks that come with them. It is intended to be used in legacy code to mark the end of a contract block.&lt;/p&gt;  &lt;p&gt;Given the example I used in my previous posts:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;    {&lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input.Length &lt; 1)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"The input should have at least one character."&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (count &lt;= 0)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"count"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;    }&lt;/pre&gt;

&lt;p&gt;This could very easy be rewritten with a call to the Contract.EndContractBlock() method to get the benefit of the Code Contracts. It would end up like this:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;    {&lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input.Length &lt; 1)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"The input should have at least one character."&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (count &lt;= 0)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"count"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.EndContractBlock();&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;There are some things to note here though. Not every block of argument checking etc. could be easily marked with the EndContractBlock call. Any code that precedes the call to Contract.EndContractBlock() must adhere to the following form:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;if &lt;condition&gt; throw &lt;exception&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There can not be an else branch in the statements. Also the code in the condition must be side-effect free, and the exception that is to be thrown must be at least as visible as the method in which the contract occurs. As mentioned already in my post on &lt;A href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/09/554919.aspx"&gt;Contract.Requires&lt;/a&gt;, the conditions in the IF-THEN-THROW statements are the negation of the conditions that occur as part of the Contract.Requires method. This is something to be aware of when working with contracts.&lt;/p&gt;

&lt;p&gt;The Code Contracts binary rewriter will convert the code marked by the Contract.EndContractBlock into code similar to the code that uses Contract.Requires&lt;TException&gt; which was explained in my previous post &lt;A href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx"&gt;here&lt;/a&gt;. The Contract.EndContractBlock call is only needed as a marker when there are no other or further calls to the Contract&amp;#8217;s static members. If we were to enhance the code with an additional precondition, or with a new post condition, it would make the call to Contract.EndContractBlock() obsolete.&lt;/p&gt;

&lt;p&gt;The following example would also result in use of all benefits of Code Contracts, even though there is no explicit call to Contract.EndContractBlock():&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;    {&lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (input.Length &lt; 1)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"The input should have at least one character."&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"input"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;if&lt;/span&gt; (count &lt;= 0)&lt;br /&gt;      {&lt;br /&gt;        &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"count"&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&gt;().Length == count);&lt;br /&gt; &lt;br /&gt;      &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This example has a new post condition that has been added. The call to Contract.Ensures is a post conditional check, which is performed on the result of the method. Contract.Ensures is to be explained in the next post in this series.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s it for now. 
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/555310.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.Requires&amp;lt;TException&amp;gt; (3 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555228.aspx</link><pubDate>Mon, 12 Jul 2010 21:38:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555228.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/555228.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555228.aspx#Feedback</comments><slash:comments>11</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/555228.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/555228.aspx</trackback:ping><description>&lt;p&gt;The previous post on Code Contracts was about &lt;a href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx"&gt;Contract.Requires&lt;/a&gt;. That method comes with an overload, which is a generic one that takes an Exception type as the generic type parameter. The purpose of this method is mainly in line with that of the Contract.Requires method that I explained in the previous post &lt;a href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The original code sample contained a few ArgumentExceptions that were thrown if certain conditions were not met. The use of the Contract.Requires method made the code more succinct with less clutter, but the exceptions were nowhere to be found. In case the precondition is not met in the code that uses Contract.Requires, a ContractException will be thrown at runtime. Sometimes it is feasible though to throw a more specific exception, like the ArgumentExceptions that were thrown in the IF-THEN-THROW constructions.&lt;/p&gt;  &lt;p&gt;Just to step back to the code we had initially:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (input == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (input.Length &amp;lt; 1)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;The input should have at least one character.&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (count &amp;lt;= 0)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;count&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;This code is very specific about the exceptions. About the ArgumentOutOfRangeException one could argue that it is actually not a correct exception to throw here, since there is no specific range, but beyond that, the exceptions serve a purpose. It is up to the requirements of the final code and build whether or not the exception must be of the&amp;#160; ArgumentException family, or it should be a more ‘general’ ContractException, since it involves the predefined contract. When it is necessary to have the existing exceptions, one should use the Contract.Requires&amp;lt;TException&amp;gt; overload. The example code would end up looking like this:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;&amp;gt;(input != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(input.Length &amp;gt;= 0, &lt;span style="color: #a31515"&gt;&amp;quot;The input should have at least one character.&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;&amp;gt;(count &amp;gt; 0);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;Beyond the fact that the more specific exception is used on contract failure, all other points mentioned in the post on Contract.Requires, also apply to Contract.Requires&amp;lt;TException&amp;gt;. The overload has an additional caveat though. The exception type that is to be thrown must have a public constructor accepting a single string argument. If such a constructor can not be determined at runtime, an internal ContractException will be created and thrown instead.&lt;/p&gt;

&lt;p&gt;To conclude the posts on preconditions, the next will handle about Contract.EndContractBlock(). Even though it is not specific for preconditions, it will only truly serve a purpose when it appears at the end of legacy preconditions that are stated in the form of IF-THEN-THROW.&lt;/p&gt;

&lt;p&gt;That’s it for now. 
  &lt;br /&gt;Meile &lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/555228.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Meile Zetstra</dc:creator><title>Code Contracts: Contract.Requires (2 of n)</title><link>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx</link><pubDate>Mon, 12 Jul 2010 10:28:00 GMT</pubDate><guid>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx</guid><wfw:comment>http://blogger.xs4all.nl/mzetstra/comments/555185.aspx</wfw:comment><comments>http://blogger.xs4all.nl/mzetstra/archive/2010/07/12/555185.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogger.xs4all.nl/mzetstra/comments/commentRss/555185.aspx</wfw:commentRss><trackback:ping>http://blogger.xs4all.nl/mzetstra/services/trackbacks/555185.aspx</trackback:ping><description>&lt;p&gt;This second post in the series on Code Contracts will deal with the Contract.Requires method. In the &lt;a href="http://blogger.xs4all.nl/mzetstra/archive/2010/07/09/554919.aspx"&gt;introduction&lt;/a&gt;, I already mentioned that the Contract.Requires method is used to express a precondition. Whenever something has to be validated upfront, the Contract.Requires method is the one to use.&lt;/p&gt;  &lt;p&gt;In the old style programming, any precondition would be stated as an IF-THEN-THROW construction, like in the following example:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (input == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (input.Length &amp;lt; 1)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;The input should have at least one character.&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (count &amp;lt;= 0)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;count&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;Not only does this create a lot of clutter, it also expresses the conditions from the negative, exceptional point of view. Looking at the code, it is required for the input to be a non-null string with a length of 1 or more. But the conditional check is on a null string value, and a length smaller than 1. If we were to rewrite the code above with the use of Contract.Requires, we would end up with the following code:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; RepeatFirstCharacter(&lt;span style="color: blue"&gt;string&lt;/span&gt; input, &lt;span style="color: blue"&gt;int&lt;/span&gt; count)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(input != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(input.Length &amp;gt;= 0);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(count &amp;gt; 0);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(input.ToCharArray(0, 1)[0], count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;Now all the clutter has gone. The code is more succinct, and the conditions really reflect the contract in&amp;#160; a non-exceptional manner. Given the fact that in many software development cycles the specifications are mainly written in terms of requirements, it’s a good thing to have this also in the code. Since the Code Contract preconditions, post conditions, object invariants etc. can all be merged into the generated XML documentation, this also means the requirements will also appear in them as well.&lt;/p&gt;

&lt;p&gt;Moreover, the contracts as defined using the IF-THEN-THROW style will only affect the current call. If the RepeatFirstCharacter method had been defined as a virtual method, any derived class that implements an override will have to check for the contract itself. When using Code Contracts, this is no longer necessary. The binary rewriter used by Code Contracts will make sure the contract is also exposed in the derived method as well as in the base method.&lt;/p&gt;

&lt;p&gt;All this comes with a few caveats though. Besides the fact that Contract.Requires calls must be at the beginning of the method (which is quite logical given their nature), the contract must only reference members with at least the same visibility as the enclosing method. Since the contract is exposed to clients calling the method, this requirement makes a lot of sense. Another caveat is that on a derived method additional requirements can not be set. This is one that also makes a lot of sense, since the interface definition largely determines the contract. And on an override, the interface isn’t changed, so why should the contract?&lt;/p&gt;

&lt;p&gt;This method should be used for those preconditions that do not enforce a particular exception to be thrown, e.g. for backward compatibility reasons. In case a particular exception should be thrown one would have to use Contract.Requires&amp;lt;TException&amp;gt;(Boolean) method instead. I will dig into that method in the next post in this series.&lt;/p&gt;

&lt;p&gt;That’s it for now. 
  &lt;br /&gt;Meile&lt;/p&gt;&lt;img src ="http://blogger.xs4all.nl/mzetstra/aggbug/555185.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>
