<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : comments</title><link>http://msmvps.com/blogs/joacim/archive/tags/comments/default.aspx</link><description>Tags: comments</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>To comment or not to comment</title><link>http://msmvps.com/blogs/joacim/archive/2009/09/18/to-comment-or-not-to-comment.aspx</link><pubDate>Fri, 18 Sep 2009 15:52:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1724762</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1724762</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/09/18/to-comment-or-not-to-comment.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Anyone that has ever taken a programming class or read a beginners book on the subject know how important it is to write comments in your code. But is that really true? In this post I’m going to discuss/question the common practice of writing comments in your code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Bad comments&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A couple of weeks ago I was contacted by an old friend of mine that runs a construction company. They have an old custom system that they’ve had since the mid or late &amp;#39;90s sometime. He wanted some slight modifications made to this system, but the consultant business that created it apparently disappeared sometime during the dot-com death era. So he wondered if I could have a look at it and come up with an estimation of how much work it would require to do these customizations he needed.&lt;/p&gt;  &lt;p&gt;Since he had all the source code - and because I’m such a nice guy – I asked him to e-mail the source over to me, which he did. When I opened up the old project (which was written in Visual C++ 6.0) the first thing I saw in the main file was this (the name of the developer and company have been changed to protect the guilty).&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:green;"&gt;/***********************************************************************************
*
* File name   : CGMain.cpp
* Developed by: John Doe
* Company     : Former Dot Com Inc
* E-mail      : john.doe@nowheretobefound.com
* Date        : 1997-03-21
* Requirements: None
*
*-----------------------------------------------------------------------------------
* Version history
*-----------------------------------------------------------------------------------
*
* 1.0.0 Initial release
*       1997-03-21
* 1.0.1 Minor bug fixes
*       1997-03-24
* 1.0.2 Added 2 functions: AccViewF() and AccViewT()
*       Reason: The customer needed to store the AccView creator and delivery
*       1997-04-03
* 1.0.3 Minor bug fix
*       1997-04-05
* 1.0.3 .... This goes on and on ...
*  ...
*  ...
* 1.2.7 Minor fix
*       1998-04-02
*
***********************************************************************************/&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I can assure you that the list was lot longer than what I posted here. What is this good for? Why does a file need a comment header that tells me the name of the file? I already know which file I opened. Do I really care who wrote it? I don’t know that person and the contact information isn’t correct anyway. Could somebody explain to me why you would need a version history typed into the file? Ever heard of source control systems? Those are made to keep track of different versions of your file. The above comment doesn’t bring any useful information my way, or to my friend who isn’t a developer and because of that never even looked at the source code. All it did was made me scroll down a page or two before I got to the actual code.&lt;/p&gt;

&lt;p&gt;I can understand that a company that sells class libraries and components with accompanied source code might want to add a (hopefully short) copyright notice on top of each file, but other than that there is really no reason why you want to have a commented header in your file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to add comments and when not to&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In .Net there is one type of comments I always recommend people to use and that is XML comments. Those are great when they are added to methods and properties of a class library. A library, I might add, that you probably never open up the source code for, at least not if it comes from a third party. The XML comments aren’t meant to be read by humans, they are read by the compiler and shows valuable (hopefully) information in IntelliSense tooltips. Other helpful comments are JavaDoc and PyDoc comments used respectively in Java and Python to create documentation.&lt;/p&gt;

&lt;p&gt;I remember once reading a blog post by Jeff Atwood about this very subject, please hold on while I Google it for you……. ah, &lt;a href="http://www.codinghorror.com/blog/archives/001150.html" target="_blank"&gt;here it is&lt;/a&gt;. Jeff also wrote a long rant about header comments, much like the one above. However in his post he, more or less, said that you shouldn’t use comments at all, which I don’t fully agree with him on. But he has a very important point which I think is also an excellent advice, always aim to write, or refactor, your code so it doesn’t need any comments. With that I (and I think also Jeff, even though I can’t really speak for him) mean that you should always give your methods a very descriptive name, keep them short and to the point.&lt;/p&gt;

&lt;p&gt;But unlike Jeff I still think it can be valuable to sometimes stick in a short comment that explains what the code is doing. However you must remember that the comment is then as important as the source code. If you refactor or change the source it is important that you also update the comment. Nothing is as useless as a comment that isn’t valid anymore.&lt;/p&gt;

&lt;p&gt;A while back I saw the following comment in the code behind file of an ASP.Net page.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:green;"&gt;&amp;#39;TODO: The following code is very resource-intensive and
&amp;#39;      should be refactored.&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The page that contained this comment had been in the web application in question for years. I looked at the code and couldn’t figure out what it was that was so resource-intensive. I stepped through it, and analyzed it very carefully without finding anything special. So somebody must at some point have done what the comment said, but without removing the comment, which made me waste my time to analyze perfectly working code for no reason at all. Here’s another comment from the same project.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:green;"&gt;&amp;#39;This is probably not a good idea, 
&amp;#39;and should be changed but I’ll leave it for now.&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Excuse me while I scream and swear… &lt;font color="#808080"&gt;&lt;em&gt;(this text has been censored). &lt;/em&gt;&lt;/font&gt;What is it that isn’t such a good idea? How am I to know how the original developer was thinking when he wrote the above comment? Did he have a brilliant idea how the code could be refactored but was to lazy to do it now? Has he refactored the code but yet again forgot to remove the comment?&lt;/p&gt;

&lt;p&gt;The point here is to never write comments that only make sense to yourself. &lt;em&gt;Code is written once and read many times&lt;/em&gt;. Much of the code you write today are legacy tomorrow when you aren’t around anymore and some other poor sob have to update it. Always edit or remove comments when you edit the source.&lt;/p&gt;

&lt;p&gt;At this point it probably sounds as if I think you shouldn’t write any comments at all with the exception of XML comments, but that is not the case. As &lt;a href="http://www.joelonsoftware.com" target="_blank"&gt;Joel Spolsky&lt;/a&gt; have said: &lt;em&gt;Code is easier to write than to read.&lt;/em&gt; That is so true. How often haven’t you looked back at some code you wrote just a couple of months ago and wondered: &lt;em&gt;How was I thinking here…&lt;/em&gt; Not that you weren&amp;#39;t thinking when you wrote it, the code is probably just fine, but at the moment you can’t remember the flow of the source. When you develop you are in the middle of it and probably know exactly why you should call that particular method from this particular place, but a few weeks later (after you’ve been working on something very different) when you revisit your older code you just can’t remember that flow. So it takes a while before you are up to speed with it again. Now, what if that isn’t you that revisit your old code? Somebody else that thinks in a different manner than you will have a much harder time to get the flow before he/she can be productive with your source. This is the main reason why developers want to work on new projects all the time. We usually don’t like to work with old legacy code, especially old code we haven’t written ourselves.&lt;/p&gt;

&lt;p&gt;In those cases a few well placed comments might help a lot. Just keep remembering that you need to refactor the comments just as much as you need to refactor the source code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End words&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are one type of comments I do enjoy to read, and that is your comments on my blog posts. :)&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1724762" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/rant/default.aspx">rant</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/refactoring/default.aspx">refactoring</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/comments/default.aspx">comments</category></item></channel></rss>