{"id":247,"date":"2018-01-03T01:53:08","date_gmt":"2018-01-03T01:53:08","guid":{"rendered":"http:\/\/freedville.com\/blog\/?p=247"},"modified":"2019-04-12T14:04:56","modified_gmt":"2019-04-12T14:04:56","slug":"book-review-your-code-as-a-crime-scene","status":"publish","type":"post","link":"https:\/\/freedville.com\/blog\/2018\/01\/03\/book-review-your-code-as-a-crime-scene\/","title":{"rendered":"Book Review: Your Code as a Crime Scene"},"content":{"rendered":"<p>Over the holiday break I read&nbsp;<a href=\"https:\/\/www.amazon.com\/Your-Code-Crime-Scene-Bottlenecks\/dp\/1680500384\/\">Your Code as a Crime Scene: Use Forensic Techniques to Arrest Defects, Bottlenecks, and Bad Design in Your Programs<\/a> by <a href=\"https:\/\/twitter.com\/AdamTornhill\">Adam Tornhill<\/a>.&nbsp; I was drawn by the title &#8211; after watching some CSI, who doesn&#8217;t want to think like a forensics expert?&nbsp; I&#8217;m glad to say I learned several things from reading this book.<\/p>\n<p>The overall premise is to use heuristics to identify problem code modules including &#8220;hotspots&#8221; that are likely to cause future problems, and to intelligently deal with these modules before they cause any more damage.&nbsp; These heuristics are built into a tool built by the author, called <a href=\"https:\/\/github.com\/adamtornhill\/code-maat\">Code Maat<\/a>.&nbsp; The problems include code of high complexity, high rate of change, or unexpected coupling to other code modules.&nbsp; Each of these problems is likely to cause new defects or to decrease the speed at which the overall application can be improved.&nbsp; The appendix suggests several ways to refactor these problem modules.<\/p>\n<p>The first chapters introduce several heuristics to help you gauge code and software complexity.&nbsp; The most interesting assertion from the author is that despite their inherently simplistic (and thus you might say flawed) nature, they are fast and good enough!&nbsp; The heuristics are great for combing through a mountain of possibilities and providing you a small subset of places worth reviewing.&nbsp; Several of these places will be false positives, but there are very few false negatives with these approaches.<\/p>\n<p>One interesting code-level heuristic is to use the&nbsp;<strong>shape<\/strong> of the code as a way to measure code complexity.&nbsp; Indeed, by taking a blurred snapshot of several code examples it was easy to see which code snippet was more complex than others.&nbsp; Other useful code-level heuristics were simply rate of change (number of commits affecting a module) and size (in lines) of code module.&nbsp; Further, by taking any of these heuristics and plotting them over time, you can figure out whether the quality of your codebase is increasing or decreasing!<\/p>\n<p>The author reiterates several times these forensic measures are to identify refactoring candidates.&nbsp; The next time you hear a developer say &#8220;we need to rip and replace&#8221; or worse &#8220;we need to rewrite from scratch&#8221;, try running the analyses from this book to see if a more targeted effort could work.<\/p>\n<p>The middle chapters take the code-level heuristics and map them to a system or architecture level.&nbsp; By grouping code into packages\/subsystems, you can determine which of these higher-level constructs need quality improvements.&nbsp; These chapters also introduce the concept of temporal coupling &#8211;&nbsp;different files\/modules that change at the same time &#8211; which is a sign of low quality architecture or architecture decay.&nbsp; Each of the architecture-level metrics can also be plotted over time.<\/p>\n<p>The final chapters remind you when to use these heuristics and when not to rely on them.&nbsp; Several of the heuristics measure and detect code churn.&nbsp; However, code churn is a symptom not an actual disease! Code changes for a reason and you can\u2019t tell from numbers alone if it was a good or bad reason.<\/p>\n<p>Overall, this is a useful book for developers and architects, and Code Maat is a useful tool for measuring code quality.&nbsp; Well worth a read and worth your time to evaluate your own code base against these techniques!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the holiday break I read&nbsp;Your Code as a Crime Scene: Use Forensic Techniques to Arrest Defects, Bottlenecks, and Bad Design in Your Programs by Adam Tornhill.&nbsp; I was drawn by the title &#8211; after&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[11],"_links":{"self":[{"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/247"}],"collection":[{"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":3,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}