{"id":7887,"date":"2016-06-13T09:06:15","date_gmt":"2016-06-13T08:06:15","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7887"},"modified":"2026-03-26T16:49:07","modified_gmt":"2026-03-26T20:49:07","slug":"comments-and-debug-code","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2016\/06\/13\/comments-and-debug-code\/","title":{"rendered":"Comments and debug code"},"content":{"rendered":"<p>I have frequently asserted that one of the most important attributes &#8211; maybe the most important attribute &#8211; of code is readability. This is because an enormous amount of time is spent on debugging and maintaining code &#8211; far more than that expended on writing it in the first place. So, keeping the future human reader [who might be you!] in mind at all times is essential.<\/p>\n<p>Interesting challenges occur when you think about hiding text\/code from the compiler &#8230;<!--more--><\/p>\n<p>Consider the general concept of concealing things from the compiler. It receives a stream of characters, which need to be translated into machine instructions, except where they are intended solely for human consumption. There are three broad contexts in which stuff is hidden from the compiler:<\/p>\n<ul>\n<li>documentation &#8211; comments in the code<\/li>\n<li>temporary code removal &#8211; an inevitable part of the debugging process<\/li>\n<li>debug\/trace code &#8211; which can be switched on and off, as required<\/li>\n<\/ul>\n<p>Taking these in turn &#8230;<\/p>\n<h3>Documentation<\/h3>\n<p>Everyone knows that comments are a good idea, but we are all lazy. However, the effort is well worthwhile. Old style <strong>\/* \u2026 *\/<\/strong> C comments are OK, but I feel the C++ end-of-line [ <strong>\/\/ \u2026<\/strong> ] variety tend be be clear. They still need to be used with care. For example, code like this:<\/p>\n<pre>int number; \/\/ input count\nchar c; \/\/ single character buffer\nchar buffer[99]; \/\/ the input line\n<br>\n<\/pre>\n<p>is so hard to follow. Alignment is everything:<\/p>\n<pre>int number;      \/\/ input count\nchar c;          \/\/ single character buffer\nchar buffer[99]; \/\/ the input line\n<br>\n<\/pre>\n<p>And do not use tabs. They are not portable.<\/p>\n<h3>Temporary Code Removal<\/h3>\n<p>In the process of debugging and testing code, it is commonly useful to be able to \u201cswitch off\u201d a chunk of code temporarily. Many programmers \u201ccomment out\u201d code to achieve this result by putting <strong>\/*<\/strong> at the beginning and <strong>*\/<\/strong> at the end. That is quick and dirty, but frequently fails to achieve the required result. Many compilers do not support nested comments, so, if the code was commented, problems arise. Overall, it is error prone and should be avoided.<\/p>\n<p>The use of C++ style <strong>\/\/<\/strong> comment notation &#8211; i.e. putting <strong>\/\/<\/strong> at the beginning of each line is marginally better, but very tedious to apply and may also be error prone in removal.<\/p>\n<p>The best way to achieve this result is by use of pre-processor directives, thus:<\/p>\n<pre>#if 0\n   &lt;code to be hidden&gt;\n#endif\n<br>\n<\/pre>\n<p>In any case, \u201cturned off\u201d code should not be included in any code which is being considered for release.<\/p>\n<h3>Debug\/Trace Code<\/h3>\n<p>Another kind of temporary code is that included to facilitate the output of extra information when debugging. Although modern debuggers and such tools can be very effective, instrumenting the code can still be the best way to figure out exactly what is going on. Modern <a href=\"https:\/\/www.mentor.com\/embedded-software\/sourcery-tools\/sourcery-codebench\/overview\" target=\"_blank\" rel=\"noopener noreferrer\">development tools<\/a> are so fast that rebuilding to create a debug-enabled image is not a serious overhead.<\/p>\n<p>A common way to facilitate this is using pre-processor directives, thus:<\/p>\n<pre>#ifdef DEBUG_TRACE\n   &lt;debug\/trace code&gt;\n#endif\n<br>\n<\/pre>\n<p>So, when the symbol <strong>DEBUG_TRACE<\/strong> is defined, the debug code is included.<\/p>\n<p>A slightly different approach is to code it like this:<\/p>\n<pre>#ifndef NDEBUG\n   &lt;debug\/trace code&gt;\n#endif\n<br>\n<\/pre>\n<p>This double negative seems clumsy, but this symbol is used to control the standard <strong>assert()<\/strong> macro. The programmer needs to define the symbol to suppress debugging mode. I acknowledge Michael Barr, who raised my awareness of this approach in \u201cEmbedded C Coding Standard\u201d.<\/p>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/plus.google.com\/116301748426290440139\/posts?hl=en%3Fhl=en\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6587\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/google.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/www.slideshare.net\/ColinWalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6595\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/slideshare.jpg\" alt=\"\" width=\"41\" height=\"41\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have frequently asserted that one of the most important attributes &#8211; maybe the most important attribute &#8211; of code&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,424,339,300,640,641,642,340,502,478],"industry":[],"product":[],"coauthors":[],"class_list":["post-7887","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-debugging","tag-development-tools","tag-embedded-software","tag-embedded-software-analysis","tag-embedded-software-debug","tag-linux-software-debug","tag-programming-languages","tag-sourcery-analyzer","tag-sourcery-codebench"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=7887"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7887\/revisions"}],"predecessor-version":[{"id":10482,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7887\/revisions\/10482"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7887"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7887"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7887"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}