
{"id":5108,"date":"2024-09-01T17:38:24","date_gmt":"2024-09-01T21:38:24","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5108"},"modified":"2024-09-01T17:47:23","modified_gmt":"2024-09-01T21:47:23","slug":"what-is-dead-code","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5108","title":{"rendered":"What is dead code?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignnone size-full wp-image-5109\" src=\"https:\/\/ikriv.com\/blog\/wp-content\/uploads\/2024\/08\/dead_code.png\" alt=\"\" width=\"160\" \/><br \/>\nWhat is dead code?<\/p>\n<p>Looks like it depends on who you ask. Some people think it&#8217;s unreachable code than never executes. Other people think that it&#8217;s redundant code that does execute, but has no influence on the final outcome.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef square(x):\r\n   y = x*10                    # redundant code\r\n   return x*x\r\n   print(&quot;We never get here&quot;)  # unreachable code\r\n<\/pre>\n<p>We just had this confusion play out at work: I asked my colleague to delete the &#8220;dead code&#8221; meaning unreachable code, but he ended up deleting not only that code, but also the code he thought was redundant.<\/p>\n<p>When you google &#8220;dead code&#8221;, Google AI overview say:<\/p>\n<p>&#8220;<i>Dead code is a segment of code unnecessary for the successful operation of a particular software program. These are sections of code that are executed, but not used, accessed, or referenced during software operation.<\/i>&#8221;<\/p>\n<p>On the other hand, many web sites define &#8220;dead code&#8221; as &#8220;code that is never executed&#8221;. E.g. Python <a href=\"https:\/\/pypi.org\/project\/deadcode\/\">deadcode<\/a> package says it &#8220;removes dead code&#8221;, and defines it as &#8220;<i>variables\/functions\/classes\/files which are not used in a whole code base.<\/i>&#8221;<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Dead_code\">Wikipedia article for dead code<\/a> has both definitions, and cites the sources accordingly:<\/p>\n<p>&#8220;<i>The term dead code has multiple definitions. Some use the term to refer to code (i.e. instructions in memory) which can never be executed at run-time. In some areas of computer programming, dead code is a section in the source code of a program which is executed but whose result is never used in any other computation.<\/i>&#8221;<\/p>\n<p>Thus, in order to avoid confusion, it is advisable to use terms &#8220;unreachable code&#8221; when we are talking about something that is never executed, and &#8220;redundant code&#8221; when we are talking about something that may or may not be executed, but has no influence on the final outcome.<\/p>\n<p>PS. It can sometimes get rather murky what is redundant or even what is unreachable. Suppose we have this check:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif square(x) &lt; 0:\r\n   raise ValueError(&quot;The square is negative!&quot;)  # is it unreachable?\r\n<\/pre>\n<p>Is the <code>raise<\/code> statement unreachable? If <code>square<\/code> function works as expected, then it is. But if the function is complex (pun intended), it may not work as we expect, and negative squares may happen. One&#8217;s man unreachable code is another&#8217;s defensive programming.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is dead code? Looks like it depends on who you ask. Some people think it&#8217;s unreachable code than never executes. Other people think that it&#8217;s redundant code that does <a href=\"https:\/\/ikriv.com\/blog\/?p=5108\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":["entry","author-ikriv","post-5108","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5108"}],"version-history":[{"count":3,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5108\/revisions"}],"predecessor-version":[{"id":5112,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5108\/revisions\/5112"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}