{"id":82033,"date":"2008-07-30T22:19:43","date_gmt":"2008-07-30T22:19:43","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73193"},"modified":"2019-05-22T10:09:38","modified_gmt":"2019-05-22T10:09:38","slug":"commenting-your-code","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/commenting-your-code\/","title":{"rendered":"Commenting your code"},"content":{"rendered":"<p>As I am easing back into real life from writing the book, I am in search of easy targets for blogging.\u00a0 My boss mentioned <a href=\"http:\/\/www.codinghorror.com\/blog\/archives\/001150.html\" target=\"_blank\" rel=\"noopener\">this blog<\/a> over on Jeff Atwood&#8217;s Coding Horror Blog and it got me thinking about commenting.\u00a0 His advice is to only comment &#8220;why&#8221; the code works.\u00a0 I can&#8217;t quite agree, because the code he claims to be acceptable is:<\/p>\n<p>private double SquareRootApproximation(n) { <br \/>\n\u00a0 r = n \/ 2; <br \/>\n\u00a0 while ( abs( r &#8211; (n\/r) ) &gt; t ) { <br \/>\n\u00a0\u00a0\u00a0 r = 0.5 * ( r + (n\/r) ); <br \/>\n\u00a0 } <br \/>\n\u00a0 return r; <br \/>\n} <br \/>\nSystem.out.println( &#8220;r = &#8221; + SquareRootApproximation(r) );<\/p>\n<p>I mean, it is better than some code I have seen,\u00a0 but still, I would like a bit more information about why this works.\u00a0 Maybe the name of the algorithm used, or at least what to do if this fails to provide the expected results.\u00a0 Admittedly this is probably something that could be easily found, but most algorithms are not.\u00a0 Comments in my mind should at least lead you to understand the mindset of the programmer.\u00a0 What would actually improve this code in my mind is to change the variables to full words (though in this case it might not make sense to do this.)<\/p>\n<p>On an extremely different side of things is <a href=\"http:\/\/www.mssqltips.com\/tip.asp?tip=1213\" target=\"_blank\" rel=\"noopener\">this article<\/a> from &#8220;<a href=\"http:\/\/www.mssqltips.com\/author.asp?authorid=11\">Edgewood Solutions Engineers<\/a>&#8221; on mssqltips.com. Their answer is to explain what the code is doing in simple terms, making sure to comment almost everything.\u00a0 They have a very elaborate header devised, with dependencies, both users of the object and objects it used.\u00a0 Most of what is said seems a bit like overkill, but their point here &#8220;Comment all of the major code blocks of the code and the critical minor points that can be easily overlooked such as a obscure WHERE clause.&#8221; is a good one.\u00a0 I generally pepper my code with comments where I think it will be hard to debug for myself later, with a consideration for others, particularly when those others will call me to explain the code.<\/p>\n<p>Which brings me to my commenting philosophy. I personally think you have to comment to the expected lowest common denominator.\u00a0 Think of the dumbest person who could have the need to read your code who is also qualified to have their job (otherwise you would have to write instructions on every line of code). If the qualified person can figure out what you are doing just by your naming conventions and , then it doesn&#8217;t need comments. But if that person would look at the code and reasonable figure it out, then there is no need to comment the code.\u00a0 What this requires is a few things:<\/p>\n<ul>\n<li><strong>Naming objects<\/strong> &#8211; if your procedures, tables, columns, functions all have meaningful names, you won&#8217;t have to explain what they mean, saving time<\/li>\n<li><strong>Good design<\/strong> &#8211; if the relationship between objects and the cardinality of those relationships is clear, then you don&#8217;t need to explain that what you are doing is hack due to poor thinking&#8230;<\/li>\n<li><strong>Naming variables<\/strong> &#8211; probably the most important thing to avoid the need for comments is naming stuff.\u00a0 Name variables with words, not single character values (except sometimes i, x, etc will suffice for obvious typical uses)<\/li>\n<li><strong>Reasonable code formatting<\/strong> &#8211; SQL has no real form, so you <em>could<\/em> write procedures on a single line.\u00a0 You could.\u00a0 You could smash your hand with a hammer too.\u00a0 Neither action would be very good.\u00a0 (Consider using Red-Gate&#8217;s SQL Refactor tool if nothing else.)<\/li>\n<\/ul>\n<p>However, the fact is, for SQL code, the real problem comes in when you start coming up with cool relational methods of solving problems that most moderately qualified people wouldn&#8217;t get. For example the trick of using a sequence table to break apart a comma delimited list. Couple that with a join and you get some amazingly cool code, but how do you comment it?<\/p>\n<p>For example, say an architect that shouldn&#8217;t be an architect designs a table with a comma delimited list like this (didn&#8217;t I mention good design earlier?\u00a0 I hate having to say this is a hack, but it is an elegant hack&#8230;)<\/p>\n<p><em>&#8211;excerpted from Chapter 7 of <\/em><a href=\"https:\/\/www.apress.com\/gp\/book\/9781430208662\" target=\"_blank\" rel=\"noopener\"><em>Pro SQL Server 2008 Relational Database Design and Implementation<\/em><\/a> <br \/>\nCREATE TABLE poorDesign <br \/>\n( <br \/>\n\u00a0\u00a0\u00a0\u00a0 poorDesignId int, <br \/>\n\u00a0\u00a0\u00a0\u00a0 badValue varchar(20) <br \/>\n)<\/p>\n<p>INSERT INTO poorDesign &#8211;using 2008 syntax <br \/>\nVALUES (1,&#8217;1,3,56,7,3,6&#8242;), <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (2,&#8217;22,3&#8242;), <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (3,&#8217;1&#8242;)<\/p>\n<p>You can &#8220;normalize&#8221; this set using a table of numbers (in my examples named tools.sequence) and a really cool join:<\/p>\n<p>SELECT\u00a0\u00a0\u00a0 poorDesign.poorDesignId as betterDesignId, <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SUBSTRING(&#8216;,&#8217; + poorDesign.badValue + &#8216;,&#8217;,i + 1, <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CHARINDEX(&#8216;,&#8217;,&#8217;,&#8217; + poorDesign.badValue + &#8216;,&#8217;,i + 1) &#8211; i &#8211; 1) <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 as betterScalarValue <br \/>\nFROM\u00a0\u00a0\u00a0\u00a0 poorDesign <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 JOIN tools.sequence <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 on i &gt;= 1 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AND i &lt; LEN(&#8216;,&#8217; + poorDesign.badValue + &#8216;,&#8217;) &#8211; 1 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AND SUBSTRING(&#8216;,&#8217; + + poorDesign.badValue + &#8216;,&#8217;, i, 1) = &#8216;,&#8217;<\/p>\n<p>But are there enough pixels available on the planet to make that more understandable to most SQL programmers? Even the reasonably qualified?\u00a0\u00a0 I mean, I am still kind of amazed at the technique and the fact that it returns the following:<\/p>\n<p>betterDesignId betterScalarValue <br \/>\n&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 56 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 22 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1<\/p>\n<p>still impresses me.\u00a0 Frankly I don&#8217;t know how to comment that code to make it readable.\u00a0 In a real situation I would settle for a comment before the SELECT that stated:<\/p>\n<p>&#8211;Uses a table of numbers to parse the comma delimited list into a SQL acceptable format. <br \/>\n&#8211;If you don&#8217;t understand this code, read this article: <a title=\"http:\/\/www.sommarskog.se\/arrays-in-sql-2005.html#tblnum\" href=\"http:\/\/www.sommarskog.se\/arrays-in-sql-2005.html#tblnum\">http:\/\/www.sommarskog.se\/arrays-in-sql-2005.html#tblnum<\/a><\/p>\n<p>Opinions? What do you use for a comments in your code?\u00a0 Do you have commenting policies?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I am easing back into real life from writing the book, I am in search of easy targets for blogging.\u00a0 My boss mentioned this blog over on Jeff Atwood&#8217;s Coding Horror Blog and it got me thinking about commenting.\u00a0 His advice is to only comment &#8220;why&#8221; the code works.\u00a0 I can&#8217;t quite agree, because&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82033","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82033"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82033\/revisions"}],"predecessor-version":[{"id":84363,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82033\/revisions\/84363"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82033"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}