{"id":618,"date":"2010-01-23T12:55:26","date_gmt":"2010-01-23T11:55:26","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=618"},"modified":"2010-05-31T13:39:49","modified_gmt":"2010-05-31T12:39:49","slug":"comparison-of-dbc-and-tdd-part-1","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/comparison-of-dbc-and-tdd-part-1","title":{"rendered":"Comparison: DbC and TDD &#8211; Part 1"},"content":{"rendered":"<p>Let&#8217;s come to another blog post in preparation of elaborating the synergy of DbC and TDD. My <a href=\"http:\/\/www.minddriven.de\/?p=608\">first blog post<\/a> on this topic covered an initial discussion on specification of code elements. Thereby it has shown different characteristics of DbC and TDD in terms of code specification. Since specification is one really important aspect in comparison of DbC and TDD, it&#8217;s of course not the only one. Hence today&#8217;s blog post is a starting point for a more general comparison of DbC and TDD with some other important aspects. There <a href=\"http:\/\/www.minddriven.de\/?p=638\">are<\/a> <a href=\"http:\/\/www.minddriven.de\/?p=654\">3<\/a> <a href=\"http:\/\/www.minddriven.de\/?p=700\">more<\/a> blog posts that I will come up with during the next weeks, forming a 4-part comparison series.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" title=\"DbC and TDD\" src=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2010\/01\/DbC_TDD.png\" alt=\"\" width=\"299\" height=\"125\" \/>To say<em> <\/em>first,<em> Test-Driven Development<\/em> (<em>TDD<\/em>) and <em>Design by Contract<\/em> (<em>DbC<\/em>) have very similar aims. Both concepts want to improve software quality by providing a set of conceptual building blocks that allow you to prevent bugs before making them. Both have impact on the design or development process of software elements. But both have their own characteristics and are different in achieving the purpose of better software. It&#8217;s necessary to understand commonalities and differences in order to take advantage of both principles in conjunction.<\/p>\n<h3>Specification again&#8230;<\/h3>\n<p>The <a href=\"http:\/\/www.minddriven.de\/?p=608\">last blog post<\/a> has already given a dedicated look at the specification aspect and how DbC and TDD can add some value. To summarize both principles extend the code-based specification in their own ways. TDD let&#8217;s you <strong>specify the expected behavior<\/strong> of a code element in an example-driven and reproducible way. It&#8217;s easy to use and allows the expression of any expected behavior. DbC on the other side sets universal contracts in place that <strong>extend the definition<\/strong> of a code element and are tightly coupled to it. It&#8217;s a great concept for narrowing the definition of a code element by defining additional physical constraints as preconditions, postconditions and invariants. By defining contracts on interfaces DbC strengthens the <em>role of interfaces<\/em> and enforces identical constraints\/behavior over all implementations of an interface. However not every behavior can be expressed by contracts and they&#8217;re bound to a single code element. Thus they don&#8217;t lessen the position of tests, but can be seen as great complement.<\/p>\n<h3>Design aspect<\/h3>\n<p>Both DbC and TDD have impact on the design of an API. Well, there is a slight but important difference as the names imply: it&#8217;s <em>Design<\/em> by Contract, but Test-Driven <em>Development<\/em>. Thus with DbC (<em>contract-first<\/em>) on the one side you are encouraged to write your contracts as you design your components (at <strong>design phase<\/strong>), which perfectly fits with the idea of contracts as extension of a component&#8217;s definition. With TDD and the <em>test-first <\/em>principle on the other side you write a test which maps to a new feature and afterwards you directly implement the code to get the test to green state. Hence TDD is tightly coupled to the <strong>development phase<\/strong> in contrast to DbC, which seems to come first. In addition personally I wouldn&#8217;t fight a religious war on this naming. If you think of DbC as &#8222;Contract-First <em>Development<\/em>&#8220; or &#8222;<em>Development<\/em> by Contract&#8220; you would have the contract-first principle coupled to the development phase as well. The more important thing is to find a way to effectively use contracts in the development cycle. If you are an advocate of up-front design you would perhaps want to set your contracts at design phase. But if you intensively use TDD it would be difficult to go down this design phase road. However you would set your contracts at development phase in conjunction with the test-first principle. This leads to the question of an effective development model with TDD and DbC and that&#8217;s another important story&#8230;<\/p>\n<p>But for now let&#8217;s come back to the impact of DbC and TDD to the design of an API. With TDD you write a new test for each new feature and then you bring this test to green by implementing some piece of logic. This is some form of <strong>Client-Driven Development<\/strong>. Your test is the client of your API and you call your methods from the client&#8217;s perspective (as a client would do). If the current API doesn&#8217;t fit your needs, you extend or modify it. Thus the resulting API is very focussed on the client&#8217;s needs and furthermore doesn&#8217;t contain code for unnecessary features, which is a great thing in terms of <a href=\"http:\/\/en.wikipedia.org\/wiki\/YAGNI\">YAGNI<\/a>. Another impact of TDD is that it leads to <em>loosely coupled components<\/em>. Tests in form of unit tests are very distinct and should be coupled to the tested component, but not far beyond that (other components that are called, e.g. data access). Thus there is a certain demand for loose coupling e.g. by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dependency_injection\">DI<\/a>.<\/p>\n<p>With DbC and contracts on your components you have other impacts. Contracts <strong>clarify the definition<\/strong> and intent of your components. When you come up with contracts you strengthen your opinion about a component by setting contractual obligations and benefits. This leads to much cleaner components. Moreover your components will have fewer responsibilities (and thus a better <em>cohesion<\/em>) since it would be painful to write contracts for components with many different responsibilities. Thus DbC is great in terms of supporting the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">SRP<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Separation_of_concerns\">SoC<\/a>. Another impact comes from the &#8222;limitation&#8220; of contracts to support only <em>pure methods<\/em> as part of a contract. If you want to use class methods in contracts (e.g. invariants) of this class you have to keep those methods pure. This leads to the enforcement of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Command-query_separation\">command-query separation<\/a> by contracts, which very often is <a href=\"http:\/\/martinfowler.com\/bliki\/CommandQuerySeparation.html\">a good thing<\/a> in terms of comprehensibility and maintainability.<\/p>\n<p>[<a href=\"http:\/\/www.minddriven.de\/?p=638\">To be continued&#8230;<\/a>]<\/p>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.minddriven.de%2f%3fp%3d618\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.minddriven.de%2f%3fp%3d618\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s come to another blog post in preparation of elaborating the synergy of DbC and TDD. My first blog post on this topic covered an initial discussion on specification of code elements. Thereby it has shown different characteristics of DbC and TDD in terms of code specification. Since specification is one really important aspect in &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/comparison-of-dbc-and-tdd-part-1\" class=\"more-link\"><span class=\"screen-reader-text\">Comparison: DbC and TDD &#8211; Part 1<\/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,209,203,210],"tags":[319,108,133,208,134,207,205,206],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-code-contracts","category-design-by-contract","category-software-design","category-test-driven-development","tag-code-contracts","tag-comparison","tag-dbc","tag-design","tag-design--by--contract","tag-specification","tag-tdd","tag-test--driven--development"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/618","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=618"}],"version-history":[{"count":34,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}