<?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>nklein software &#187; python</title>
	<atom:link href="http://nklein.com/tags/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Wed, 30 Jun 2010 14:00:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Programming When Clarity Counts</title>
		<link>http://nklein.com/2009/05/programming-when-clarity-counts/</link>
		<comments>http://nklein.com/2009/05/programming-when-clarity-counts/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:10:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=285</guid>
		<description><![CDATA[In my previous post,  I wrote some code in Perl because I wanted the code to be clear and obvious.  Wait?  What?  Who uses Perl when they want clarity?
Perl
I admit:  I was surprised myself.  Seriously, I tried several other languages first.  Let&#8217;s just focus on the first loop [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/">my previous post</a>,  I wrote some code in Perl because I wanted the code to be clear and obvious.  Wait?  What?  Who uses Perl when they want clarity?</p>
<h3>Perl</h3>
<p>I admit:  I was surprised myself.  Seriously, I tried several other languages first.  Let&#8217;s just focus on the first loop here.  Here&#8217;s the Perl again for reference:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>Objective C</h3>
<p>The actual code that I was attempting to explain was in Objective C with the NeXTStep foundation classes.  I couldn&#8217;t imagine that anyone would want to try to understand it:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">double</span> total <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;<br />
<span style="color: #400080;">NSEnumerator</span><span style="color: #002200;">*</span> ee <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>itemList objectEnumerator<span style="color: #002200;">&#93;</span>;<br />
Item<span style="color: #002200;">*</span> item;<br />
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> item <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Item<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>ee nextObject<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; total <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>item weight<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<h3>C</h3>
<p>My first attempt for the article was to write it in C using an array of items.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">double</span> total <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ii<span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> itemCount<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; total <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> itemArray<span style="color: #009900;">&#91;</span> ii <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>That&#8217;s not too terrible.  There is, however, a great deal of syntax and code devoted to handling the array including an extra variable to track its length.  Beyond that, the loop counter is of little use to someone trying to understand the concept.</p>
<h3>C++</h3>
<p>My next go was C++.  My hope was that iterators would make the looping less painful.  C++ iterators, however, are the very model of pain.</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">double</span> total <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span><br />
std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span> Item<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it<span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> it <span style="color: #000080;">=</span> itemList.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> itemList.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> weight<span style="color: #008000;">&#40;</span> <span style="color: #000040;">*</span>it <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>This requires the reader to wade through a syntactic thicket to get the iterator into the picture at all.  It also requires a certain comfort with pointers that one wouldn&#8217;t have coming from another language.</p>
<h3>Java</h3>
<p>My next attempt was Java.  I hate Java.  It may not be terrible for this code with the new looping constructs that came in Java 1.5.  Alas, I quit coding it before I made it through this first loop.</p>
<h3>Lisp</h3>
<p>Of course, I wanted to code this in Lisp:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>total <span style="color: #66cc66;">&#40;</span>reduce #'+ list-of-items <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'weight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>That would be perfectly clear to someone who has used Lisp or Scheme.  Alas, not everyone has.</p>
<h3>So&#8230; Perl&#8230;</h3>
<p>If you want to write the Perl code, you have to understand the difference between scalars and arrays.  To read the code, however, you don&#8217;t have to grok that distinction to see what&#8217;s going on.  I didn&#8217;t try it in Python.  Maybe it would be clear to a wider audience.  I may have to try it next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/programming-when-clarity-counts/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
