<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: C# by Contract - Using Expression Trees</title>
	<atom:link href="http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/feed/" rel="self" type="application/rss+xml" />
	<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/</link>
	<description>Closed weekends and holidays.</description>
	<pubDate>Sat, 17 May 2008 17:22:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
		<item>
		<title>By: Milan</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20511</link>
		<dc:creator>Milan</dc:creator>
		<pubDate>Mon, 07 Apr 2008 20:02:27 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20511</guid>
		<description>Design by contract is not simple as that. When you writing contracts you are writing policy for both client ( invoker ) and method it self. If any of this sides are trying to break policy ( for client precondition and for method postcondition ) the exception will be thrown which will tell us that illegal call of method is invoked, if precondition is not satisfied ,or method is not properly implemented if precondition is satisfied but postconditions are not. 
One more thing, contracts are not spoused to be implemented in production code. That is why contract methods should be putted in comments of method or as attributes, not in method body/implementation. Look onto JML, which is made for Java, or bogor... If all went OK, dynamic and static analyzes are passed in whole application, then contracts are satisfied and application is working properly so contracts are not needed any more! so there is no need for abnormal amount of ArgumentNullExceptions, for instance, just to ensure that method is not called with null argument.

I must say this is good article, but be aware that some things can and will some times put you on wrong way.

I'm currently working on VS2008 Tool which will do static and dynamic analyze of code. But I'm just at start because its working with IL code and there is lots of work to do. In min time I'm googleing if someone has done it already for .NET :) so I can be focused on my projects full time :)

One more thing, DBC is not test driven design, it is mathematical prove that your application is working correctly.

