<?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; code design</title>
	<atom:link href="http://nklein.com/tags/code-design/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>Code Clarity Revisited</title>
		<link>http://nklein.com/2009/05/code-clarity-revisited/</link>
		<comments>http://nklein.com/2009/05/code-clarity-revisited/#comments</comments>
		<pubDate>Mon, 11 May 2009 19:19:40 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code design]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=345</guid>
		<description><![CDATA[In an earlier post, I showed a simple loop written in several programming languages.  The loop had to sum the weights of each items in a list.  Dmitry pointed out a much clearer loop in Lisp using the (loop &#8230;) construct.
&#40;loop for item in item-list sum &#40;weight item&#41;&#41;
I then twiddled for the better [...]]]></description>
			<content:encoded><![CDATA[<p>In an <a href="http://nklein.com/2009/05/programming-when-clarity-counts/">earlier post</a>, I showed a simple loop written in several programming languages.  The loop had to sum the weights of each items in a list.  <a href="http://nklein.com/2009/05/programming-when-clarity-counts/comment-page-1/#comment-30">Dmitry pointed out</a> a much clearer loop in Lisp using the <strong>(loop &#8230;)</strong> construct.</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>loop for item in item-<span style="color: #b1b100;">list</span> sum <span style="color: #66cc66;">&#40;</span>weight item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>I then twiddled for the better part of an hour trying to find the clearest way to do <a href="http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/">weighted random choice</a> with Lisp <strong>(loop &#8230;)</strong>.  This is the best that I have come up with:</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>loop<br />
&nbsp; &nbsp;with total <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>loop for item in item-<span style="color: #b1b100;">list</span> sum <span style="color: #66cc66;">&#40;</span>weight item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;with thresh <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> total<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;for item in item-<span style="color: #b1b100;">list</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">minusp</span> <span style="color: #66cc66;">&#40;</span>decf thresh <span style="color: #66cc66;">&#40;</span>weight item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> item<span style="color: #66cc66;">&#41;</span></div></div>
<p>The biggest pedagogical obstacle in the above is the <strong>(decf &#8230;)</strong> which decrements the variable in place by the given amount and returns the new value of the variable.  What I really wanted to do was this:</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>loop<br />
&nbsp; &nbsp;with total <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>loop for item in item-<span style="color: #b1b100;">list</span> sum <span style="color: #66cc66;">&#40;</span>weight item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;for item in item-<span style="color: #b1b100;">list</span><br />
&nbsp; &nbsp;tracking item<br />
&nbsp; &nbsp;as thresh downfrom <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> total<span style="color: #66cc66;">&#41;</span> by <span style="color: #66cc66;">&#40;</span>weight item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>That fails on multiple fronts, however.</p>
<ul>
<li>There is no <strong>tracking</strong> keyword in <strong>(loop &#8230;)</strong>.  I can sum or maximize or collect items, but I cannot keep track of the last one that I saw.  I had hoped to use the <strong>finally</strong> keyword, but its value isn&#8217;t returned from the loop.</li>
<li>Lisp requires that the decrement amount in the <strong>by</strong> of a <strong>downfrom</strong> be greater than zero.  As such, I have to filter out any items that have zero weight.  Feh.  I can do that.  I would rather not.  But, I can do that.</li>
<li>Lisp only evaluates the <strong>by</strong> expression once.  It does not evaluate it each time through the loop.</li>
</ul>
<p>I am still learning all of the ins and outs of <strong>(loop &#8230;)</strong>.  Today, I learned as many <q>outs</q> as <q>ins</q>.  Feh.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/code-clarity-revisited/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Move over UML, I Have SQL</title>
		<link>http://nklein.com/2009/05/move-over-uml-i-have-sql/</link>
		<comments>http://nklein.com/2009/05/move-over-uml-i-have-sql/#comments</comments>
		<pubDate>Fri, 08 May 2009 19:29:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code design]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=296</guid>
		<description><![CDATA[I have been trying to nail down a design for a sizable software project.  I know how I want the system to behave.  I just hadn&#8217;t nailed down a good API or internal structure for it yet.
I knew that I was going to have to track multiple versions of uniquely identifiable data with [...]]]></description>
			<content:encoded><![CDATA[<p>I have been trying to nail down a design for a sizable software project.  I know how I want the system to behave.  I just hadn&#8217;t nailed down a good API or internal structure for it yet.</p>
<p>I knew that I was going to have to track multiple versions of uniquely identifiable data with different versions stored in different locations.</p>
<p>I had all sorts of <abbr title="Unified Modeling Language">UML</abbr> drawing on whiteboards and graph paper and in <a href="http://www.omnigroup.com/applications/omnigraffle/">OmniGraffle</a>.  Everything just kept getting messy.</p>
<p>Earlier this week, I read a great article about <a href="http://icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">Using SQLite on the iPhone</a>.  So, today, I tried to hammer out what I would do with this data if I were using <abbr title="Structured Query Language">SQL</abbr> to track it.  I wrote a bunch of <b>CREATE TABLE</b> statements and then a few <b>SELECT</b> statements.</p>
<p>It worked.  Getting the <b>JOIN</b>s in the <b>SELECT</b> statements to make sense forced me to make the relations in my data <q>as simple as possible, but no simpler.</q>  I had been trying to jam many-to-many relationships into one-to-many or many-to-one relationships.  The SQL exercise forced me to get it right.</p>
<p>Will I use SQL in the final project?  Maybe, but it doesn&#8217;t seem like I will need it.  Did I need to use SQL for the design?  Absolutely.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/move-over-uml-i-have-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
