{"id":315,"date":"2009-03-16T07:44:54","date_gmt":"2009-03-16T06:44:54","guid":{"rendered":"http:\/\/www.leading-edge-dev.de\/?p=315"},"modified":"2010-07-10T16:21:06","modified_gmt":"2010-07-10T15:21:06","slug":"code-contracts-contract-checking","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts-contract-checking","title":{"rendered":"Code Contracts #3: Contract checking"},"content":{"rendered":"<p>In my first two posts of this series, I covered some <a href=\"http:\/\/www.leading-edge-dev.de\/?p=290\">basic information<\/a> about Code Contracts, their origins and core elements of DbC as well as the basic declarative nature of Code Contracts, the <a href=\"http:\/\/www.leading-edge-dev.de\/?p=303\">Contract Rewriter<\/a> and why contracts are defined imperatively and not via attributation or additional keywords. In this blog post, I&#8217;ll dig even deeper to show the two main elements, how contracts on your code are checked&#8230;<\/p>\n<p>The functionality of checking contracts\/conditions on your code comes from two main components of Code Contracts: the <em>dynamic checker<\/em> (runtime checker) and the <em>static checker<\/em>. Those can be configured and activated after installation of the Code Contracts package through a new tab inside the project&#8217;s Properties window:<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_1_projectproperties_new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-316\" style=\"border: 1px solid black; margin-top: 10px; margin-bottom: 10px;\" title=\"Configuring Code Contracts in the project's Properties window\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_1_projectproperties_new.png\" alt=\"Configuring Code Contracts in the project's Properties window\" width=\"531\" height=\"348\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_1_projectproperties_new.png 758w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_1_projectproperties_new-300x196.png 300w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/a>More information on that can be found e.g. on the <a href=\"http:\/\/download.microsoft.com\/download\/C\/2\/7\/C2715F76-F56C-4D37-9231-EF8076B7EC13\/userdoc.pdf\">Code Contracts User Manual<\/a>.<\/p>\n<h3>The dynamic checker<\/h3>\n<p>The first component is the <em>dynamic checker<\/em>, which is even called runtime checker, what better implies its behavior. This type of contract checking is done at runtime of your program code. Everytime when the program execution hits a point, where contracts are defined, the Contracts runtime checks, if the contracts are fulfilled in the current context, that means with the current class state, current variable and parameter values etc. Thus the dynamic checker is searching for <em>actual<\/em> broken contracts in just the current context. Its behavior is very similar to a test (under the given context) resp. to a <code>Debug.Assert()<\/code>, leading to the term of <em>validation<\/em> of your code. If a contract check fails, then the default behavior is that a <code>ContractException<\/code> is thrown and the application terminates. This can be easily adopted to your own needs.<\/p>\n<h3>The static checker<\/h3>\n<p>So, dynamic checking ensures at runtime that no contracts are broken by current calls of your code. But it leads to the same problems as in unit testing: code coverage, choice of input\/output pairs etc. Perhaps contract violations are not found during testing, because the checking with a chosen set of parameters fulfills those contracts. Problems occur, when you deliver your code and other developers use parameters that you didn&#8217;t consider and then contracts are not fulfilled any more.<\/p>\n<p>The second and more interesting part of Code Contracts checking is the <em>static checker<\/em>. What&#8217;s the difference to dynamic checking? The static checker is able to check defined contracts at <em>compile time<\/em> of your code. Moreover, the static checker doesn&#8217;t only search for actual broken contracts, but for <em>all possibilities<\/em> where contracts could be broken by the defined code. For example, let&#8217;s imagine a method <code>int Predecessor(int x)<\/code>, that returns the value <code>x-1<\/code> and a post-condition on that method, that the return value is in every case <code>\"&gt;= 0<\/code><code>\"<\/code>. With <em>dynamic<\/em> checking, perhaps you input some arguments for x: 1,5,100,13, &#8230; it would never lead to a situation, where the contract is broken. With <em>static<\/em> checking instead, the static checker would analyze the code at compile time and would recognize, that there are situations (every value for <code>x &lt; 1<\/code>) where the post-condition of the method is not fulfilled. In this case, the static checker would output a warning to inform the developer of the possible contract breach.<\/p>\n<p>With all this, the static checker is doing a formal <a href=\"http:\/\/en.wikipedia.org\/wiki\/Verification_and_Validation\"><em>verification<\/em><\/a> of the defined contracts in contrast to the test-based validation of the dynamic checker. This is done by a complex algorithm that&#8217;s based on methods of formal logic and program verification and that&#8217;s why the static checker needs some time to perform the verification. The concrete algorithm is using <a href=\"http:\/\/en.wikipedia.org\/wiki\/Abstract_interpretation\">abstract interpretation<\/a> of the code, thus the code is being inspected, but not executed! By this process and with a basic rule-set on the CLR, facts about the code are generated and those facts are used to find out, if the code fulfills the defined contracts or not. The behavior of Code Contracts is very nice in comparison with the dynamic checking, but the complexity of this process may not be underestimated. Moreover, it&#8217;s heavily requiring contracts on most code pieces that you use in your contracted code, for example in the .NET framework. That&#8217;s why the Code Contracts team is currently working hard to introduce contracts all over the .NET libraries. Let&#8217;s do an example: if you have a <code>string s<\/code> and call <code>s.Contains()<\/code>, then you as programmer know that <code>s.Contains()<\/code> doesn&#8217;t change <code>s<\/code> or any other values. But if there would be no contracts on <code>String.Contains()<\/code>, then the static checker of Code Contracts could not know that <code>Contains()<\/code> is free of side effects and isn&#8217;t changing the value of <code>s<\/code>. Thus, it would warn on any post-condition of a method, that makes use of the value of <code>s<\/code>.<\/p>\n<p>The following example shows a compiler warning, created by the static checker. The method <code>WithDraw()<\/code> for withdrawing money from an account has been understood and implemented as deposit (using the <code>Deposit()<\/code> method) with a negative <code>amount<\/code>. The static checker fails, because on deposit only positive values are allowed and that&#8217;s what the pre-condition on <code>Deposit()<\/code> declares. The warning is shown in the following picture. Furthermore you can see, that Code Contracts is suggesting a pre-condition on <code>WithDraw()<\/code>, by which the contract could be fulfilled.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-322\" style=\"border: 1px solid black; margin-top: 10px; margin-bottom: 10px;\" title=\"Warning of the static checker at compile time\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_2_staticcheckerwarning.png\" alt=\"Warning of the static checker at compile time\" width=\"595\" height=\"331\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_2_staticcheckerwarning.png 744w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/03\/codecontracts_1_2_staticcheckerwarning-300x166.png 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/p>\n<p>And that&#8217;s it again for the moment. In this blog post I&#8217;ve introduced the two core checking components of Code Contracts: the static checker and the dynamic checker. Where the dynamic checker is just doing a <em>validation<\/em> at runtime under the actual values context, the static checker is performing a complex formal <em>verification<\/em> at compile time, which finds all possible contract breaches. Those are two very different approaches and should be clear before using Code Contracts. Next blog posts will give you more examples on defining contracts and further insights.<\/p>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d315\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d315\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my first two posts of this series, I covered some basic information about Code Contracts, their origins and core elements of DbC as well as the basic declarative nature of Code Contracts, the Contract Rewriter and why contracts are defined imperatively and not via attributation or additional keywords. In this blog post, I&#8217;ll dig &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts-contract-checking\" class=\"more-link\"><span class=\"screen-reader-text\">Code Contracts #3: Contract checking<\/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":[6,126],"tags":[319,129,142,128,143,135],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-dot-net","category-code-contracts","tag-code-contracts","tag-dynamic-checker","tag-runtime-checker","tag-static-checker","tag-validation","tag-verification"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/315","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=315"}],"version-history":[{"count":14,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions\/957"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}