<?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; programming by contract</title>
	<atom:link href="http://nklein.com/tags/programming-by-contract/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Numbers 30:2, for programmers</title>
		<link>http://nklein.com/2009/07/numbers-30-2-for-programmers/</link>
		<comments>http://nklein.com/2009/07/numbers-30-2-for-programmers/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 14:38:34 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bible]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[programming by contract]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=686</guid>
		<description><![CDATA[The role-playing game Paranoia was set in a dystopian future where a paranoid computer had seized control to protect us from Commie, mutant traitors. In the game, secret societies were strictly forbidden. As such, every character was a member of some secret society or other. One of those secret societies was the FCCCP: First Church [...]]]></description>
			<content:encoded><![CDATA[<p>The role-playing game <a href="http://en.wikipedia.org/wiki/Paranoia_(role-playing_game)">Paranoia</a> was set in a dystopian future where a paranoid computer had seized control to protect us from Commie, mutant traitors.  In the game, secret societies were strictly forbidden.  As such, every character was a member of some secret society or other.  One of those secret societies was the FCCCP: First Church of Christ, Computer Programmer.  This, of course, tickled me.  Heck, it still does.</p>
<p>Outside the world of role-playing games, there is a long tradition of analyzing sacred texts, pulling out every possible thread of meaning and applying those meanings to every aspect of life.  I&#8217;ve been pondering a series of articles that combines the exegesis with the computer programming.</p>
<p>Well, this week&#8217;s sermon at our synagogue was about the verse <a href="http://www.biblegateway.com/passage/?search=Numbers%2030:2;&#038;version=50;">Numbers 30:2</a>:</p>
<blockquote cite="New King James Version"><p>
If a man makes a vow to the LORD, or swears an oath to bind himself by some agreement, he shall not break his word; he shall do according to all that proceeds out of his mouth.</p></blockquote>
<p>How could I sit there without pondering <q>Programming by Contract</q>?  In fact, I would say that this verse goes much beyond just the contract.  Every line of code makes promises.  Are they promises the code can keep?</p>
<p>To ground the discussion, I dug around for a short function that I wrote more than a year ago.  The following snippet of code takes two arrays: <em>counter</em> and <em>maximum</em>.  The array elements of each are assumed to be integers.  The arrays are assumed to be the same length.  The value of the i-th entry in <em>counter</em> is assumed to be less than the i-th entry in <em>maximum</em>.  If you think of an axis-aligned, n-dimensional cube with one corner at the origin and its opposite corner at the maximum, the goal is for counter to scan through every set of integer coordinates in the cube (well, including the faces that touch the origin, but not including the faces that touch the maximum).  If every entry in maximum is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, then the result should be just like incrementing a base <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> number that is displayed least-significant digit first.  If maximum were <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_dede3bae262b95c2e3326b9380f89837.png" title="\left( 60, 60, 24, 7, 4 \right)" style="vertical-align:-20%;" class="tex" alt="\left( 60, 60, 24, 7, 4 \right)" />, the counter would count out the seconds in February (non-leap year).</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre 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;">defun</span> increment-counter <span style="color: #66cc66;">&#40;</span>counter maximum<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> counter<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>nn <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> counter<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>result <span style="color: #66cc66;">&#40;</span>make-<span style="color: #b1b100;">array</span> nn <span style="color: #66cc66;">:</span><span style="color: #555;">initial-contents</span> counter<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>ii nn <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span>incf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> result ii<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> maximum ii<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">return</span> result<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> result ii<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>What promises does the above code make?  The function name <b>increment-counter</b> is a pretty obvious promise.  The function is going to increment some sort of counter.  Given just the function name, what would we expect?  I would expect that it takes a single argument, that argument is a reference to an integer, and that integer is incremented in place.  I would be way off.  Our promise is already on sketchy grounds.  I&#8217;m not coming up with a particularly good name at the moment though:  <em>step-cube-walker</em>? <em>increment-n-dimensional-counter</em>?</p>
<p>The function doesn&#8217;t increment the <em>counter</em> in place.  Instead, it returns a newly allocated counter (or nil, if we&#8217;ve reached the end).  The function declaration doesn&#8217;t make any promise that <em>maximum</em> will be left untouched, though Lisp doesn&#8217;t make it easy to declare that.  Lisp usually handles that sort of thing by convention.  The function declaration also makes no claim that the <em>counter</em> and <em>maximum</em> arguments should be arrays, let alone that they should be the same length.  (Technically, the code only requires that <em>maximum</em> be an array.  <em>Counter</em> can be any sequence.  Further, <em>maximum</em> is allowed to be longer than <em>counter</em>, but cannot be shorter.)</p>
<p>Line 2 of the code doesn&#8217;t trust you to stop calling it when you&#8217;re already done.  This is an interesting hedge, but not really an oath.  It may rise to that level at some point though if enough code starts to depend on this utterance.</p>
<p>Lines 3 &amp; 4 are the only uses of (a non-nil) <em>counter</em>.  They work just fine for any sequence.  As such, while this function always returns either nil or an array, it will accept any sequence as the <em>counter</em>.</p>
<p>Line 5 outright promises that the function is going to return nil.  This is, of course, countermanded on line 7.  I suppose this is akin to <a href="http://www.biblegateway.com/passage/?search=Numbers%2030:3-5;&#038;version=50;">verses 3, 4, &amp; 5</a>:</p>
<blockquote cite="New King James Version"><p>[3] Or if a woman makes a vow to the LORD, and binds herself by some agreement while in her father’s house in her youth, [4] and her father hears her vow and the agreement by which she has bound herself, and her father holds his peace, then all her vows shall stand, and every agreement with which she has bound herself shall stand. [5] But if her father overrules her on the day that he hears, then none of her vows nor her agreements by which she has bound herself shall stand; and the LORD will release her, because her father overruled her.</p></blockquote>
<p>Even though the loop on line five is released of its promise, it still feels like a cheap back door.</p>
<p>So, you can see that even in these eight lines of code, I have made many, many inadvertent promises.  Hopefully, in future code, I will keep a more careful tongue.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/07/numbers-30-2-for-programmers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

