<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Musings of the Kat: Comments vs Readable Code</title>
    <link>http://blog.klaws.org/articles/2009/12/29/comments-vs-readable-code</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>the intriguing Mr Crichton-Seager's blog</description>
    <item>
      <title>Comments vs Readable Code</title>
      <description>&lt;p&gt;There is a &lt;a href="http://www.cincomsmalltalk.com/userblogs/vbykov/blogView?searchCategory=Just%20Smalltalk"&gt;widely&lt;/a&gt; &lt;a href="http://c2.com/cgi/wiki?TreatCommentsWithSuspicion"&gt;contested&lt;/a&gt; mantra of Extreme Programming that says that comments are superfluous.  Inaccurate and often out-of-date meta-information just makes your self-describing code and accompanying tests harder to read and understand.&lt;/p&gt;


	&lt;p&gt;However the opposite can seem to be true; how often do we try to decipher unpleasant code that has no comments because the mantra said that they were &amp;#8220;evil&amp;#8221;?  It is dogma that leads to this problem.&lt;/p&gt;


	&lt;p&gt;It is important to remember that &amp;#8220;no comments&amp;#8221; is &lt;a href="http://en.wikipedia.org/wiki/Mantra"&gt;mantra&lt;/a&gt; not &lt;a href="http://en.wikipedia.org/wiki/Dogma"&gt;dogma&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Dogma is authoritative and not to be disputed, doubted or diverged from. That doesn&amp;#8217;t sound at all agile to me.  The word mantra originates in Sanskrit meaning &amp;#8220;tool of thinking&amp;#8221; or alternatively &amp;#8220;liberation of thought&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Dogma forces you to stick to practices that are &lt;strong&gt;believed&lt;/strong&gt; to be good for you.  Mantra can help you to re-evaluate your practices to find those that are &lt;strong&gt;actually&lt;/strong&gt; good for &lt;em&gt;you&lt;/em&gt;.&lt;/p&gt;


When you come to the point where you feel compelled to write a comment, think hard about whether or not it&amp;#8217;s really necessary;
	&lt;ul&gt;
	&lt;li&gt;Could I convey this comment by renaming a method or variable to something meaningful?&lt;/li&gt;
		&lt;li&gt;Could the accompanying unit test describe the code better?&lt;/li&gt;
	&lt;/ul&gt;


Can&amp;#8217;t think of a good name for a class or method?
	&lt;ul&gt;
	&lt;li&gt;Is the method or class trying to do too much (&lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;)?&lt;/li&gt;
		&lt;li&gt;Could I break down this object into two that are easier named?&lt;/li&gt;
	&lt;/ul&gt;


Okay, sometimes higher level architectural structures need to be documented, right?...
	&lt;ul&gt;
	&lt;li&gt;Maybe the class that manages the high-level architectural pattern could be better named?&lt;/li&gt;
		&lt;li&gt;Is a complex high-level architecture really giving you the required benefit if it is not easily understood?&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Personally I cannot get to the end of these options without recognising ways of improving my code, although sometimes I&amp;#8217;ve added comments to help me understand a large amount of ugly code, prior to refactoring.&lt;/p&gt;


	&lt;p&gt;The trick is to just keep thinking. &lt;em&gt;Process&lt;/em&gt; is &lt;strong&gt;never&lt;/strong&gt; an alternative to &lt;em&gt;thought&lt;/em&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 29 Dec 2009 16:51:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4d8d56e3-5199-49b2-8c2e-7734314a7953</guid>
      <author>Kat Crichton</author>
      <link>http://blog.klaws.org/articles/2009/12/29/comments-vs-readable-code</link>
      <category>Extreme Programming</category>
      <trackback:ping>http://blog.klaws.org/articles/trackback/104</trackback:ping>
    </item>
  </channel>
</rss>

