{"id":863,"date":"2014-10-03T14:48:51","date_gmt":"2014-10-03T14:48:51","guid":{"rendered":"http:\/\/www.visionriders.com\/blog\/?p=863"},"modified":"2014-10-03T14:48:51","modified_gmt":"2014-10-03T14:48:51","slug":"comment-everything","status":"publish","type":"post","link":"https:\/\/www.visionriders.com\/blog\/2014\/10\/comment-everything\/","title":{"rendered":"Comment Everything"},"content":{"rendered":"<p>There&#8217;s a lot of programmers out there that are big fans of &#8220;self-documenting code&#8221;. That is, code that is easily understandable without comment lines explaining it. <a href=\"http:\/\/www.cleancoder.com\">Robert Martin<\/a>, in his book <cite>Clean Code: A Handbook of Agile Software Craftsmanship<\/cite>, even goes so far as to say:<\/p>\n<blockquote><p>The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. <em>Comments are always failures.<\/em> We must have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration.<\/p><\/blockquote>\n<div class=\"artwork_note\">(Emphasis mine.)<\/div>\n<p>The philosophy here is &#8220;your code should speak for itself&#8221;. After all, a programming language is a <em>language<\/em>, right? We don&#8217;t need asides just to get through a block of pure English text, do we?<\/p>\n<p>The thing is, programming languages are not one-to-one equivalents to a human tongue. For that reason I follow a different philosophy, as you can probably tell from this sample of some C++ code I wrote:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.visionriders.com\/blog\/images\/2014\/code_01.png\" class=\"artwork\" alt=\"Well-commented C++ code.\"><\/p>\n<p>Some of you out there who are programmers are probably cringing right now looking at that. Take, for example, the <code>c = file->peek()<\/code> line at the bottom. Anyone with experience in C++ would recognize at once what I&#8217;m doing. Really, anyone familiar with file input\/output in general should understand what I&#8217;m doing just from the &#8220;peek&#8221;. I&#8217;m looking ahead one character in the file, without advancing the file reader, and storing the value in the character variable named &#8220;c&#8221;.<\/p>\n<p>Yet despite that, the line is proceeded by a comment: &#8220;Peek at the next value.&#8221; It describes the obvious. Some would say it&#8217;s useless and redundant. But I purposefully took the time to type it out there anyway. As you can probably see, I&#8217;ve even done it more than once.<\/p>\n<p>I strongly believe code should be well-formatted, organized, and easy-to-read, but also extensively commented. Why? There&#8217;s several reasons.<\/p>\n<ol>\n<li>It keeps me focused. I type out what I&#8217;m trying to do right before I go about it, so I have a good idea how I need to format the code to best accomplish whatever task is at hand. When you look at my comments, you&#8217;re not just seeing what I&#8217;m doing, you&#8217;re seeing the entire thought process behind it.<\/li>\n<li>It helps me keep my code organized. I can easily skim through the comments to find the chunk of logic I&#8217;m looking for, without having to dig too deep into the code itself.<\/li>\n<li>It makes it easier for me to find mistakes. Occasionally I can spot a bug by just running through the comments and seeing the thought process doesn&#8217;t line up. I&#8217;m missing a step! Other times, I&#8217;ll actually find bugs by seeing that a comment doesn&#8217;t match the code that follows it. My implementation is wrong because I goofed and wasn&#8217;t following the logic that I&#8217;d stepped through in the comments.<\/li>\n<li>Building on point 1, it reminds me what I was thinking when I wrote the code. A lot of times I&#8217;ll wonder, &#8220;why did I write it this way?&#8221; Then I&#8217;ll go back over the comments and remember. It&#8217;s saved me rewrites before, especially when I&#8217;ve forgotten that I <em>already<\/em> tried what I might think is the &#8220;better&#8221; way and realized it won&#8217;t do what I need it to.<\/li>\n<\/ol>\n<p>According to <a href=\"http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.html\">Google&#8217;s C++ coding style standards<\/a>, when writing comments you should &#8220;assume that the person reading the code knows [programming] better than you do.&#8221; I for one don&#8217;t. I assume that the person reading my comments is an idiot. Because I will be reading them later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a lot of programmers out there that are big fans of &#8220;self-documenting code&#8221;. That is, code that is easily understandable without comment lines explaining it. Robert Martin, in his book Clean Code: A Handbook of Agile Software Craftsmanship, even goes so far as to say: The proper use of comments is to compensate for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-863","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/posts\/863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":11,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":874,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions\/874"}],"wp:attachment":[{"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.visionriders.com\/blog\/wp-json\/wp\/v2\/tags?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}