<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx</link><description>Nothing revolutionary about that statement.&amp;#160; Yet I keep reading the opposite on various comment threads and message boards so I thought it a good idea to explore it again. Code is not self documenting. The &amp;quot;code is self document&amp;quot; argument</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>I agree, but then reality strikes.</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8585464</link><pubDate>Mon, 09 Jun 2008 15:43:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8585464</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I agree that comments need to be included to the extent that they help make the code more clear.&lt;/p&gt;
&lt;p&gt;However, two points need to be made in defense of &amp;quot;the code is the comment&amp;quot; mantra.&lt;/p&gt;
&lt;p&gt;First, before we had long variable names, comments became manditory only because the code couldn't be clear. &amp;nbsp;Many managers STILL believe that ALL code should be commented, &amp;nbsp;even if all you are saying is, &amp;quot;this line increments X by 1&amp;quot; (which hopefully is clear from the code... and hopefully, they didn't use X as a variable name). &amp;nbsp;So, if a manager is asking for the comments, I'm a lot more likely to argue that the code IS the comment. &amp;nbsp;If another programmer is asking for a comment, I'd first ask, &amp;quot;what exactly isn't clear about the code?&amp;quot; That would allow me to address that specific issue either with my code, so the code becomes the comment again, or by adding a comment.&lt;/p&gt;
&lt;p&gt;Second, in the 20+ years I've been programming, I have NEVER worked on a program where the comments describe what the code currently does. &amp;nbsp;So, even when I was working on programs with 10 character variable names, I always had to rely on the code as the ultimate authority on what it was doing. &amp;nbsp;In fact, I currently ignore comments in the code unless I just can't figure out what the code is doing, and even when I do look, I only use it as a historical annotation (where the code came from, not necessarily what it is doing now.)&lt;/p&gt;
&lt;p&gt;The fact of the matter is, comments are difficult to keep in sync with the code. &amp;nbsp;While code will always stay in sync with itself. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Instead of arguing for or against comments, what we SHOULD be arguing for is code that can be maintained. &amp;nbsp;If that means comments are required, so be it.&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8586279</link><pubDate>Mon, 09 Jun 2008 19:04:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8586279</guid><dc:creator>~AVA</dc:creator><description>&lt;p&gt;You did not provide any example of comments that 1) explain the algorithm and 2) explain the greater purpose of the algorithm in the program, so I can only guess what you mean.&lt;/p&gt;
&lt;p&gt;From my experience, comments like:&lt;/p&gt;
&lt;p&gt;// Initialize the list of students&lt;/p&gt;
&lt;p&gt;bool Init( HWND ctl);&lt;/p&gt;
&lt;p&gt;// Turn the lamp on&lt;/p&gt;
&lt;p&gt;on = TRUE;&lt;/p&gt;
&lt;p&gt;// engine states&lt;/p&gt;
&lt;p&gt;enum State { off, warming, working }; &lt;/p&gt;
&lt;p&gt;can be transformed to names like:&lt;/p&gt;
&lt;p&gt;bool InitializeTheListOfStudents(HWND listControl);&lt;/p&gt;
&lt;p&gt;TurnLampOn();&lt;/p&gt;
&lt;p&gt;enum EngineState { engineIsOn, engineIsWarming, engineIsWorking };&lt;/p&gt;
&lt;p&gt;Comments explaining the interface of some class or function indicate the weakness in the design: if the purpose of a function (&amp;quot;server&amp;quot;) is not expressed by the name, you have to repeat the comment with an explanation in each place where the function is called (&amp;quot;client code&amp;quot;). &lt;/p&gt;
&lt;p&gt;From other hand, the comments explaining the implementation of some algorithm are OK:&lt;/p&gt;
&lt;p&gt;// collect responses&lt;/p&gt;
&lt;p&gt;while ( total_descriptors_in_the_set )&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	FD_ZERO( &amp;amp;socket_set );&lt;/p&gt;
&lt;p&gt;	int number_of_open_sockets = ::snmp_select_info(&lt;/p&gt;
&lt;p&gt;		&amp;amp;total_descriptors_in_the_set, &lt;/p&gt;
&lt;p&gt;		&amp;amp;socket_set, &lt;/p&gt;
&lt;p&gt;		&amp;amp;timeout, &lt;/p&gt;
&lt;p&gt;		&amp;amp;need_block&lt;/p&gt;
&lt;p&gt;	);&lt;/p&gt;
&lt;p&gt;	// ...&lt;/p&gt;
&lt;p&gt;Typically calls of OS API, 3rd-part libraries, or your published functions, whose names cannot be changed, require more attention (and comments) than your private functions. You have full control over names of your private functions, so you can make them self-explanatory.&lt;/p&gt;
&lt;p&gt;Comments are also good for cross-references pointing to other documents, like:&lt;/p&gt;
&lt;p&gt;// Adapted from Big Book of C++ Tricks, p.274&lt;/p&gt;
&lt;p&gt;MakeJuice( apple ); // &amp;lt;SomeHeader.h&amp;gt;&lt;/p&gt;
&lt;p&gt;// See &amp;quot;Project Code Conventions.pdf&amp;quot; for details&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8587617</link><pubDate>Tue, 10 Jun 2008 01:03:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8587617</guid><dc:creator>DevTopics</dc:creator><description>&lt;p&gt;A programmer can write code in such a way that it is &amp;quot;almost&amp;quot; self-documenting. &amp;nbsp;In other words, by using descriptive local variables, by breaking complex expressions into multiple simpler statements, etc. &amp;nbsp;But even though the code itself can convey the &amp;quot;what&amp;quot; it's trying to do, rarely can it explain the &amp;quot;why&amp;quot;. &amp;nbsp;This of course requires explicit comments.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.devtopics.com/13-tips-to-comment-your-code/"&gt;http://www.devtopics.com/13-tips-to-comment-your-code/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8587769</link><pubDate>Tue, 10 Jun 2008 02:04:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8587769</guid><dc:creator>dclayton</dc:creator><description>&lt;p&gt;Comments should only exist to point out what is not obvious in the code. &amp;nbsp;In any programming assignment, an assumption must be made as to the base competence of whomever will be reading the comments subsequently. &amp;nbsp;Only document that which cannot be assumed to be clear to a subsequent developer of moderate skills.&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8587782</link><pubDate>Tue, 10 Jun 2008 02:12:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8587782</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@~AVA,&lt;/p&gt;
&lt;p&gt;True I didn't give specific algortihmn's which I documented one way or another. &amp;nbsp;I thought the post was long enough without adding explanations of algortihmns that readers wouldn't have context into :) &lt;/p&gt;
&lt;p&gt;You can use the names of functions to make them explanatatory but only to a degree. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think DevTopics hit the nail on the head though. &amp;nbsp;If you want them to be truly self documenting you have to include the &amp;quot;why&amp;quot; in addition to the &amp;quot;what&amp;quot;.&lt;/p&gt;
&lt;p&gt;Taking threading for example. &amp;nbsp;Extremely clean code can potentially identiy the &amp;quot;what&amp;quot; part for a threading question but it's very unlikely to include the &amp;quot;why.&amp;quot; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;1) Am I in the background for speed?&lt;/p&gt;
&lt;p&gt;2) Am I avoiding blocking the UI&lt;/p&gt;
&lt;p&gt;3) Do I need to switch my COM apartment&lt;/p&gt;
&lt;p&gt;4) Am I accessing a thread affinitized object?&lt;/p&gt;
&lt;p&gt;5) All of the above.&lt;/p&gt;
&lt;p&gt;You can cram that into a function name but it's not going to be very easy on the eyes.&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8588847</link><pubDate>Tue, 10 Jun 2008 08:44:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8588847</guid><dc:creator>Timmy Jose</dc:creator><description>&lt;p&gt;Refreshing read! Most well-meaning folks (who are no push-overs when it comes to coding) who generate hundreds of thousands of lines of code for an enterprise product do not even have the freakin' patience/ inclination/ common sense to even have a pretense of a documentation. And the code is anything but self-documenting! I have a gala time reverse engineering the &amp;quot;code&amp;quot; to understand what it does and where it is actually used! And I am talking about a Fortune 10 company! &lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8588849</link><pubDate>Tue, 10 Jun 2008 08:44:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8588849</guid><dc:creator>Torsten Curdt</dc:creator><description>&lt;p&gt;I think no one really suggests to write no comments or documentation when following this mantra. Self documenting more means to express the flow of the code properly. Of course you still need documentation on the class, the module and the conceptional model of the application. But you should not clutter your code.&lt;/p&gt;
&lt;p&gt;See &lt;a rel="nofollow" target="_new" href="http://vafer.org/blog/20050323095453"&gt;http://vafer.org/blog/20050323095453&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8588853</link><pubDate>Tue, 10 Jun 2008 08:45:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8588853</guid><dc:creator>Torsten Curdt</dc:creator><description>&lt;p&gt;I think no one really suggests to write no comments or documentation when following this mantra. Self documenting more means to express the flow of the code properly. Of course you still need documentation on the class, the module and the conceptional model of the application. But you should not clutter your code.&lt;/p&gt;
&lt;p&gt;See &lt;a rel="nofollow" target="_new" href="http://vafer.org/blog/20050323095453"&gt;http://vafer.org/blog/20050323095453&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8588858</link><pubDate>Tue, 10 Jun 2008 08:46:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8588858</guid><dc:creator>Timmy Jose</dc:creator><description>&lt;p&gt;Refreshing read! Most well-meaning folks (who are no push-overs when it comes to coding) who generate hundreds of thousands of lines of code for an enterprise product do not even have the freakin' patience/ inclination/ common sense to even have a pretense of a documentation. And the code is anything but self-documenting! I have a gala time reverse engineering the &amp;quot;code&amp;quot; to understand what it does and where it is actually used! And I am talking about a Fortune 10 company! &lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8590101</link><pubDate>Tue, 10 Jun 2008 19:01:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590101</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@Tortsen,&lt;/p&gt;
&lt;p&gt;Unfortunately I've had that very argument made to me on several occasions and the most recent of which inspired me to write this post. &amp;nbsp;I don't think this is the majority case though. &lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8590222</link><pubDate>Tue, 10 Jun 2008 20:37:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590222</guid><dc:creator>giku</dc:creator><description>&lt;p&gt;This problem is analogue... So both : black and white are wrong, all depends on Everything&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8590256</link><pubDate>Tue, 10 Jun 2008 21:08:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590256</guid><dc:creator>luweewu</dc:creator><description>&lt;p&gt;Good code is self-documenting.&lt;/p&gt;
&lt;p&gt;No amount of commenting will help reading bad code.&lt;/p&gt;
</description></item><item><title>//code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8590323</link><pubDate>Tue, 10 Jun 2008 22:33:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590323</guid><dc:creator>ncloud</dc:creator><description>&lt;p&gt;I try to name my variables appropriately so they make sense in their given context. &amp;nbsp;I find that for most obvious operations, this is sufficient. &amp;nbsp;However, there are cases where just knowing &amp;quot;what&amp;quot; is happening is not enough -- the developer needs to know &amp;quot;why&amp;quot; it is happening. &amp;nbsp;Just today I was reviewing code that I wrote nine months ago, and while the code was completely legible and all the variables had obvious names, I couldn't figure out *why* I had written a specific branching statement, and I had not bothered to comment it at the time, so now I'm stuck.&lt;/p&gt;
&lt;p&gt;Another good reason to comment your code is to justify changes that were made at given points in time. &amp;nbsp;On two separate occasions, my manager has asked me why certain behavior was being manifest in our application, and I was able to pull up the code and note that, in both cases, he had made the decision to implement that specific behavior (I also included the exact date in the comments to be more specific).&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8590355</link><pubDate>Tue, 10 Jun 2008 23:09:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590355</guid><dc:creator>Dhananjay Nene</dc:creator><description>&lt;p&gt;The statement is taken too literally and imho misinterpreted a little extremely. Self documenting code can be made better by annotating it with comments (think of it as inserting little notes inside your MS-Word document). Strip the comments and if the code is still readable and roughly understandable I would say it is self documenting. Throw in a few comments and you just improved its readability - you didn't suddenly end up documenting the code.&lt;/p&gt;
&lt;p&gt;Another interpretation is that along with all the annotations if, the resultant generated documentation (eg. javadocs) can help you get a hang of the design and usage without having to consult any other documents, that would also be self documenting in a different sense (ie. all the documentation requirements are serviced by the code files themselves)&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8591197</link><pubDate>Wed, 11 Jun 2008 14:47:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8591197</guid><dc:creator>M.C.</dc:creator><description>&lt;p&gt;erm... I think the argument is that code should be written to be as self-documenting as possible, not that code is self-documenting. &lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8591689</link><pubDate>Wed, 11 Jun 2008 19:53:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8591689</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@M.C.&lt;/p&gt;
&lt;p&gt;I'm a huge fan of clear code and enforce that principal in my code reviews. &amp;nbsp;Just because code is not self documenting doesn't mean it can't be readable and easily understood. &amp;nbsp;Code should be both readable and documented. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Code is not self documenting</title><link>http://blogs.msdn.com/jaredpar/archive/2008/06/09/code-is-not-self-documenting.aspx#8593295</link><pubDate>Thu, 12 Jun 2008 18:43:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8593295</guid><dc:creator>I Hate IT</dc:creator><description>&lt;p&gt;Code is &amp;quot;self documenting&amp;quot; in at least one respect.&lt;/p&gt;
&lt;p&gt;If you replace a human driven task (say compilation, and deployment) with a programmed script. The script is typically more &amp;quot;self documented&amp;quot; than the human process (baring well established SOPs, followed precisely).&lt;/p&gt;
</description></item></channel></rss>