{"id":348,"date":"2009-04-04T19:31:06","date_gmt":"2009-04-04T18:31:06","guid":{"rendered":"http:\/\/www.leading-edge-dev.de\/?p=348"},"modified":"2010-07-10T10:42:41","modified_gmt":"2010-07-10T09:42:41","slug":"code-contracts-4-modelling-implications","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/code-contracts-4-modelling-implications","title":{"rendered":"Code Contracts #4: Modelling implications"},"content":{"rendered":"<p>Hey guys. It&#8217;s me again on Code Contracts. Sorry for not posting something on this exciting topic during the last weeks, I&#8217;ve been busy with some other tasks. This day&#8217;s post will cover some more <em>technical<\/em> ascpect of Code Contracts: how can we model implications using Code Contracts?<\/p>\n<p>Code Contracts bring the DbC principle into the .NET world as one feature of the upcoming .NET framework 4.0. It&#8217;s providing statements on the code, which can be used to define specifications of how this code should work. Software elements should be understood as implementations which satisfy those specifications &#8211; that&#8217;s a very systematic approch and the core idea behind DbC. Let&#8217;s take a class method. With DbC in general and Code Contracts in particular, you&#8217;re able to define a system of obligations and benefits on this method. <em>Obligations<\/em> for clients of this method can be modelled via <em>pre-conditions<\/em> (Code Contracts: <code>Contract.Requires(...)<\/code>) to express, which conditions have to be satisfied by the client to run the method properly. In return the client get&#8217;s some <em>benefits<\/em> from the supplier (the method itself), if it maintains the obligations. Those guaranteed benefits can be defined by <em>post-conditions<\/em> on the method (Code Contracts: <code>Contract.Ensures(...)<\/code>). This obligation\/benefit-based approach is very similar to contracts between humans. Example: I give you some money (my obligation as client) and you give me in return a Toyota Prius (my benefit from you as supplier), but you&#8217;re not bound to give me a Ferrari or Mercedes or something.<\/p>\n<p>Thus in normal contract cases (code or real-life) you say &#8222;I give you <code>X<\/code> and you give me <code>Y<\/code> in return&#8220;. For a code example: &#8222;I give you an integer <code>X&gt;0<\/code> and you give me some number <code>Y<\/code> between <code>0<\/code> and <code>X<\/code> in return&#8220; (e.g. a random number). But other contracts can be imagined, where the benefit depends on what has been delivered by the client. Real-world example: &#8222;I give you 40.000 $ and you give me a Toyota Prius. But if I give you 75.000 $, you give me a Mercedes S-class.&#8220; That&#8217;s an <strong>implication<\/strong>. <strong>&#8222;If I give you <code>X<\/code>, you give me <code>Y<\/code>&#8220; or shortly &#8222;<code>X =&gt; Y<\/code>&#8222;<\/strong>. Cases can be imagined, where this could be useful in contracts on code. For example: &#8222;If you give me an empty list or null, I&#8217;ll return null. If you give me a non-empty list, I&#8217;ll return an element not equal to null.&#8220; In this scenario, it depends if you would use a pre-condition to prohibit empty lists or null&#8217;s as parameter or not, but let&#8217;s assume those cases are normal and allowed by the method, thus shifting the responsibility to handle those in the supplier (the method) instead of the client.<\/p>\n<p>Such implications are generally post-conditions on methods (&#8222;I&#8217;ll give you something&#8230;&#8220;), but cannot defined <em>directly<\/em> through Code Contracts. But that&#8217;s no definite issue of Code Contracts. Implications are a special form of boolean expressions and every form of boolean expression can be used as input of a <code>Contract.Ensures(...)<\/code> call. Rather that&#8217;s a limitation of most .NET languages like C#. They don&#8217;t have an implication operator for boolean expressions and therefore don&#8217;t have direct implication support.<\/p>\n<p>Solution for that is the decomposition of implications to primitive boolean expressions. Consider two boolean expressions <code>a<\/code> and <code>b<\/code>. Then the implication &#8222;<strong><code>a =&gt; b<\/code><\/strong>&#8220; is semantically equivalent to &#8222;<strong><code>!a || b<\/code>&#8222;<\/strong>, which can be used in this form in any program code.<\/p>\n<p>Let&#8217;s take the example from above again: &#8222;If you give me an empty list or null, I&#8217;ll return null. If you give me a non-empty list, I&#8217;ll return an element not equal to null.&#8220; This contract can be modelled with three implications: &#8222;list==null =&gt; null&#8220;, &#8222;list.Count==0 =&gt; null&#8220;, &#8222;list.Count &gt; 0 =&gt; !null&#8220;. Using Code Contracts and boolean substitution, we end in the following code:<\/p>\n<pre class=\"brush:csharp\">public string GetSomething(IList&lt;string&gt; list)\r\n{\r\n    Contract.Ensures(!(list == null) || (Contract.Result&lt;string&gt;() == null));\r\n    Contract.Ensures(!((list != null) && (list.Count == 0)) || (Contract.Result&lt;string&gt;() == null));\r\n    Contract.Ensures(!((list != null) && (list.Count != 0)) || (Contract.Result&lt;string&gt;() != null));\r\n\r\n    if ((list == null) || (list.Count == 0))\r\n        return null;\r\n\r\n    return list[0];\r\n}<\/pre>\n<p>Looks a little bit frightening, doesn&#8217;t it? Moreover, the static checker will be overwhelmed with that (doesn&#8217;t have a contract on the indexer of list and thus doesn&#8217;t know, that <code>list[0]<\/code> will return a value <code>!= null<\/code>). Ok, that&#8217;s perhaps not the best example, but it shows one general problem: expressions can become very big and obscure, even in relatively simple cases. That raises the question where and when to use contracts, but that&#8217;s another story&#8230;<\/p>\n<p>That&#8217;s it for the moment. As you can see, implications can be used not directly, but relatively easy by use of boolean expression substitution. One could call for direct support, but that&#8217;s a language feature (providing an implication operator) and need not to be considered by the Code Contracts team. More stuff to come as soon as I find some spare time.<\/p>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d348\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d348\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey guys. It&#8217;s me again on Code Contracts. Sorry for not posting something on this exciting topic during the last weeks, I&#8217;ve been busy with some other tasks. This day&#8217;s post will cover some more technical ascpect of Code Contracts: how can we model implications using Code Contracts? Code Contracts bring the DbC principle into &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/code-contracts-4-modelling-implications\" class=\"more-link\"><span class=\"screen-reader-text\">Code Contracts #4: Modelling implications<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[319,145,131,130],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-code-contracts","tag-code-contracts","tag-implications","tag-postcondition","tag-precondition"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=348"}],"version-history":[{"count":13,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/348\/revisions"}],"predecessor-version":[{"id":943,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/348\/revisions\/943"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}