Regards,
Milan</description>
		<content:encoded><![CDATA[<p>Design by contract is not simple as that. When you writing contracts you are writing policy for both client ( invoker ) and method it self. If any of this sides are trying to break policy ( for client precondition and for method postcondition ) the exception will be thrown which will tell us that illegal call of method is invoked, if precondition is not satisfied ,or method is not properly implemented if precondition is satisfied but postconditions are not.<br />
One more thing, contracts are not spoused to be implemented in production code. That is why contract methods should be putted in comments of method or as attributes, not in method body/implementation. Look onto JML, which is made for Java, or bogor&#8230; If all went OK, dynamic and static analyzes are passed in whole application, then contracts are satisfied and application is working properly so contracts are not needed any more! so there is no need for abnormal amount of ArgumentNullExceptions, for instance, just to ensure that method is not called with null argument.</p>
<p>I must say this is good article, but be aware that some things can and will some times put you on wrong way.</p>
<p>I&#8217;m currently working on VS2008 Tool which will do static and dynamic analyze of code. But I&#8217;m just at start because its working with IL code and there is lots of work to do. In min time I&#8217;m googleing if someone has done it already for .NET :) so I can be focused on my projects full time :)</p>
<p>One more thing, DBC is not test driven design, it is mathematical prove that your application is working correctly.</p>
<p>Regards,<br />
Milan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andre</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20429</link>
		<dc:creator>Andre</dc:creator>
		<pubDate>Thu, 06 Mar 2008 14:55:51 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20429</guid>
		<description>Great idea!

I've just extended this to check that arguments are not null. 

Here is the method

----------------------------------
public static void RequireArgumentNotNull( this object obj, Expression&#60;Func&#62; expr )
{
   var x = expr.Compile();
   var o = x();

   if( o == null )
   {
      string paramName = "";

      if( expr.Body is MemberExpression )
      {
         paramName = ((MemberExpression)expr.Body).Member.Name;
      }

      throw new ContractException( expr.ToString(), new ArgumentNullException( paramName ) );
   }
}
----------------------------------


And an example of its usage


----------------------------------
public void TestArg( string someArg )
{
   this.RequireArgumentNotNull( someArg );

   ...
}
----------------------------------







The great thing about this is that the param name is now not a string as it used to be with a simple "throw new ArgumentNullException(...)", so refactorings work etc. Naturally you need to rely on the user using the method correctly but since in this case I'm my only user its not a problem :)


Andre</description>
		<content:encoded><![CDATA[<p>Great idea!</p>
<p>I&#8217;ve just extended this to check that arguments are not null. </p>
<p>Here is the method</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
public static void RequireArgumentNotNull( this object obj, Expression&lt;Func&gt; expr )<br />
{<br />
   var x = expr.Compile();<br />
   var o = x();</p>
<p>   if( o == null )<br />
   {<br />
      string paramName = &#8220;&#8221;;</p>
<p>      if( expr.Body is MemberExpression )<br />
      {<br />
         paramName = ((MemberExpression)expr.Body).Member.Name;<br />
      }</p>
<p>      throw new ContractException( expr.ToString(), new ArgumentNullException( paramName ) );<br />
   }<br />
}<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>And an example of its usage</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
public void TestArg( string someArg )<br />
{<br />
   this.RequireArgumentNotNull( someArg );</p>
<p>   &#8230;<br />
}<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>The great thing about this is that the param name is now not a string as it used to be with a simple &#8220;throw new ArgumentNullException(&#8230;)&#8221;, so refactorings work etc. Naturally you need to rely on the user using the method correctly but since in this case I&#8217;m my only user its not a problem :)</p>
<p>Andre</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Matthews</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20344</link>
		<dc:creator>Andrew Matthews</dc:creator>
		<pubDate>Tue, 22 Jan 2008 01:03:59 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20344</guid>
		<description>Hi Eric,

Yes, that would be nicer. I guess that with closures I don't need to worry too much about whether the objects I want to make comparisons on are in scope within the lambda when it gets invoked inside the predicate checking method. I guess that parameter stemmed from earlier incarnations, but it's really not needed any more.

Cheers

Andrew</description>
		<content:encoded><![CDATA[<p>Hi Eric,</p>
<p>Yes, that would be nicer. I guess that with closures I don&#8217;t need to worry too much about whether the objects I want to make comparisons on are in scope within the lambda when it gets invoked inside the predicate checking method. I guess that parameter stemmed from earlier incarnations, but it&#8217;s really not needed any more.</p>
<p>Cheers</p>
<p>Andrew</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: W&#246;chentliche Rundablage: .NET 3.5, WPF, LINQ, Tests, System.AddIn, SubSonic, Sandcastle &#124; Code-Inside Blog</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20342</link>
		<dc:creator>W&#246;chentliche Rundablage: .NET 3.5, WPF, LINQ, Tests, System.AddIn, SubSonic, Sandcastle &#124; Code-Inside Blog</dc:creator>
		<pubDate>Mon, 21 Jan 2008 20:41:57 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20342</guid>
		<description>[...] C# by Contract - eine interessante Variante wie man bestimmte Verträge in C# einhalten muss. Gemacht wurde dies mit Expression Trees - ein Blick lohnt. [...]</description>
		<content:encoded><![CDATA[<p>[...] C# by Contract - eine interessante Variante wie man bestimmte Verträge in C# einhalten muss. Gemacht wurde dies mit Expression Trees - ein Blick lohnt. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Lee</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20341</link>
		<dc:creator>Eric Lee</dc:creator>
		<pubDate>Mon, 21 Jan 2008 18:57:05 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20341</guid>
		<description>I like the use of extension methods for this but I don't like the extraneous "x =&#62;" when the parameter is not actually being used for anything in the lambda.  I don't know how much of an improvement it would be, but you could change your predicate to take a Func (no input parameters) and use the no-parameter syntax:

this.Require(() =&#62; !string.IsNullOrEmpty(location));

At least that way the reader isn't looking around thinking, "Ok, there's this parameter x . . . what's it for?"</description>
		<content:encoded><![CDATA[<p>I like the use of extension methods for this but I don&#8217;t like the extraneous &#8220;x =&gt;&#8221; when the parameter is not actually being used for anything in the lambda.  I don&#8217;t know how much of an improvement it would be, but you could change your predicate to take a Func (no input parameters) and use the no-parameter syntax:</p>
<p>this.Require(() =&gt; !string.IsNullOrEmpty(location));</p>
<p>At least that way the reader isn&#8217;t looking around thinking, &#8220;Ok, there&#8217;s this parameter x . . . what&#8217;s it for?&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Matthews</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20339</link>
		<dc:creator>Andrew Matthews</dc:creator>
		<pubDate>Mon, 21 Jan 2008 05:52:46 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20339</guid>
		<description>Thanks Stephen,

Thanks for your kind remarks - glad to know you (too) are getting something out of this! ;-)

Yes, In this case the syntax of any predicate will be of the form:

  static void Assert(bool testresult){if(!test) throw new Exception();}

but there is an implied semantic difference, which is why I've distinguished between the two (needlessly). A Require-type method is equivalent to any other 'Assert' type method - it checks the incoming boolean, and if false it throws. The Ensure, on the other hand could do a more complicated job, because it it not only testing the current state, but comparing it to the previous state.

In addition, the Ensure could throw a different contract exception like EnsureException or something like that. I've done this before to distinguish the type of predicate I was testing. I haven't done it with this little system, but I probably should. That would be easier to implement if I had two predicate testing methods: Require and Ensure.

Cheers

Andrew</description>
		<content:encoded><![CDATA[<p>Thanks Stephen,</p>
<p>Thanks for your kind remarks - glad to know you (too) are getting something out of this! ;-)</p>
<p>Yes, In this case the syntax of any predicate will be of the form:</p>
<p>  static void Assert(bool testresult){if(!test) throw new Exception();}</p>
<p>but there is an implied semantic difference, which is why I&#8217;ve distinguished between the two (needlessly). A Require-type method is equivalent to any other &#8216;Assert&#8217; type method - it checks the incoming boolean, and if false it throws. The Ensure, on the other hand could do a more complicated job, because it it not only testing the current state, but comparing it to the previous state.</p>
<p>In addition, the Ensure could throw a different contract exception like EnsureException or something like that. I&#8217;ve done this before to distinguish the type of predicate I was testing. I haven&#8217;t done it with this little system, but I probably should. That would be easier to implement if I had two predicate testing methods: Require and Ensure.</p>
<p>Cheers</p>
<p>Andrew</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Pope</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20338</link>
		<dc:creator>Stephen Pope</dc:creator>
		<pubDate>Mon, 21 Jan 2008 00:01:33 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20338</guid>
		<description>Very nice article (as always). I cam see how the Require function alone is massively helpful. 

Im just coming to terms with all the DBC / Spec# stuff and I had a very quick question .. does this mean that Ensure has exactly the same syntax as Require ?</description>
		<content:encoded><![CDATA[<p>Very nice article (as always). I cam see how the Require function alone is massively helpful. </p>
<p>Im just coming to terms with all the DBC / Spec# stuff and I had a very quick question .. does this mean that Ensure has exactly the same syntax as Require ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#187; Daily Bits - January 20, 2008 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20337</link>
		<dc:creator>&#187; Daily Bits - January 20, 2008 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.</dc:creator>
		<pubDate>Sun, 20 Jan 2008 15:09:20 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20337</guid>
		<description>[...] C# by Contract - Using Expression Trees [...]</description>
		<content:encoded><![CDATA[<p>[...] C# by Contract - Using Expression Trees [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Moser</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20335</link>
		<dc:creator>Jeff Moser</dc:creator>
		<pubDate>Sat, 19 Jan 2008 14:20:36 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20335</guid>
		<description>I definitely think that to do DBC well, we'll need framework and compiler support. I really hope Spec# goes mainstream into a future version of C#. It appears that the Boogie component of Spec# does some nice static analysis/proving of conditions. I'd love to see these tools all converge to something like much better compiler errors that would tell you that "If I compiled this code, it'd produce a contract violation."

Right now in my production code, I'm sort of doing a hybrid of a "Guard" class for basic contracts and Asserts for ensure clauses. I'll be checking this blog for ideas on how to go further than that though.</description>
		<content:encoded><![CDATA[<p>I definitely think that to do DBC well, we&#8217;ll need framework and compiler support. I really hope Spec# goes mainstream into a future version of C#. It appears that the Boogie component of Spec# does some nice static analysis/proving of conditions. I&#8217;d love to see these tools all converge to something like much better compiler errors that would tell you that &#8220;If I compiled this code, it&#8217;d produce a contract violation.&#8221;</p>
<p>Right now in my production code, I&#8217;m sort of doing a hybrid of a &#8220;Guard&#8221; class for basic contracts and Asserts for ensure clauses. I&#8217;ll be checking this blog for ideas on how to go further than that though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Matthews</title>
		<link>http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20334</link>
		<dc:creator>Andrew Matthews</dc:creator>
		<pubDate>Sat, 19 Jan 2008 08:22:37 +0000</pubDate>
		<guid isPermaLink="false">http://aabs.wordpress.com/2008/01/18/c-by-contract-using-expression-trees/#comment-20334</guid>
		<description>Hi Jeff,

I noticed the same (frustratingly internal) things too. I also enjoyed your post very much. I'm sorry I didn't give you any link love at the time of writing - I forgot the URL between me reading your post and revisiting this issue myself.

Using a stack trace might be acceptable, except that it only tells me _where_ the exception got thrown (not why). That where part will be in the predicate method, not in the method that called the predicate, so it will not be so useful. Also being able to literally show the code (rather than its location) is really useful when you're potentially just looking in log files. An exception class or stack trace can't do this normally - that's why I had to resort to LambaExpressions.

Going back to the disassembly of the System.Core assembly, it looks as though there's a method called 'Old' that seems to serve the same purpose as my 'before' extension method. It's not clear how or where that gets used, but it definitely requires the spec# assembly rewriter to provide those snapshot services. My 'before' extension method allows me to describe the effects that the method will produce if you meet the contract in the require clauses of the method. Which is critical to DBC, in ensuring that you the service provider meet the terms of the contract.

Unfortunately, one critical aspect of the DBC picture is still missing from my implementation - inheritance. In Eiffel, and attributes in .NET, you can control the inheritance of a constraint. With virtual methods, a specialisation relationship increases the total constraints as new constraints get added to the method in the sub-class. Using constraints within the body of the method denies me that ability. That's the reason I was so set on using attributes to begin with, where the 'GetCustomAttributes' method will retrieve all of the attributes all the way up the inheritance hierarchy.

I see that flaw as pretty major in my implementation, but right now I can't see a way around it. If you have any ideas, please let me know. I think DBC is a very powerful way to increase the quality of my code, and to help me think about how it works in the abstract. So I'd like to get it right.

Cheers

Andrew</description>
		<content:encoded><![CDATA[<p>Hi Jeff,</p>
<p>I noticed the same (frustratingly internal) things too. I also enjoyed your post very much. I&#8217;m sorry I didn&#8217;t give you any link love at the time of writing - I forgot the URL between me reading your post and revisiting this issue myself.</p>
<p>Using a stack trace might be acceptable, except that it only tells me _where_ the exception got thrown (not why). That where part will be in the predicate method, not in the method that called the predicate, so it will not be so useful. Also being able to literally show the code (rather than its location) is really useful when you&#8217;re potentially just looking in log files. An exception class or stack trace can&#8217;t do this normally - that&#8217;s why I had to resort to LambaExpressions.</p>
<p>Going back to the disassembly of the System.Core assembly, it looks as though there&#8217;s a method called &#8216;Old&#8217; that seems to serve the same purpose as my &#8216;before&#8217; extension method. It&#8217;s not clear how or where that gets used, but it definitely requires the spec# assembly rewriter to provide those snapshot services. My &#8216;before&#8217; extension method allows me to describe the effects that the method will produce if you meet the contract in the require clauses of the method. Which is critical to DBC, in ensuring that you the service provider meet the terms of the contract.</p>
<p>Unfortunately, one critical aspect of the DBC picture is still missing from my implementation - inheritance. In Eiffel, and attributes in .NET, you can control the inheritance of a constraint. With virtual methods, a specialisation relationship increases the total constraints as new constraints get added to the method in the sub-class. Using constraints within the body of the method denies me that ability. That&#8217;s the reason I was so set on using attributes to begin with, where the &#8216;GetCustomAttributes&#8217; method will retrieve all of the attributes all the way up the inheritance hierarchy.</p>
<p>I see that flaw as pretty major in my implementation, but right now I can&#8217;t see a way around it. If you have any ideas, please let me know. I think DBC is a very powerful way to increase the quality of my code, and to help me think about how it works in the abstract. So I&#8217;d like to get it right.</p>
<p>Cheers</p>
<p>Andrew</p>
]]></content:encoded>
	</item>
</channel>
</rss>
