{"id":13499,"date":"2025-06-02T21:58:11","date_gmt":"2025-06-02T19:58:11","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=13499"},"modified":"2025-06-02T21:58:11","modified_gmt":"2025-06-02T19:58:11","slug":"design-by-contract-dbc-v-test-driven-design-tdd-wolands-cat","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2025\/06\/02\/design-by-contract-dbc-v-test-driven-design-tdd-wolands-cat\/","title":{"rendered":"Design-by-Contract (DbC) v Test-Driven Design (TDD) | Woland&#8217;s cat"},"content":{"rendered":"<p><em><a href=\"https:\/\/wolandscat.net\/2021\/03\/04\/design-by-contract-dbc-v-test-driven-design-tdd\/\">Design-by-Contract (DbC) v Test-Driven Design (TDD) from\u00a0 Woland&#8217;s cat<\/a><\/em><\/p>\n<p>shortly:<strong> DbC = contracts + tests<\/strong><\/p>\n<p><!--more--><!--nextpage--><\/p>\n<blockquote>\n<h2 class=\"entry-title\"><a href=\"https:\/\/wolandscat.net\/2021\/03\/04\/design-by-contract-dbc-v-test-driven-design-tdd\/\" rel=\"bookmark\">Design-by-Contract (DbC) v Test-Driven Design\u00a0(TDD)<\/a><\/h2>\n<div class=\"entry-meta\"><span class=\"meta-prep meta-prep-author\">Posted on<\/span> <a title=\"18:55\" href=\"https:\/\/wolandscat.net\/2021\/03\/04\/design-by-contract-dbc-v-test-driven-design-tdd\/\" rel=\"bookmark\"><span class=\"entry-date\">04\/03\/2021<\/span><\/a> <span class=\"meta-sep\">by<\/span> <span class=\"author vcard\"><a class=\"url fn n\" title=\"View all posts by wolandscat\" href=\"https:\/\/wolandscat.net\/author\/wolandscat\/\">wolandscat<\/a><\/span><\/div>\n<div class=\"entry-content\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1803\" data-permalink=\"https:\/\/monodes.com\/predaelli\/2016\/10\/18\/w3c-html5-logo\/html5-logo-300x203-1\/\" data-orig-file=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/10\/html5-logo-300x203-1.png?fit=300%2C203&amp;ssl=1\" data-orig-size=\"300,203\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"html5-logo-300&amp;#215;203-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/10\/html5-logo-300x203-1.png?fit=300%2C203&amp;ssl=1\" class=\"wp-image-1803\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2025\/06\/eiffel_contract.png?resize=910%2C278&#038;ssl=1\" sizes=\"auto, (max-width: 913px) 100vw, 913px\" srcset=\"https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png 913w, https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?w=150 150w, https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?w=300 300w, https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?w=768 768w\" alt=\"\" width=\"910\" height=\"278\" data-attachment-id=\"1803\" data-permalink=\"https:\/\/wolandscat.net\/eiffel_contract\/\" data-orig-file=\"https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png\" data-orig-size=\"913,279\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"eiffel_contract\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?w=300\" data-large-file=\"https:\/\/wolandscat.net\/wp-content\/uploads\/2021\/03\/eiffel_contract.png?w=640\" \/><\/a><figcaption>A software contract in the Eiffel language<\/figcaption><\/figure>\n<p>Another bit of software engineering knowledge from my archive relates to two well-known formal quality methods used in software development. This is from a presentation made at ETH Zurich in 2010.<\/p>\n<p><span id=\"more-1796\"><\/span>The background to this is my use of the Eiffel language for 25 years, in which Design by Contract (DbC) figures strongly. Its creator, Bertrand Meyer (currently provost Chair of Software Engineering at Schaffhausen Institute of Technology, Switzerland) coined the term, and developed DbC from earlier formulations such as in the Z language, Djikstra etc.<\/p>\n<p>For those not familiar with \u2018contracts\u2019, the full form consists of routine pre- and post-conditions and class invariants. An example of a contract is shown below:<\/p>\n<pre class=\"wp-block-preformatted\"><em>fill<\/em> <strong>is<\/strong>\n        -- Fill tank with liquid\n    <strong>require<\/strong>\n        <em>in_valve.open\n        out_valve.closed<\/em>\n    <strong>deferred<\/strong>\n        -- i.e., no implementation\n    <strong>ensure<\/strong>\n        <em>in_valve.closed\n        out_valve.closed\n        is_full<\/em>\n    <strong>end<\/strong><\/pre>\n<p>The contract consists of the require and ensure predicates. The <a href=\"https:\/\/www.eiffel.org\/doc\/solutions\/Design_by_Contract_and_Assertions\">Eiffel website has a full explanation<\/a>.<\/p>\n<p>DbC was built into Eiffel (since 1988), and has some native supported in some languages like Ada 2012, Clojure, and Kotlin. Many other languages have had additional frameworks or tools created to add contracts, e.g. Java, C#, Groovy and so on.<\/p>\n<p>However, it has to be said that the culture of contract-oriented has not become mainstream, which those of us who use them routinely find strange \u2013 they catch so many errors in testing that it is clear they greatly add to the quality of software.<\/p>\n<p>Some experts think that Test-Driven Design (TDD) is a better alternative, since can be used to force developers to prove that each function they write passes an appropriate test, before they write more code. Personally, although I think routine use of unit testing via built-in frameworks is useful, TDD doesn\u2019t replace DbC, and indeed, it can lead some developers to think their software, if passing all its tests, is \u2018done\u2019.<\/p>\n<p>Understanding the difference between DbC and TDD is instructive to understanding why TDD, although very useful, doesn\u2019t tell you if your software is correct.<\/p>\n<p>Here\u2019s the basic difference:<\/p>\n<ul class=\"wp-block-list\">\n<li>A DbC contract is a <em>mathematical specification<\/em> of the valid domain (input state space) and\/or result (output state space) of a routine within a class;<\/li>\n<li>TDD is development using \u2018tests\u2019 that constitute <em>specific points<\/em> in a routine\u2019s input value space, which test a routine <em>on an instance<\/em>.<\/li>\n<\/ul>\n<p>Mathematically, contracts are <em>intensional<\/em> (i.e. formally stated) definitions of valid state spaces. Tests are a kind of <em>extensional<\/em> definition in that they form a list of <em>particular<\/em> input or output values. From tests on their own, it is hard to a) determine what the total valid value space is and b) hard to see the <em>design intent<\/em> of the routine.<\/p>\n<p>Since you still do testing if you are using DbC, <strong>DbC = contracts + tests<\/strong>. If you are using TDD only, you are just using tests \u2013 you have no formal statement of the intended valid state space of input values or results. It is therefore a somewhat haphazard exercise to know what tests to write, especially against someone else\u2019s code. How do you know if you have sufficient coverage? Answer: you don\u2019t.<\/p>\n<p>Indeed, the presence of contracts is an important indicator for writing new tests, since the contract is telling you which kinds of values or states will make the software execute correctly or not.<\/p>\n<p>I remain mystified as to why they are not built into more languages.<\/p>\n<\/div>\n<\/blockquote>\n<div id=\"inkAppRoot\">\n<div><\/div>\n<\/div>\n<div id=\"inkPopUp\">\n<div><\/div>\n<\/div>\n<div id=\"inkAppFlex\">\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Design-by-Contract (DbC) v Test-Driven Design (TDD) from\u00a0 Woland&#8217;s cat shortly: DbC = contracts + tests<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2025\/06\/02\/design-by-contract-dbc-v-test-driven-design-tdd-wolands-cat\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"federated","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34],"tags":[],"class_list":["post-13499","post","type-post","status-publish","format-standard","hentry","category-eiffel"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-3vJ","jetpack-related-posts":[{"id":8299,"url":"https:\/\/monodes.com\/predaelli\/2021\/03\/31\/design-by-contract-in-a-spreadsheet\/","url_meta":{"origin":13499,"position":0},"title":"Design by contract in a spreadsheet!","author":"Paolo Redaelli","date":"2021-03-31","format":false,"excerpt":"Today I wanted to format a boolean value in a spreadsheet so that true values are shown as \"S\u00ec\" and false as \"No\" (italian words for \"yes\" and \"no\"). I was easy as librelive describe in Change Boolean to Yes\/No instead of True\/False - Ask LibreOffice i suggest basically the\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3904,"url":"https:\/\/monodes.com\/predaelli\/2018\/03\/23\/the-mistakes-i-made-as-a-beginner-programmer-samer-buna-medium\/","url_meta":{"origin":13499,"position":1},"title":"The Mistakes I Made As a Beginner Programmer \u2013 Samer Buna \u2013 Medium","author":"Paolo Redaelli","date":"2018-03-23","format":"link","excerpt":"Good reading: The Mistakes I Made As a Beginner Programmer by\u00a0 Samer Buna The Mistakes I Made As a Beginner Programmer Learn to identify them, make habits to avoid\u00a0them Let me make one thing clear first. If you are a beginner programmer, this article is not meant to make you\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1286,"url":"https:\/\/monodes.com\/predaelli\/2016\/04\/17\/eiffel-a-viable-candidate-as-a-language-for-the-gnome-platform\/","url_meta":{"origin":13499,"position":2},"title":"Eiffel: A viable candidate as a language for the Gnome platform ?","author":"Paolo Redaelli","date":"2016-04-17","format":false,"excerpt":"Eiffel: A viable candidate as a language for the Gnome platform ? It was 2004. Linux were labelled as a cancer by Ballmer, Android and iPhone didn't existed. Multi-core CPU were still high-end. It was a different world. From archive.org, before it got lost.... Twelve years ago. And it was\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1100,"url":"https:\/\/monodes.com\/predaelli\/2016\/03\/12\/unfinished\/","url_meta":{"origin":13499,"position":3},"title":"Unfinished","author":"Paolo Redaelli","date":"2016-03-12","format":false,"excerpt":"I found this photo on the website of professional association of architects of Monza and Brianza. It makes me feel sad: I personally designed the concretes used to built that unfinished tower.\u00a0 Then I found this petition asking to demolish it. Then I don't feel sad. Sadness turns into anger.\u2026","rel":"","context":"In &quot;Mood&quot;","block_context":{"text":"Mood","link":"https:\/\/monodes.com\/predaelli\/category\/mood\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/slider_pic2.jpg?fit=1200%2C380&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/slider_pic2.jpg?fit=1200%2C380&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/slider_pic2.jpg?fit=1200%2C380&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/slider_pic2.jpg?fit=1200%2C380&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/slider_pic2.jpg?fit=1200%2C380&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5720,"url":"https:\/\/monodes.com\/predaelli\/2019\/06\/13\/not-as-freedom\/","url_meta":{"origin":13499,"position":4},"title":"Not as freedom&#8230;","author":"Paolo Redaelli","date":"2019-06-13","format":false,"excerpt":"\u00a0 I've been designing concrete mixes for almost 20 years and I've always used spreadsheets. Seeing those frees for a \"free product\" Free Concrete Mix Design Software | Concrete Quality make me think that we do actually need one which is free-as-in-freedom The Concrete Quality software is free\" Free to\u2026","rel":"","context":"In &quot;Ethics&quot;","block_context":{"text":"Ethics","link":"https:\/\/monodes.com\/predaelli\/category\/ethics\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2019\/06\/Screenshot_2019-06-13-Free-Concrete-Mix-Design-Software-Concrete-Quality-510x241-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":10343,"url":"https:\/\/monodes.com\/predaelli\/2023\/04\/01\/debian-programs-from-stable-testing-unstable-and-even-experimental-without-trauma\/","url_meta":{"origin":13499,"position":5},"title":"Debian: programs from stable, testing, unstable and even experimental without trauma","author":"Paolo Redaelli","date":"2023-04-01","format":false,"excerpt":"I originally wrote this in Italian but I think that it may be more useful translated into English In daily use of computers it often happens that you need a stable base and at the same time use recent, very recent or even 'beta\/alpha\/experimental' versions of some specific programs. Debian\u2026","rel":"","context":"In &quot;Debian&quot;","block_context":{"text":"Debian","link":"https:\/\/monodes.com\/predaelli\/category\/debian\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/13499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=13499"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/13499\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=13499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=13499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=13499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}