<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>So Jake Says &#187; Commenting code</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/commenting-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Norvig, Atwood, and Comments</title>
		<link>http://www.jakevoytko.com/blog/2008/08/04/norvig-atwood-and-comments/</link>
		<comments>http://www.jakevoytko.com/blog/2008/08/04/norvig-atwood-and-comments/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 04:00:44 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[be helpful]]></category>
		<category><![CDATA[Comment the unobvious]]></category>
		<category><![CDATA[Commenting code]]></category>
		<category><![CDATA[Jeff Atwood]]></category>
		<category><![CDATA[Newton's Method]]></category>
		<category><![CDATA[Norvig]]></category>
		<category><![CDATA[Steve McConnell]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=109</guid>
		<description><![CDATA[Steve McConnell on Comments Jeff Atwood recently wrote an article on comments in source code. He discusses a few different approaches to commenting a Newton&#8217;s method square root function, and describes the pros and cons. His reasoning was (unsurprisingly) influenced by Steve McConnell. Good code is its own best documentation. As you&#8217;re about to add [...]]]></description>
			<content:encoded><![CDATA[<h2>Steve McConnell on Comments</h2>
<p>Jeff Atwood recently <a href="http://www.codinghorror.com/blog/archives/001150.html">wrote an article</a> on comments in source code. He discusses a few different approaches to commenting a <a href="http://en.wikipedia.org/wiki/Newton%27s_method">Newton&#8217;s method</a> square root function, and describes the pros and cons. His reasoning was (<a href="http://www.codinghorror.com/blog/archives/001020.html">unsurprisingly</a>) influenced by Steve McConnell.</p>
<blockquote><p>Good code is its own best documentation. As you&#8217;re about to add a comment, ask yourself, &#8216;How can I improve the code so that this comment isn&#8217;t needed?&#8217; Improve the code and then document it to make it even clearer.</p>
<p>~ Steve McConnell, <span style="text-decoration: underline;">Code Complete</span></p></blockquote>
<p>Atwood goes through a few design iterations of a snippet of code and arrives at the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> SquareRootApproximation<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  r <span style="color: #339933;">=</span> n <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> abs<span style="color: #009900;">&#40;</span> r <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">/</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> t <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    r <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span> r <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">/</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This has been written about extensively by various bloggers, but nobody cares about what bloggers think. It logically follows that I don&#8217;t care about what I think.</p>
<p>I couldn&#8217;t indifferently agree more.</p>
<p>Rather than take a stab at improving the documentation of this particular piece of code myself,<strong> I wanted to improve it using someone else&#8217;s method</strong>. To do this, I engaged in an old hobby of mine: I researched the practices of people who are hell of a lot more successful than I am.</p>
<p>Knuth&#8217;s <a href="http://en.wikipedia.org/wiki/Literate_programming">Literate Programming</a> has always fascinated me, and I might someday use it to write a large program. However, it won&#8217;t be soon, and I sure won&#8217;t be using it when working with others. I also feel that its effect would be lost on an example this small, so I decided to pass on this.</p>
<p>In lieu of this, I found that Peter Norvig has written the best practical advice on comments.</p>
<h2>Enter Norvig</h2>
<p>Dr. Peter Norvig [<a href="http://norvig.com/bio.html">bio</a>] wrote a <a href="http://norvig.com/luv-slides.ps">Tutorial on Good Lisp Programming Style</a> [.ps alert]. This particular work is worth the read from beginning to end, but of particular use are his few commenting conventions. Some are Lisp-specific, but there are a few good lessons for the taking.</p>
<p><strong>Be Helpful</strong></p>
<blockquote><p>&#8220;Documentation should be organized around tasks the user needs to do, not around what your program happens to provide. Adding documentation strings to each function doesn&#8217;t tell the reader how to use your program, but hints in the right place can be very effective.&#8221;</p></blockquote>
<p>This has been very helpful for me in the week since I first read it. He also cites good examples from the Emacs documentation:</p>
<blockquote><p>&#8220;next-line: Move cursor vertically down ARG lines.</p>
<p>&#8230; If you are thinking of using this in a Lisp program, consider using &#8216;forward-line&#8217; instead. It is usually easier to use and more reliable (no dependence on goal-column, etc.)&#8221;</p></blockquote>
<p><strong>Declare the unobvious</strong></p>
<blockquote><p>&#8220;If you&#8217;re thinking of something useful that others might want to know when they read your code and that might not be instantly apparent to them, make it a comment.&#8221;</p></blockquote>
<p>This is related to the above. Anything that a person might need when using your code should be mentioned. If you had to derive your code, the method should be described (even if briefly) so that a maintainer may double-check your work.</p>
<p><strong>Say what you mean</strong></p>
<p>Speak in declarations and assertions. Use appropriate detail. Be explicit. Not much to say here.</p>
<h2>Applying it to the Code Sample</h2>
<p>There are two different ways that I would comment this code. If the interface and the implementation were separated such as in C++, I would add different comments for the header file and the source file:</p>
<p><strong>Header</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/* Approximates sqrt(n) to within the specified error.
 * Convergence is quadratic.*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">double</span> SquareRootApproximation<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><strong>Source</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Approximates sqrt(n) using Newton's method.</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">double</span> SomeClass<span style="color: #008080;">::</span><span style="color: #007788;">SquareRootApproximation</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  r <span style="color: #000080;">=</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">abs</span><span style="color: #008000;">&#40;</span> r <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span>r<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> t <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    r <span style="color: #000080;">=</span> <span style="color:#800080;">0.5</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span> r <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span>r<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We could also be using a language where the interface and the code are the same. I would comment like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Approximates sqrt(n) using Newton's Method
 *      to within the specified error.
 *
 * Convergence is quadratic.*/</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> SquareRootApproximation<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  r <span style="color: #339933;">=</span> n <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> abs<span style="color: #009900;">&#40;</span> r <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">/</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> t <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    r <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span> r <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">/</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>So is this helpful, declarative, and straightforward?</strong></p>
<p>I think so.</p>
<p>Giving the user the convergence is simply being considerate. We just as easily could have developed a linear method using fixed-point methods, or any method for getting super-quadratic convergence.</p>
<p>Without the comment, it would simply be an implicit assumption that this was &#8220;fast&#8221;, for some value of fast.</p>
<p>We also stated (for the maintainer&#8217;s sake) that we are using Newton&#8217;s Method. The maintainer could verify that the code does what you claim it does, replace it with his favorite super-quadratic convergence sqrt() function, or ignore it because it&#8217;s too mathy.</p>
<p>The example is also concise, in that Mr. Atwood&#8217;s rearrangement of the code allowed a few little comments to convey a world of information.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=109&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/08/04/norvig-atwood-and-comments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
