{"id":68,"date":"2016-05-25T04:49:50","date_gmt":"2016-05-25T04:49:50","guid":{"rendered":"http:\/\/96.126.106.214\/?p=68"},"modified":"2023-11-26T01:01:12","modified_gmt":"2023-11-26T01:01:12","slug":"falsifiability-and-unit-testing","status":"publish","type":"post","link":"https:\/\/codingismycraft.blog\/index.php\/2016\/05\/25\/falsifiability-and-unit-testing\/","title":{"rendered":"Falsifiability and Unit Testing"},"content":{"rendered":"<p>Without a doubt, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Test-driven_development\">Test Driven Development<\/a> (TDD) lies in the core of my software development approach. \u00a0I have been a very early adapter of automated testings since my C++ and C# days and always felt very enthusiastic about their impact on the quality of the software.<\/p>\n<p>I have to admit that it was not until I became a full time python developer that my commitment and understanding of TDD was perfected. Compared to statically linked languages like C# or Java, the dynamic nature of python multiplies the significance of unit testing, elevating it to one of the most fundamental tactics for the delivery of a successful project. More than this, I do not think that I will exaggerate if I claim that as a back end developer, the most frequent way I can see my code in action, is simply the execution of the test scripts.<\/p>\n<p>I\u00a0have spent a lot of time and effort thinking about the best practices and patterns that can simplify\u00a0testing and improve their \u00a0effectiveness in discovering as many bogus conditions as possible. \u00a0Although there exist preferable\u00a0heuristics and coding idioms which can indeed improve the degree of code&#8217;s testiness, \u00a0I still believe that the process of creating highly testable software involves a lot of talent and experience on top of knowledge that can be communicated through books or taught by a professor.<\/p>\n<h4><strong>Karl Popper and unit testing<\/strong><\/h4>\n<p>Aside from computer programming, the other topic I am always fascinated about is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Philosophy_of_science\">Philosophy of Science<\/a>. \u00a0Karl Popper, one of the greatest philosophers of science ever lived, introduced us to the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Falsifiability\">Falsifiability<\/a>\u00a0principle, which I believe is perfectly applicable to the concept of Test Driven Development.<\/p>\n<p>Falsifiability defines\u00a0the distinction between a\u00a0scientific theory and\u00a0metaphysics. Based on this principle a theory is considered scientific only if we can conceive an\u00a0observation\u00a0to nullify it (prove it to be wrong).<\/p>\n<p>For example, a statement like &#8220;The Earth is flat&#8221; is scientific because it can be nullified if a traveler reaches his starting point after departing from a specific location and keep on moving towards the same direction. \u00a0The fact that a statement is scientific, does not mean that it is correct of course.<\/p>\n<blockquote><p>Based on Popper&#8217;s philosophy, we can never be sure\u00a0that a scientific theory is correct as the only thing we can do with it, is to prove it wrong by executing a <em>successful<\/em> falsifying test.<\/p><\/blockquote>\n<p>I believe that TDD constitutes a \u00a0projection\u00a0of\u00a0Karl Popper&#8217;s Falsifiability to the process of developing software, as it can be viewed as a special type of a &#8220;scientific&#8221; conjecture that needs to satisfy a collection of\u00a0tests whose purpose is \u00a0to &#8220;prove&#8221; it wrong by braking its expected behavior.<\/p>\n<p>In software development we need to substitute the term &#8220;scientific theory&#8221; with &#8220;software that works as intended&#8221; and the proposed experiments with tests that need to be satisfied.<\/p>\n<p>In the same way that we can never be certain about the absolute &#8220;truth&#8221; of a scientific theory but the best we can do is to explain as many experiments as possible, we can also never be sure that a specific piece of software is completely &#8220;bug free&#8221;. The best we can do is to describe as many tests as possible and prove that they can be served as expected without breaking the desired behaviour.<\/p>\n<p>At this point we need to clarify that, as strange as it might sound, a successful test is the one that fails! \u00a0A failing test needs our attention, as its existence means that we must revisit our code and resolve the related issues something that will cause the test to pass. Our mission as developers, is to increase the descriptiveness and comprehensiveness of our tests, trying to cover every execution path and parameter combination that can ever occur!<\/p>\n<h4><strong>Diminishing the value of TDD is a sign of inexperience<\/strong><\/h4>\n<p>As a developer, there have been many times when I have been presented to some legacy code that was clearly ill functioning and my mission was to either refactor or rewrite it from scratch. In almost all of these cases, the legacy platform was extremely difficult to test, as it presented a very tight coupling among its components and external dependencies, making it impossible to write effective and realistic tests.<\/p>\n<p>A very widespread tendency among the software project managers (PMs) is to diminish the value of testing. In their struggle to meet irrational deadlines, PMs are focusing on delivering a working and feature rich solution, usually underestimating its complexity and its potential for extensibility. Following this way of delivering software, TDD is one of the practices that is usually overlooked, as inexperienced and non technical PMs like to believe that writing comprehensive tests is very time consuming and it does not really add lots of business value in the final solution!<\/p>\n<p>In the core of my software development philosophy, I am viewing production code as a conjecture that is always paired with a vast array of tests that are meant to invalidate its functionality. I think that this approach is very well aligned with Popper&#8217;s <strong>Falsifiability<\/strong> and I have to admit that this analogy works fairly well when it comes to measuring the quality of a platform.<\/p>\n<p>I completely disagree with the view that the development of tests is a time consuming and expensive process. On the contrary, investing in extensive and well thought testing suites actually decreases the final cost of the application when we consider bug fixes, evolution to new technologies and adaption of new and originally unanticipated features.<\/p>\n<h4><strong>Conclusion<\/strong><\/h4>\n<p>Writing highly testable code requires experience, talent and knowledge of both generic testing patterns and all of the available platform specific utilities and tools. Possessing the ability to create testable software is one of the most critical qualities of a master developer. Code that is easy and convenient to test will outperform any other solution that is difficult to test, as it will evolve easier to meet future needs, extend its life span and maximize its cost-reward ratio.<\/p>\n<blockquote><p>To master the TDD process, the developer needs to reach a certain level of maturity and cultivate the ability to transform specifications to &#8220;Popperian&#8221; experiments expressed as simple tests, always trying to maintain the golden ratio of coverage over cost of development.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Without a doubt, Test Driven Development (TDD) lies in the core of my software development approach. \u00a0I have been a very early adapter of automated&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-programming"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":66,"url":"https:\/\/codingismycraft.blog\/index.php\/2016\/03\/10\/the-three-dimensions-of-a-programmer\/","url_meta":{"origin":68,"position":0},"title":"The three dimensions of a programmer.","author":"john","date":"March 10, 2016","format":false,"excerpt":"\u00a0 It is my impression that the vast majority of new developers, tend to underestimate the importance of design and implementation details as they struggle to prove that they can deliver a solution very quickly and please their managers with very tight deadlines. A developer eager to prove his abilities,\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codingismycraft.blog\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":30,"url":"https:\/\/codingismycraft.blog\/index.php\/2013\/03\/11\/python-is-great\/","url_meta":{"origin":68,"position":1},"title":"Python is great","author":"john","date":"March 11, 2013","format":false,"excerpt":"I have fallen in love with several programming languages in the past, with C++ being the most dominate of them. During the last years though, I have become a Python fan boy, trying to use it as much as I can, something that translates to use it almost everywhere except\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codingismycraft.blog\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":187,"url":"https:\/\/codingismycraft.blog\/index.php\/2020\/07\/03\/thinking-about-algorithmic-problems-ii\/","url_meta":{"origin":68,"position":2},"title":"Thinking about Algorithmic problems II","author":"john","date":"July 3, 2020","format":false,"excerpt":"Solving a programming problem that requires a custom algorithm can be broken in several steps which can be summarized in the creation of testing data, the implementation of a brutal force solution and the deeper study of the particular case which might reveal a smart trick to achieve an efficient\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codingismycraft.blog\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":279,"url":"https:\/\/codingismycraft.blog\/index.php\/2024\/10\/03\/the-challenges-of-python-migration-lessons-from-c-and-beyond\/","url_meta":{"origin":68,"position":3},"title":"The Challenges of Python Migration: Lessons from C++ and Beyond","author":"john","date":"October 3, 2024","format":false,"excerpt":"One project that confirmed the need for caution and conservatism when estimating deadlines involved migrating a massive codebase of over 4,000 Python files and 250+ open-source libraries from Python 3.6 to 3.10. What was initially seen as a straightforward task, expected to take just a few weeks, ended up consuming\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":35,"url":"https:\/\/codingismycraft.blog\/index.php\/2013\/03\/11\/programming-languages\/","url_meta":{"origin":68,"position":4},"title":"Programming Languages","author":"john","date":"March 11, 2013","format":false,"excerpt":"As a seasoned programmer I have used a wide spectrum of programming languages and development approaches. By the end of the 80's I was introduced to the concepts of Object Oriented Programming using as main language the C++ which I still follow very closely. As a heavy user of Visual\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":213,"url":"https:\/\/codingismycraft.blog\/index.php\/2017\/10\/04\/adding-descriptors-to-your-python-arsenal\/","url_meta":{"origin":68,"position":5},"title":"Adding descriptors to your python arsenal","author":"john","date":"October 4, 2017","format":false,"excerpt":"Python\u00a0provides a\u00a0 rich programming paradigm providing the ability to the programmer to express his solutions in a very concise and elegant way.\u00a0 One of the python features that is not in wide use although it gives the opportunity to simplify certain pieces of code,\u00a0is the descriptor protocol which is the\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codingismycraft.blog\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingismycraft.blog\/index.php\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}