
{"id":1645,"date":"2015-05-15T11:23:58","date_gmt":"2015-05-15T11:23:58","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1645"},"modified":"2015-05-17T11:31:04","modified_gmt":"2015-05-17T11:31:04","slug":"readability-and-maintainability-regulators-using-halstead-and-technical-debt","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1645","title":{"rendered":"Readability and Maintainability regulators using Halstead and Technical Debt measures."},"content":{"rendered":"<p style=\"color: #252525;\"><span style=\"color: #2a2a2a;\">The readability of source code has a direct impact on how well a developer comprehends a software system. Code maintainability refers to how easily that software system can be changed to add new features, modify existing features, fix bugs, or improve performance.<\/span><\/p>\n<p style=\"color: #252525;\">Many coding techniques exist to improve the readability and the maintainability. However it&#8217;s better to be assisted by some metrics to help refactor the\u00a0code and keep it clean. We can regulate our code base like an industrial process:<\/p>\n<p style=\"color: #252525;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/metrics.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1711\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/metrics.png\" alt=\"metrics\" width=\"781\" height=\"207\" \/><\/a><\/p>\n<p style=\"color: #252525;\"><b>Halstead complexity measures<\/b>\u00a0are\u00a0software metrics\u00a0introduced by Maurice Howard Halstead in 1977<span style=\"font-size: 10px;\">\u00a0<\/span>as part of his treatise on establishing an empirical science of software development.<\/p>\n<p style=\"color: #252525;\">Halstead&#8217;s goal was to identify measurable properties of software, and the relations between them. \u00a0Thus his metrics are actually not just complexity metrics.<\/p>\n<p>For a given problem, Let:<\/p>\n<ul>\n<li><img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/4\/3\/7\/437473c0c7936015844c4390a55a02f8.png\" alt=\"\\,\\eta_1\" \/>\u00a0= the number of distinct operators<\/li>\n<li><img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/4\/b\/e\/4be6367d86ce065ceab5c030decbb30c.png\" alt=\"\\,\\eta_2\" \/>\u00a0= the number of distinct operands<\/li>\n<li><img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/8\/3\/4\/83435643a88b91fe6c7b1feb3df2a4c8.png\" alt=\"\\,N_1\" \/>\u00a0= the total number of operators<\/li>\n<li><img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/6\/6\/1\/661e4b93e7eeb9cad50b38aa6f8929ff.png\" alt=\"\\,N_2\" \/>\u00a0= the total number of operands<\/li>\n<\/ul>\n<p>From these numbers, several measures can be calculated:<\/p>\n<ul>\n<li>Program vocabulary:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/7\/1\/7\/717a20087068c44611fd8b9ada50438b.png\" alt=\"\\eta = \\eta_1 + \\eta_2 \\,\" \/><\/li>\n<li>Program length:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/8\/6\/8\/8683c993ab3fbe1b95845b18e86cf62b.png\" alt=\"N = N_1 + N_2 \\,\" \/><\/li>\n<li>Calculated program length:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/4\/2\/d\/42d523745122a5b1477b79484e975e4e.png\" alt=\"\\hat{N} = \\eta_1 \\log_2 \\eta_1 + \\eta_2 \\log_2 \\eta_2 \" \/><\/li>\n<li>Volume:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/8\/2\/3\/8235c2b33a9d4bba70003662f9d77ea8.png\" alt=\"V = N \\times \\log_2 \\eta \" \/><\/li>\n<li>Difficulty\u00a0:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/b\/5\/b\/b5b32620ba5ffe4f55f7484286b01506.png\" alt=\"D = { \\eta_1 \\over 2  } \\times { N_2 \\over \\eta_2 } \" \/><\/li>\n<li>Effort:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/6\/0\/d\/60dcca126364bb26dc3a15100e941c77.png\" alt=\"E =  D \\times V \" \/><\/li>\n<\/ul>\n<p>The difficulty measure is related to the difficulty of the program to write or understand, e.g. when doing\u00a0<a style=\"color: #0b0080;\" title=\"Code review\" href=\"http:\/\/en.wikipedia.org\/wiki\/Code_review\">code review<\/a>.<\/p>\n<p>The effort measure translates into actual coding time using the following relation,<\/p>\n<ul>\n<li>Time required to program:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/a\/0\/6\/a060266aa8516994a662e9d4f1f20c48.png\" alt=\"T = {E \\over 18}\" \/>\u00a0seconds<\/li>\n<\/ul>\n<p>Halstead&#8217;s delivered bugs (B) is an estimate for the number of errors in the implementation.<\/p>\n<ul>\n<li>Number of delivered bugs\u00a0:\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/8\/e\/0\/8e0da3517f029b4474b6600bfeb23037.png\" alt=\"B = {E^{2 \\over 3} \\over 3000}\" \/>\u00a0or, more recently,\u00a0<img decoding=\"async\" class=\"mwe-math-fallback-image-inline tex\" src=\"http:\/\/upload.wikimedia.org\/math\/e\/f\/3\/ef3f63a10654f058e24ceae209e5c948.png\" alt=\"B = {V \\over 3000}\" \/>\u00a0is accepted<\/li>\n<\/ul>\n<p>The <a style=\"color: #7f3a21;\" href=\"http:\/\/en.wikipedia.org\/wiki\/Halstead_complexity_measures\">Halstead complexity measures<\/a>\u00a0provide insight into the readability of the code. These count the operators and operands to determine volume, difficulty, and effort. Often, these can indicate how difficult it will be for someone to understand the code.<\/p>\n<p>These metics could be used to improve the readability of the code base and it&#8217;s better to refactor when their values are more than the accepted ones to keep your code readable. If you have a\u00a0C\/C++ code base you can use <a href=\"http:\/\/www.cppdepend.com\">CppDepend <\/a>to calculate them.<\/p>\n<p><strong>Technical debt<\/strong><\/p>\n<p>Here&#8217;s a definition from this interesting <a href=\"http:\/\/www.infoq.com\/news\/2010\/03\/monetizing-technical-debt\">article<\/a>:<\/p>\n<blockquote><p>Just like a financial debt, the technical debt incurs interest payments. These are paid in the form of extra effort required to maintain and enhance the software which has either decayed or is built on a shaky foundation. Most Agilists recommend repaying the technical debt as early as possible. However, most Agile teams fail to monetize the technical debt, which can give valuable insights.<\/p>\n<ul style=\"color: #000000;\">\n<li><strong>Debt(in man days) =\u00a0<\/strong><em>{cost_to_fix_duplications + cost_to_fix_violations + cost_to_comment_public_API + cost_to_fix_uncovered_complexity + cost_to_bring_complexity_below_threshold + cost_to_cut_cycles_at_package_level}<\/em><\/li>\n<\/ul>\n<p style=\"color: #000000;\">There is a default cost in hour associated with each of the above violation. For example<\/p>\n<ul style=\"color: #000000;\">\n<li><strong>cost_to_fix_duplications =\u00a0<\/strong><em>{cost_to_fix_one_block * duplicated_block}<\/em><\/li>\n<\/ul>\n<p style=\"color: #000000;\">Now, as per the defaults\u00a0<strong>cost_to_fix_one_block = 2 hours<\/strong>. Assuming that the average developer cost is $500 per day and there are 8 hours to a day then to fix one such block $125 would be spent. Likewise,\u00a0<a style=\"color: #286ab2;\" href=\"http:\/\/docs.codehaus.org\/display\/SONAR\/Technical+Debt+Calculation\" target=\"_blank\">monetary analysis can be done for each violation<\/a>\u00a0to finally arrive at the total technical debt.<\/p>\n<\/blockquote>\n<p style=\"color: #000000;\"><span style=\"color: #333333;\">Having more technical debt means that it will become more difficult to continue to develop a system &#8211; you either need to cope with the technical debt and allocate more and more time for what would otherwise be simple tasks, or you need to invest resources (time and money) into reducing technical debt by refactoring the code, improving the tests, and so on. Using the Technical debt from the beginning <span class=\"b3\" style=\"color: #181818;\">\u00a0<\/span><span class=\"b4\" style=\"color: #0c0c0c;\">for\u00a0<\/span><span class=\"b5\" style=\"color: #000000;\">continuous\u00a0<\/span><span class=\"b4\" style=\"color: #0c0c0c;\">imp<\/span><span class=\"b3\" style=\"color: #181818;\">rov<\/span><span class=\"b2\" style=\"color: #242424;\">e<\/span><span class=\"b3\" style=\"color: #181818;\">men<\/span><span class=\"b4\" style=\"color: #0c0c0c;\">t \u00a0is a good idea to keep the code maintanable. For C\/C++ code base you can use the <a href=\"http:\/\/www.cppdepend.com\/sonarplugin\">C\/C++ Sonar Plugin<\/a> based on Clang to calculate the technical debt.<\/span><\/span><\/p>\n<p style=\"color: #000000;\"><strong>Summary<\/strong><\/p>\n<p style=\"color: #000000;\">Keep the code base readable and maintainable is not an easy task and using some metrics could help to facilitate this task and give a relevant indicators on where we have to improve our code. Many measures exist and you can choose the ones you consider relevant. But never\u00a0blindly move forward, \u00a0your code will be quickly a\u00a0labyrinthine system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The readability of source code has a direct impact on how well a developer comprehends a software system. Code maintainability refers to how easily that software system can be changed to add new features, modify existing features, fix bugs, or improve performance. Many coding techniques exist to improve the readability and the maintainability. However it&#8217;s &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1645\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Readability and Maintainability regulators using Halstead and Technical Debt measures.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1645","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1645"}],"version-history":[{"count":18,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1645\/revisions"}],"predecessor-version":[{"id":1719,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1645\/revisions\/1719"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}