<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Sliding up the banister</title>
	<atom:link href="http://joelneely.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelneely.wordpress.com</link>
	<description>Sneaking up on functional programming</description>
	<lastBuildDate>Thu, 04 Jun 2009 14:51:47 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='joelneely.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/13e8a7d496a55942e937d773f4ba0386?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Sliding up the banister</title>
		<link>http://joelneely.wordpress.com</link>
	</image>
			<item>
		<title>Intermission: Google Squared</title>
		<link>http://joelneely.wordpress.com/2009/06/04/intermission-google-squared/</link>
		<comments>http://joelneely.wordpress.com/2009/06/04/intermission-google-squared/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 14:48:24 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=140</guid>
		<description><![CDATA[Google Squared is an impressive (but still beta) offering from The Google that seems to have some real potential for students and others who want to do quick-and-maybe-not-do-dirty research.

This post is also a bit of an experiment. I want to find out whether I can capture screen shots that are large enough to be (at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=140&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://www.google.com/squared">Google Squared</a> is an impressive (but still beta) offering from The Google that seems to have some real potential for students and others who want to do quick-and-maybe-not-do-dirty research.</p>
<blockquote><p>
This post is also a bit of an experiment. I want to find out whether I can capture screen shots that are large enough to be (at least partly) readable without completely blowing up the page layout I&#8217;m currently using for this blog.
</p></blockquote>
<p>Given the subject matter of this blog, it&#8217;s no surprise that I thought of this query:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-1.png?w=434&#038;h=174" alt="Google squared query for programming languages" border="0" width="434" height="174" /></div>
<p>which gave me the following result:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-21.png?w=640&#038;h=357" alt="First query result" border="0" width="640" height="357" /></div>
<p>I admit to being surprised and impressed with the collection of values presented with no additional guidance. However, I want to refine the content a little. First, I&#8217;ll click on the [X] column to the left to remove Pascal, Fortran, Cobol, and Forth, (none of which I&#8217;m <em>currently</em> using <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ; then I&#8217;ll use the &#8220;Add items&#8221; input field to include Java, Scala, Erlang, and Haskell. Some fairly interesting look-ahead kicked in as I began typing:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-3.png?w=210&#038;h=102" alt="Typing J provided a hint for Jython" border="0" width="210" height="102" /></div>
<p>With all my new rows in place, I have this:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-5.png?w=640&#038;h=357" alt="Updated query results with new rows" border="0" width="640" height="357" /></div>
<p>It&#8217;s interesting that &#8220;Appeared In&#8221; was not found for Java, but Google Squared <em>is</em> a beta. I&#8217;m impressed that it chose to put that column in, and <em>did</em> find values for the other languages!</p>
<p>The next hint of the underlying sophistication in G<sup>2</sup> came when I decided to modify the columns. After removing &#8220;Influenced&#8221; and &#8220;Appeared In&#8221;, I started to add replacement columns, and was offered an interesting set of options.</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-6.png?w=194&#038;h=229" alt="List of proposed columns to add" border="0" width="194" height="229" /></div>
<p>Curiosity took over, and I picked &#8220;Typing Discipline&#8221; from the list. The resulting column confirmed that the term was being used correctly (e.g. with values of &#8220;duck,dynamic,strong&#8221; for Python and &#8220;static,strong,inferred&#8221; for Haskell).</p>
<p>Replacing that column with new ones for &#8220;tutorial&#8221; and &#8220;blogs&#8221; gave me some additional leads to follow:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-7.png?w=640&#038;h=357" alt="Updated results with new columns" border="0" width="640" height="357" /></div>
<p>When I clicked on the tutorial column in the Scala row, G<sup>2</sup> provided a pop-up with a link to <a href="http://www.artima.com/index.jsp">Bill</a> <a href="http://www.artima.com/consulting.html">Venners&#8217;</a> presentation on <a href="http://qconsf.com/sf2008/presentation/The+Scala+Experience+-+Programming+with+Functional+Objects">Scala at last year&#8217;s QCon</a>.</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-81.png?w=411&#038;h=170" alt="Pop-up with link to Bill's tutorial" border="0" width="411" height="170" /></div>
<p>I won&#8217;t burden this page (or you, kind reader) with any more screen shots from G<sup>2</sup>; perhaps by now you&#8217;ve seen enough that you&#8217;ve already left to try it out yourself or have decided it&#8217;s not (yet) for you.</p>
<p>There&#8217;s an obvious comparison begging to be done here; <a href="http://www.wolframalpha.com/">Wolfram Alpha</a> wasn&#8217;t immediately successful for this particular search:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/06/picture-91.png?w=581&#038;h=369" alt="Wolfram|Alpha isn't sure what to do with your input." border="0" width="581" height="369" /></div>
<p>&#8230;but let&#8217;s remember that both of these new tools are work in progress.</p>
<p>My overall impression is that it&#8217;s a very impressive start, but has room for improvement. Several of my attempts to add other columns relevant to this blog (such as &#8220;DSL&#8221; or &#8220;parsing&#8221;) yielded &#8220;No value found&#8221; in most or all rows. Even the proposal from G<sup>2</sup> of &#8220;Major Implementations&#8221; was only partially successful. There were multiple values for all languages except Java, Scala, and Erlang, all three of which got &#8220;No value found&#8221;.</p>
<p>It would be really interesting to be able to derive new columns from the contents of others. For example, counting the number of values in a list or doing arithmetic with numeric values would both be handy.</p>
<p>You may have noticed in the full-page screenshots the link at the top-right-hand corner that invited me to &#8220;Sign in to save your Square&#8221;. I did so, and plan to come back later to see if the results change over time.</p>
<p>I&#8217;m very interested in seeing how G<sup>2</sup> grows from this not-so-humble beginning.</p>
Posted in programming languages  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=140&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/06/04/intermission-google-squared/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-1.png" medium="image">
			<media:title type="html">Google squared query for programming languages</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-21.png" medium="image">
			<media:title type="html">First query result</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-3.png" medium="image">
			<media:title type="html">Typing J provided a hint for Jython</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-5.png" medium="image">
			<media:title type="html">Updated query results with new rows</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-6.png" medium="image">
			<media:title type="html">List of proposed columns to add</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-7.png" medium="image">
			<media:title type="html">Updated results with new columns</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-81.png" medium="image">
			<media:title type="html">Pop-up with link to Bill's tutorial</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/06/picture-91.png" medium="image">
			<media:title type="html">Wolfram&#124;Alpha isn't sure what to do with your input.</media:title>
		</media:content>
	</item>
		<item>
		<title>Jesse Tilly at Memphis JUG</title>
		<link>http://joelneely.wordpress.com/2009/05/25/jesse-tilly-at-memphis-jug/</link>
		<comments>http://joelneely.wordpress.com/2009/05/25/jesse-tilly-at-memphis-jug/#comments</comments>
		<pubDate>Mon, 25 May 2009 20:07:58 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MJUG]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=127</guid>
		<description><![CDATA[This month&#8217;s Memphis Java Users&#8217; Group meeting featured Jesse Tilly of IBM Rational Software, who spoke to us on static analysis. He will be doing a more product-intensive session, &#8220;What is IBM&#174; Rational&#174; Software Analyzer&#174; Telling Me?&#8221;, at the upcoming IBM Rational Software Conference. (Don&#8217;t be misled by all those &#8220;circle-R&#8221;s; I just linked to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=127&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This month&#8217;s <a href="http://www.memphisjug.org/">Memphis Java Users&#8217; Group</a> meeting featured Jesse Tilly of <a href="http://www-01.ibm.com/software/rational/">IBM Rational Software</a>, who spoke to us on static analysis. He will be doing a more product-intensive session, <a href="https://1bosweb3.experient-inc.com/Events/Rational/RSDC2009/Agenda/agenda_by_track.cfm?track=NCRM&amp;#NCRM01">&#8220;What is IBM&reg; Rational&reg; Software Analyzer&reg; Telling Me?&#8221;</a>, at the upcoming <a href="http://www-01.ibm.com/software/rational/rsdc/?S_TACT=105AGX23&amp;S_CMP=HPHERO">IBM Rational Software Conference</a>. (Don&#8217;t be misled by all those &#8220;circle-R&#8221;s; I just linked to the title from the conference web site.)</p>
<p>For our meeting, Jesse left <a href="http://www.circlerranch.org/">the</a> <a href="http://www.circlersaddles.com/">branding</a> <a href="http://www.4thegrill.com/dsrare.html">iron</a> at home. He began with an overview of the history and benefits of static analysis. The major portion of the presentation offered a practical approach to analysis as part of a development project, including a detailed how-to on interpreting and using analysis results. Jesse finished with return to history, drawing unexpected parallels with the analysis of <a href="http://en.wikipedia.org/wiki/Enigma_machine">Enigma</a> traffic at <a href="http://www.bletchleypark.org.uk/">Bletchley Park</a> during WWII&mdash;the background for <a href="http://en.wikipedia.org/wiki/Alan_Turing">Allen</a> <a href="http://plato.stanford.edu/entries/turing/">Turing</a>&#8217;s later <a href="http://mathworld.wolfram.com/TuringMachine.html">theoretical work</a> that led to the <a href="http://www.youtube.com/watch?v=cYw2ewoO6c4">computers</a> we program <a href="http://ironphoenix.org/tril/tm/">today</a>.</p>
<p>Because Jesse had an early flight, our regular door-prize drawing followed his presentation. In our lightning talk segment, <a href="http://www.mattstine.com/">Matt Stine</a> introduced <a href="http://mor.ph/">Morph AppSpace</a>, I presented on &#8220;Structured Functional Programming&#8221; (<a href="http://joelneely.files.wordpress.com/2009/05/strucfunprogmjug.pdf" title="StrucFunProgMJUG.pdf">pdf here</a>), and Walter Heger gave a quick look at <a href="http://www.jgears.org/">jGears</a>.</p>
<hr />
<p>Recommended reading:</p>
<p>Encryption and cryptanalysis are deeply entwined with computing, whether in history(<a href="http://www.amazon.com/gp/product/0192801325?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0192801325"><i>Codebreakers: The Inside Story of Bletchley Park</i></a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=0192801325" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" />) or in imagination (<a href="http://www.amazon.com/gp/product/0060512806?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0060512806"><i>Cryptonomicon</i></a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=0060512806" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" />).</p>
<p>Two highly-respected tools for static analysis in Java are FindBugs and PMD; both web sites offer excellent documentation and other reference material.</p>
Posted in functional programming, Java, MJUG  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=127&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/05/25/jesse-tilly-at-memphis-jug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=0192801325" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=0060512806" medium="image" />
	</item>
		<item>
		<title>BuilderBuilder: The Model in Haskell</title>
		<link>http://joelneely.wordpress.com/2009/05/09/builderbuilder-the-model-in-haskell/</link>
		<comments>http://joelneely.wordpress.com/2009/05/09/builderbuilder-the-model-in-haskell/#comments</comments>
		<pubDate>Sat, 09 May 2009 23:53:23 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[BuilderBuilder]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=114</guid>
		<description><![CDATA[This post describes the first model in Haskell for the BuilderBuilder task. We will develop the model incrementally until we have rough parity with the Java version.
I&#8217;m experimenting with ways to distinguish user input from system output in transcripts of interactive sessions. This time I&#8217;m trying color, using a medium blue for output. I will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=114&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This post describes the first model in Haskell for the <a href="http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/">BuilderBuilder</a> <a href="http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/">task</a>. We will develop the model incrementally until we have rough parity with the Java version.</p>
<blockquote><p><em>I&#8217;m experimenting with ways to distinguish user input from system output in transcripts of interactive sessions. This time I&#8217;m trying color, using a <font color="#0066ff">medium blue</font> for output. I will appreciate feedback on whether that works for you.</em></p></blockquote>
<h3>Step one: defining and using a type</h3>
<p>The simplest possible Haskell version of our model for a Java field is:</p>
<blockquote><pre><code>data JField = JField String String
</code></pre>
</blockquote>
<p>However, the <a href="http://www.lyberty.com/encyc/articles/abbr.html">PMNOPML</a> (&#8220;Pay Me Now Or Pay Me Later&#8221;) principle says that we&#8217;ll regret it if we stop there. In fact, later comes quickly.</p>
<p>We can create an instance of <code>JField</code> in a source file:</p>
<blockquote><pre><code>field1 = JField "name1" "Type1"
</code></pre>
</blockquote>
<p>To do the same in a <code>ghci</code> session, prefix each definition with <code>let</code>, as in:</p>
<blockquote><pre><code><font color="#0066ff">*Main&gt; </font>let field1 = JField "name1" "Type1"
</code></pre>
</blockquote>
<h3>Step two: showing the data</h3>
<p>Trying to look at the instance yields a fairly opaque error message.</p>
<blockquote><pre><code><font color="#0066ff">*Main&gt; </font>field1
<font color="#0066ff">
&lt;interactive&gt;:1:0:
    No instance for (Show JField)
      arising from a use of `print' at &lt;interactive&gt;:1:0-5
    Possible fix: add an instance declaration for (Show JField)
    In a stmt of a 'do' expression: print it</font>
</code></pre>
</blockquote>
<p>Remember that in Java the default definition of <code>toString()</code> returns something like <code>com.localhost.builderbuilder.JFieldDTO@53f67e</code>; that&#8217;s also obscure at first glance. Haskell just goes a bit further, complaining that we haven&#8217;t defined how to show a <code>JField</code> instance. We can ask for a default implementation by adding <code>deriving Show</code> to a data type definition:</p>
<blockquote><pre><code>data JField = JField String String deriving Show
</code></pre>
</blockquote>
<p>After loading that change, we get back a string that resembles the field&#8217;s defining expression:</p>
<blockquote><pre><code><font color="#0066ff">*Main&gt; </font>field1
<font color="#0066ff">JField "name1" "Type1"</font>
</code></pre>
</blockquote>
<h3>Step three: referential transparency</h3>
<p>Our first model represented a Java class by its package, class name, and enclosed fields. The Haskell equivalent is:</p>
<blockquote><pre><code>data JClass = JClass String String [JField] deriving Show
</code></pre>
</blockquote>
<p>The square brackets mean &#8220;list of &#8230;&#8221;, so a <code>JClass</code> takes two strings and a list of <code>JField</code> values. I&#8217;ll say more about lists in a moment, but first let&#8217;s deal with <a href="http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)">referential transparency</a>.</p>
<p>We can build a class incrementally:</p>
<blockquote><pre><code>field1 = JField "name1" "Type1"
field2 = JField "name2" "Type2"
class1 = JClass "com.sample.foo" "TestClass" [field1, field2]
</code></pre>
</blockquote>
<p>or all at once:</p>
<blockquote><pre><code>class1 = JClass "com.sample.foo"
                "TestClass"
                [   JField "name1" "Type1" ,
                    JField "name2" "Type2"
                ]
</code></pre>
</blockquote>
<p>and get the same result:</p>
<blockquote><pre><code><font color="#0066ff">*Main&gt; </font>class1
<font color="#0066ff">JClass "com.sample.foo" "TestClass" [JField "name1" "Type1",JField "name2" "Type2"]</font>
</code></pre>
</blockquote>
<p>As mentioned <a href="http://joelneely.wordpress.com/2009/05/09/builderbuilder-haskell-preliminaries/">previously</a>, only one of those definitions of <code>class1</code> can go in our program. To Haskell, <code>name = expression</code> is a permanent commitment. From that point forward, we can use <code>name</code> and <code>expression</code> interchangeably, because they are expected to mean the same thing. That expectation would break if we were allowed to give <code>name</code> another meaning later (in the same scope). </p>
<p>Consequently, we can define a class using previously defined fields, or we can just write everything in one definition, nesting the literal fields inside the class definition. As we&#8217;ll see later, this also has implications for how we write functions; a &#8220;pure&#8221; function and its definition are also interchangeable.</p>
<h3>Step four: lists</h3>
<p>The array is the most fundamental multiple-valued data structure in Java; the list plays a corresponding role in Haskell. In fact, lists are so important that there are a few syntactical short-cuts for dealing with lists.</p>
<ul>
<li><b>Type notation:</b> If <code>t</code> is any Haskell type, then <code>[t]</code> represents a list of values of that type.</li>
<li><b>Empty lists:</b> Square brackets with no content, written as <code>[]</code>, indicate a list of length zero.</li>
<li><b>Literal lists:</b> Square brackets, enclosing a comma-separated sequence of values of the same type, represent a literal list.</li>
<li><b>Constructing lists:</b> The <code>:</code> operator constructs a new list from its left argument (a single value) and right argument (a list of the same type).</li>
</ul>
<p>For example, <code>["my","dog","has","fleas"]</code> is a literal value that has type <code>[String]</code> and contains four strings. <code>"my":["dog","has","fleas"]</code> and <code>"my":"dog":"has":"fleas":[]</code> are equivalent expressions that compute the list instead of stating it as a literal value.</p>
<p>By representing the fields in a class with a list, we achieve two benefits:</p>
<ul>
<li>The number of fields can vary from class to class.</li>
<li>The order of the fields is significant.</li>
</ul>
<h3>Step five: types and records</h3>
<p>Given a <code>JField</code>, how do we get its name? Or its type? We can define functions:</p>
<blockquote><pre><code>fieldName (JField n _) = n
fieldType (JField _ t) = t
</code></pre>
</blockquote>
<p>and do the same for the <code>JClass</code> data:</p>
<blockquote><pre><code>package   (JClass p _ _ ) = p
className (JClass _ n _ ) = n
fields    (JClass _ _ fs) = fs
</code></pre>
</blockquote>
<p>but all that typing seems tiresome.</p>
<p>Before solving that problem, let&#8217;s note two other limitations of our current implementation:</p>
<ul>
<li>Definitions using multiple <code>String</code> values leave us with the burden of remembering the meaning of each strings.</li>
<li>The derived <code>show</code> method leaves us with a similar problem; it doesn&#8217;t help distinguish values of the same type.</li>
</ul>
<p>If you suspect that I&#8217;m going to pull another rabbit out of Haskell&#8217;s hat, you&#8217;re right. In fact, two rabbits.</p>
<h4>Type declarations</h4>
<p>We can make our code more readable by defining synonyms that help us remember <em>why</em> we&#8217;re using a particular type. By adding these definitions:</p>
<blockquote><pre><code>type Name     = String
type JavaType = String
type Package  = String
</code></pre>
</blockquote>
<p>we can rewrite our data definitions to be more informative:</p>
<blockquote><pre><code>data JField = JField Name JavaType deriving Show
data JClass = JClass Package Name [JField] deriving Show
</code></pre>
</blockquote>
<h4>Record syntax</h4>
<p>The second rabbit is a technique to get Haskell to do even more work for us. We represent each component of a data type as a name with an explicit type&mdash;all in curly braces, separated by commas:</p>
<blockquote><pre><code>data JField = JField {
     fieldName :: Name ,
     fieldType :: JavaType
} deriving Show

data JClass = JClass {
     package   :: Package ,
     className :: Name ,
     fields    :: [JField]
} deriving Show
</code></pre>
</blockquote>
<p>When we use this syntax, Haskell creates the accessor functions automagically, and enables a more explicit and flexible notation to create values. All of these definitions:</p>
<blockquote><pre><code>field1  = JField "name1" "Type1"
field1a = JField {fieldName = "name1", fieldType = "Type1"}
field1b = JField {fieldType = "Type1", fieldName = "name1"}
</code></pre>
</blockquote>
<p>produce equivalent results:</p>
<blockquote><pre><code><font color="#0066ff">*Main&gt; </font>field1
<font color="#0066ff">JField {fieldName = "name1", fieldType = "Type1"}</font>
<font color="#0066ff">*Main&gt; </font>field1a
<font color="#0066ff">JField {fieldName = "name1", fieldType = "Type1"}</font>
<font color="#0066ff">*Main&gt; </font>field1b
<font color="#0066ff">JField {fieldName = "name1", fieldType = "Type1"}</font>
</code></pre>
</blockquote>
<h3>Step last: that&#8217;s it!</h3>
<p>We have covered quite a bit of ground! The complete source code for the model appears at the end of this post. With both Java and Haskell behind us, we have most of the basic ideas we&#8217;ll need for the Erlang and Scala versions.</p>
<hr />
<p>Recommended reading:</p>
<p><a href="http://www.amazon.com/gp/product/0596514980?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596514980"><em>Real World Haskell</em></a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=0596514980" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" />, which is <a href="http://www.amazon.com/gp/product/B0026OR2FY?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B0026OR2FY">also available</a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=B0026OR2FY" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" /><br />
 for <a href="http://www.amazon.com/gp/product/B00154JDAI?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B00154JDAI">the Amazon Kindle</a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=B00154JDAI" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" /><br />
 or <a href="http://book.realworldhaskell.org/read/">on-line</a> at <a href="http://www.realworldhaskell.org/blog/">the book&#8217;s web site</a>. I really can&#8217;t say enough good things about this book. </p>
<hr />
<p>The current <code>BuilderBuilder</code> mode in Haskell, along with sample data, is this:</p>
<blockquote><pre><code>-- BuilderBuilder.hs

-- data declarations

type Name     = String
type JavaType = String
type Package  = String

data JField = JField {
     fieldName :: Name ,
     fieldType :: JavaType
} deriving Show

data JClass = JClass {
     package   :: Package ,
     className :: Name ,
     fields    :: [JField]
} deriving Show

-- sample data for demonstration and testing

field1  = JField "name1" "Type1"
field1a = JField {fieldName = "name1", fieldType = "Type1"}
field1b = JField {fieldType = "Type1", fieldName = "name1"}

field2 = JField "name2" "Type2"

class1 = JClass "com.sample.foo" "TestClass" [field1, field2]

studentDto = JClass {
    package   = "edu.bogusu.registration" ,
    className = "StudentDTO" ,
    fields    = [
        JField {
            fieldName = "id" ,
            fieldType = "String"
        },
        JField {
            fieldName = "firstName" ,
            fieldType = "String"
        },
        JField {
            fieldName = "lastName" ,
            fieldType = "String"
        },
        JField {
            fieldName = "hoursEarned" ,
            fieldType = "int"
        },
        JField {
            fieldName = "gpa" ,
            fieldType = "float"
        }
    ]
}
</code></pre>
</blockquote>
<hr />
<p>Updated 2009-05-09 to correct formatting and add category.</p>
Posted in BuilderBuilder, functional programming, Haskell, OOP, Ruby, tutorial  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=114&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/05/09/builderbuilder-the-model-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=0596514980" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=B0026OR2FY" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=B00154JDAI" medium="image" />
	</item>
		<item>
		<title>BuilderBuilder: Haskell Preliminaries</title>
		<link>http://joelneely.wordpress.com/2009/05/09/builderbuilder-haskell-preliminaries/</link>
		<comments>http://joelneely.wordpress.com/2009/05/09/builderbuilder-haskell-preliminaries/#comments</comments>
		<pubDate>Sat, 09 May 2009 14:38:21 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[BuilderBuilder]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=110</guid>
		<description><![CDATA[The next step in the BuilderBuilder project is to develop a model in Haskell that is analogous to the Java model in the previous post. This post will introduce just enough Haskell to get started; the next post will get into the BuilderBuilder model.
Environment:
I&#8217;m using GHC 6.10.1, obtained from the Haskell web site. There are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=110&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The next step in the <a href="http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/">BuilderBuilder</a> project is to develop a model in Haskell that is analogous to the Java model in <a href="http://joelneely.wordpress.com/2009/05/03/builderbuilder-the-model-in-java/">the previous post</a>. This post will introduce just enough Haskell to get started; the next post will get into the BuilderBuilder model.</p>
<h3>Environment:</h3>
<p>I&#8217;m using <a href="http://haskell.org/ghc/">GHC 6.10.1</a>, obtained from <a href="http://www.haskell.org/">the Haskell web site</a>. There are a variety of platform-specific binaries; I used the classic configure/make/install process on OSX. (For Java programmers, <a href="http://en.wikipedia.org/wiki/Make_(software)">make</a> is what <a href="http://www.youtube.com/watch?v=dFUlAQZB9Ng">we used</a> instead of <a href="http://ant.apache.org/">ant</a> back in the <a href="http://www.jurassicpark.com/">Jurassic</a> era.) Consult <a href="http://www.haskell.org/haskellwiki/Implementations">the Haskell Implementations page</a> for details on obtaining Haskell for your preferred platform.</p>
<p>The complete development environment consists of two windows: one running a text editor, and the other running <code>ghci</code>, the interactive Haskell shell that comes with GHC. </p>
<h3>Haskell introduction:</h3>
<p>Use your text editor to create a file named <code>bb1.hs</code> with this content:</p>
<blockquote><pre><code>-- bb1.hs

-- simplest possible data declarations

data JField = JField String String

-- sample data for demonstration and testing

field1 = JField "id" "String"

-- sample function

helloField :: JField -&gt; String
helloField (JField n t) = "Hello, " ++ n ++ ", of type " ++ t
</code></pre>
</blockquote>
<p>Then run <code>ghci</code> as follows, where user input is underlined:</p>
<blockquote><pre><code>your-prompt-here$ <u>ghci</u>
GHCi, version 6.10.1: http://www.haskell.org/ghc/  <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_confused.gif' alt=':?' class='wp-smiley' />  for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude&gt; <u>:l bb1.hs</u>
[1 of 1] Compiling Main             ( bb1.hs, interpreted )
Ok, modules loaded: Main.
*Main&gt; <u>helloField field1</u>
"Hello, id, of type String"
*Main&gt;
</code></pre>
</blockquote>
<p>We started <code>ghci</code>, told it to load our source file (the <code>:l</code> &#8230; line), and then invoked the <code>helloField</code> function on the sample field. Now let&#8217;s examine the Haskell features used in that code. The lines beginning with double-hyphens are comments, and will be ignored in the description.</p>
<h4>Defining data types</h4>
<p>Because Haskell emphasizes functions, it&#8217;s no surprise that the syntax for defining data types is very lightweight. The Java BuilderBuilder model represents a field with two strings, one for the name and one for the type. The simplest possible Haskell equivalent is:</p>
<blockquote><pre><code>data JField = JField String String
</code></pre>
</blockquote>
<p>This defines a data type named <code>JField</code>. It has a constructor (also named <code>JField</code>) that takes two strings, distinguished only by the order in which they are written.</p>
<h4>Defining values</h4>
<p>The next line of code defines an instance of this type:</p>
<blockquote><pre><code>field1 = JField "id" "String"
</code></pre>
</blockquote>
<p>The equal sign means &#8220;<em>is defined as</em>&#8220;. That statement defines <code>field1</code> as the instance of <code>JField</code> constructed on the right-hand side. It is <b>not</b> declaring and initializing a mutable variable. Within the current scope, attempting to redefine <code>field1</code> will produce an error. (More about scope later.)</p>
<h4>Defining functions</h4>
<p>Finally, we have a simple function that converts a <code>JField</code> to a <code>String</code>.</p>
<blockquote><pre><code>helloField :: JField -&gt; String
helloField (JField n t) = "Hello, " ++ n ++ ", of type " ++ t
</code></pre>
</blockquote>
<p>Everything in Haskell has a type, including functions. The double colon means &#8220;<em>is of type</em>&#8220;, so the type of <code>helloField</code> is function from <code>JField</code> to <code>String</code>.</p>
<p>The value of applying <code>helloField</code> to a <code>JField</code> containing strings <code>n</code> and <code>t</code> is defined by the expression on the right-hand side. Haskell regards strings as lists of characters; the <code>++</code> operator concatenates lists of any type. The names <code>n</code> and <code>t</code> are only meaningful within that definition, similar to the local variables in this Java fragment:</p>
<blockquote><pre><code>public static String helloField(IJField f) {
    String n = f.getName();
    String t = f.getType();
    return "Hello, " + n + ", of type" + t;
}
</code></pre>
</blockquote>
<h4>Type inference</h4>
<p>Java requires that we explicitly declare the local variables as type <code>String</code>. But in Haskell, because <code>JField</code> is specified to have two <code>String</code> values, the compiler can infer the types of <code>n</code> and <code>t</code> In fact, the entire first line of <code>helloField</code> is not necessary. The defining equation in the second line explicitly uses a <code>JField</code> on the left and constructs a <code>String</code> on the right. Therefore, the compiler can infer <code>JField -&gt; String</code> as the type of the function. Haskell&#8217;s type inference allows us to write very compact code without giving up strong, static typing.</p>
<p>To see that in action, add the following line to the end of your <code>bb1.hs</code> file:</p>
<blockquote><pre><code>hiField (JField n _) = "Hi, " ++ n
</code></pre>
</blockquote>
<p>(The underscore is a wild card, showing the presence of a second value but indicating that we don&#8217;t need it in this function.)</p>
<p>Reloading <code>bb1.hs</code> in <code>ghci</code> allows us to see type inference at work.</p>
<blockquote><pre><code>*Main&gt; <u>:l bb1.hs</u>
[1 of 1] Compiling Main             ( bb1.hs, interpreted )
Ok, modules loaded: Main.
*Main&gt; <u>hiField field1</u>
"Hi, id"
*Main&gt; <u>:type hiField</u>
hiField :: JField -&gt; [Char]
</code></pre>
</blockquote>
<p>As we&#8217;ll see later in this series, <a href="http://www.scala-lang.org/">Scala</a> brings type inference to the JVM environment. Coming from the dynamic language side, the <a href="http://www.cs.umd.edu/projects/PL/druby/index.html">Diamondback Ruby</a> research project is adding type inference to <a href="http://www.ruby-lang.org/en/">Ruby</a>. So perhaps type inference is (finally) an idea whose time has come.</p>
<p>We&#8217;ll pick up more Haskell details along the way, but we have enough to start defining our first BuilderBuilder model. That will be the subject of the next post.</p>
<hr />
<p>Updated 2009-05-09 to fix formatting.</p>
Posted in BuilderBuilder, functional programming, Haskell, tutorial  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=110&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/05/09/builderbuilder-haskell-preliminaries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>
	</item>
		<item>
		<title>BuilderBuilder: The Model in Java</title>
		<link>http://joelneely.wordpress.com/2009/05/03/builderbuilder-the-model-in-java/</link>
		<comments>http://joelneely.wordpress.com/2009/05/03/builderbuilder-the-model-in-java/#comments</comments>
		<pubDate>Sun, 03 May 2009 22:32:58 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[BuilderBuilder]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=103</guid>
		<description><![CDATA[This post will describe a tiny Java model for implementing the BuilderBuilder task. It is simple almost to the point of crudity, because the goal of the series is to compare languages and styles, not to produce production-ready sample code.
This post will focus on the parts of the overall data flow highlighted below:

The interfaces:
I&#8217;m using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=103&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This post will describe a tiny Java model for implementing the <a href="http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/">BuilderBuilder</a> <a href="http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/">task</a>. It is simple almost to the point of crudity, because the goal of the series is to compare languages and styles, not to produce production-ready sample code.</p>
<p>This post will focus on the parts of the overall data flow highlighted below:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/05/generationmodel.jpg?w=346&#038;h=202" alt="GenerationModel.jpg" border="0" width="346" height="202" /></div>
<h3>The interfaces:</h3>
<p>I&#8217;m using interfaces to hide implementation from the remainder of the code. The first version will use simple DTOs, but I want to leave other options (e.g. by reflection against existing DTO classes) open for later exploration.</p>
<p>This first model has two interfaces; one for a Java class:</p>
<blockquote><pre><code>package com.localhost.builderbuilder;

public interface IJClass {
    public String getPkg();
    public String getName();
    public IJField[] getFields();
}
</code></pre>
</blockquote>
<p>and the other for a Java field:</p>
<blockquote><pre><code>package com.localhost.builderbuilder;

public interface IJField {
    public String getName();
    public String getType();
}</code></pre>
</blockquote>
<p>We all know that &#8220;<a href="http://c2.com/cgi/wiki?DoTheSimplestThingThatCouldPossiblyWork">the simplest thing that could possibly work</a>&#8221; doesn&#8217;t mean &#8220;the stupidest thing that could possibly work&#8221;. The use of an array may cross that line, but it was a deliberate choice. Developers who moved to OOP from imperative programming are very familiar with arrays. We&#8217;ll be able to compare array processing against the FP style of list processing, and perhaps consider other OOP alternatives later on.</p>
<h3>First implementations:</h3>
<p>In the spirit of <a href="http://en.wikipedia.org/wiki/Eat_one%27s_own_dog_food">eating</a> our own <a href="http://c2.com/cgi/wiki?DogFood">dog food</a>, the simple DTO implementation of those interfaces will contain their own <code>Builder</code> inner classes. Given that, there&#8217;s no surprise in the <code>JFieldDTO</code> code, which appears at the end of this post.</p>
<p>The <code>JClassDTO</code> class throws in one new wrinkle&mdash;instead of having a <code>fields(IJField[] fields)</code> method that accepts an entire field array, <code>JClassDTO.Builder</code> provides a <code>field(IJField field)</code> method that accepts one field at a time, accumulating them to be placed in an array by the <code>instance()</code> method. The complete code for <code>JClassDTO</code> is given at the end.</p>
<p>It remains to be seen whether this DTO-style implementation is throw-away code, but getting a first implementation in hand will allow us to start comparing data types and structures with the other language, and then move directly to the generation phase of the project. We can always come back and add features (and complexity <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) at a later time.</p>
<hr />
<p>Recommended reading:</p>
<ul>
<li>Anything by <a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings">&#8220;Uncle&#8221; Bob Martin</a>.</li>
<li>The origin of the <a href="http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html">Model-View-Controller (MVC)</a> paradigm.</li>
</ul>
<hr />
<h4>The <code>JFieldDTO</code> implementation:</h4>
<blockquote><pre><code>package com.localhost.builderbuilder;

public class JFieldDTO implements IJField {

    private final String name;
    private final String type;

    public static class Builder {

        private String name;
        private String type;

        private Builder() {
            // do nothing
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder type(String type) {
            this.type = type;
            return this;
        }

        public JFieldDTO instance() {
            return new JFieldDTO(name, type);
        }
    }

    public static Builder builder() {
        return new Builder();
    }

    private JFieldDTO(String name, String type) {
        this.name = name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

}
</code></pre>
</blockquote>
<h4>The <code>JClassDTO</code> implementation:</h4>
<blockquote><pre><code>package com.localhost.builderbuilder;

import java.util.ArrayList;
import java.util.List;

public class JClassDTO implements IJClass {

    private final String pkg;
    private final String name;
    private final IJField[] fields;

    public static class Builder {

        private String pkg;
        private String name;
        private List&lt;JFieldDTO&gt; fields;

        private Builder() {
            fields = new ArrayList&lt;JFieldDTO&gt;();
        }

        public Builder pkg(String pkg) {
            this.pkg = pkg;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder field(JFieldDTO field) {
            this.fields.add(field);
            return this;
        }

        public IJClass instance() {
            return new JClassDTO(
                pkg,
                name,
                fields.toArray(new JFieldDTO[fields.size()])
            );
        }

    }

    public static Builder builder() {
        return new  Builder();
    }

    private JClassDTO(String pkg, String name, IJField[] fields) {
        this.pkg = pkg;
        this.name = name;
        this.fields = fields;
    }

    public String getPkg() {
        return pkg;
    }

    public String getName() {
        return name;
    }

    public IJField[] getFields() {
        return fields;
    }

}
</code></pre>
</blockquote>
<hr />
<p>Updated 2009-05-09 to fix some formatting and to add a category.</p>
Posted in BuilderBuilder, education, Java, OOP, tutorial  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/103/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=103&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/05/03/builderbuilder-the-model-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/05/generationmodel.jpg" medium="image">
			<media:title type="html">GenerationModel.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>BuilderBuilder: The Task</title>
		<link>http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/</link>
		<comments>http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 13:20:07 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[BuilderBuilder]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=93</guid>
		<description><![CDATA[Short version:
Given minimal information (package, class name, and collection of fields described by name and type), produce Java source for a data transfer class, including a static inner class that functions as a builder.
The data flow of this task looks like this:

Given data in a specified input format, a loader will consume the input to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=93&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3>Short version:</h3>
<p>Given minimal information (package, class name, and collection of fields described by name and type), produce Java source for a data transfer class, including a static inner class that functions as a builder.</p>
<p>The data flow of this task looks like this:</p>
<div style="text-align:center;"><img src="http://joelneely.files.wordpress.com/2009/05/generationdataflow1.jpg?w=346&#038;h=202" alt="GenerationDataFlow.jpg" border="0" width="346" height="202" /></div>
<p>Given data in a specified input format, a loader will consume the input to produce a model of the class and fields. A code generator will consume the model and produce Java source. The &#8220;direct construction&#8221; case allows model instances to be produced by code without the need for source data.</p>
<h3>Long version:</h3>
<p>Data transfer objects (described <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object">here</a>) may be used to pass data across boundaries (between systems via network, between Java and non-Java systems, etc.) Developers supporting the Registrar&#8217;s office at Bogus University might create a data transfer class representing a student, beginning as follows:</p>
<blockquote><pre><code>package edu.bogusu.registration;

public class StudentDTO {
    private String id;
    private String firstName;
    private String lastName;
    private int hoursEarned;
    private float gpa;
    // ... other fields
    // ... construction method(s)
    // ... get methods
}
</code></pre>
</blockquote>
<p>Constructing an instance of <code>StudentDTO</code> is commonly done in one of two ways:</p>
<ol>
<li>calling a constructor that has a parameter for every field, or</li>
<li>calling a no-argument constructor, then calling a set&#8230; method for every field.</li>
</ol>
<p>The first approach can minimize clutter in the client code that creates a <code>StudentDTO</code> and, more importantly, makes it easy for  <code>StudentDTO</code> to be immutable. But as the number of fields grows, so does the parameter list to the constructor call; it becomes ugly and potentially error-prone. The second approach makes the initialization more explicit, at the cost of losing immutability. For this series, we&#8217;re going to go through door number three.</p>
<h4>Enter The Builder</h4>
<p>We&#8217;ll create another class whose job is to construct <code>StudentDTO</code> instances. Making it a static inner class to <code>StudentDTO</code> allows us to keep the construction machinery private, giving us more control over the way clients obtain an instance. It also allows us to re-use the idea without name bloat; any <code>FooDTO</code> can have an associated <code>FooDTO.Builder</code>.</p>
<h5>Usage</h5>
<p>The code is a bit tedious, so we&#8217;ll begin with a sample of the intended usage.</p>
<blockquote><pre><code>StudentDTO student = StudentDTO.builder()
    .id(ID)
    .firstName(FIRST_NAME)
    .lastName(LAST_NAME)
    .hoursEarned(HOURS_EARNED)
    .gpa(GPA)
    .instance();
</code></pre>
</blockquote>
<p>The static method <code>StudentDTO.builder()</code> provides an instance of the inner <code>Builder</code> class. There is a method on <code>Builder</code> for each field of <code>StudentDTO</code>. Each of those methods returns the <code>Builder</code> instance, allowing the chaining shown in the sample. Finally, the <code>instance()</code> method returns an instance of <code>StudentDTO</code></p>
<p>The net effect is that of a constructor method with named parameters; we can provide them in whatever order we wish, and can even do the initialization in stages:</p>
<blockquote><pre><code>StudentDTOBuilder studentBuilder = StudentDTO.builder()
    .id(ID)
    .firstName(FIRST_NAME)
    .lastName(LAST_NAME);
// some tedious computation of HOURS_EARNED
studentBuilder.hoursEarned(HOURS_EARNED);
// some tedious computation of GPA
studentBuilder.gpa(GPA);
// and finally
StudentDTO student = studentBuilder.instance();
</code></pre>
</blockquote>
<p>(I&#8217;m not suggesting that we <em>should</em> use it that way; just demonstrating the flexibility of the technique.)</p>
<h5>The Code</h5>
<p>There&#8217;s a high level of redundancy, so I&#8217;ll post just enough to show how the code is organized; the remainder should be obvious.</p>
<blockquote><pre><code>package edu.bogusu.registration;

public class StudentDTO {

    private String id;
    private String firstName;
    private String lastName;
    private int hoursEarned;
    private float gpa;

    public static class Builder {

        private String id;
        // etc. for all StudentDTO fields
        private float gpa;

        private Builder() {
            // nothing here
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        // similar methods for the other fields

        public Builder gpa(float gpa) {
            this.gpa = gpa;
            return this;
        }

        public StudentDTO instance() {
            return new StudentDTO(
                    id,
                    firstName,
                    lastName,
                    hoursEarned,
                    gpa
            );
        }
    }

    public static Builder builder() {
        return new Builder();
    }

    private StudentDTO(
            String id,
            String firstName,
            String lastName,
            int hoursEarned,
            float gpa
    ) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.hoursEarned = hoursEarned;
        this.gpa = gpa;
    }

    public String getId() {
        return id;
    }

    // all StudentDTO fields have get methods

    public float getGpa() {
        return gpa;
    }

}
</code></pre>
</blockquote>
<p>Just a few key points before closing:</p>
<ul>
<li><b>The constructors are private to control instance creation.</b> Client code never uses <code>new... ()</code> for either class. In addition, we can later address the question of making sure that all required data are present, so that a &#8220;half-baked&#8221; instance is never created.</li>
<li><b>Each <code>StudentDTO</code> field is shadowed in <code>StudentDTO.Builder</code>.</b> This represents the &#8220;work-in-progress inventory&#8221; without creating an inconsistent or incomplete object.</li>
<li><b>The ugliness (and risk) of a zillion-parameter constructor for <code>StudentDTO</code> is hidden from the client.</b> Only the generator needs to see that monster.</li>
<li><b>Much of the redundancy is driven by Java&#8217;s approach to explicit, static typing.</b> Code generation will keep the many repetitions of the same information in synch.</li>
</ul>
<p>That&#8217;s enough to get a simple version of the problem on the table. Next time, we&#8217;ll begin looking at internal representations: first in Java, then in Haskell, Erlang, and Scala.</p>
<hr />
Recommended reading:</p>
<ul>
<li>The classic &#8220;favor immutability&#8221; essay is Item 13 in <a href="http://www.amazon.com/gp/product/0321356683?ie=UTF8&amp;tag=slidiuptheban-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321356683"><i>Effective Java (2nd Edition) (Java Series)</i></a><img src="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&amp;l=as2&amp;o=1&amp;a=0321356683" width="1" height="1" border="0" alt="" style="border:none!important;margin:0!important;" />, by Joshua Bloch.</li>
<li>The OOPSLA 2006 paper by Steve Freeman and Nat Pryce, <a href="http://www.jmock.org/oopsla2006.pdf">&#8220;Evolving an Embedded Domain-Specific Language in Java&#8221;</a>, takes this technique all the way to 11. It describes how they evolved the idea over several generations of <a href="http://www.jmock.org/index.html">the jMock test framework</a>.</li>
<li>Martin Fowler&#8217;s <a href="http://martinfowler.com/bliki/FluentInterface.html">&#8220;FluentInterface&#8221;</a>article discusses pros and cons of the approach in detail.</li>
<li><a href="http://www.ibm.com/developerworks/java/library/j-jtp02183.html">&#8220;Java theory and practice: To mutate or not to mutate?&#8221;</a>, by Brian Goetz, gives thorough coverage to the subject of immutability in Java.
</ul>
Posted in BuilderBuilder, Java, tutorial  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=93&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>

		<media:content url="http://joelneely.files.wordpress.com/2009/05/generationdataflow1.jpg" medium="image">
			<media:title type="html">GenerationDataFlow.jpg</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.com/e/ir?t=slidiuptheban-20&#38;l=as2&#38;o=1&#38;a=0321356683" medium="image" />
	</item>
		<item>
		<title>BuilderBuilder: The Agenda</title>
		<link>http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/</link>
		<comments>http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 22:45:34 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[BuilderBuilder]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=91</guid>
		<description><![CDATA[The &#8220;BuilderBuilder&#8221; posts will be my exploration of one question: &#8220;What does Functional Programming mean to me as a practicing OO software developer?&#8221; Despite contemporary buzz about how FP will save the world from bugs and single-threaded code, I don&#8217;t want to read (or write) another clever Fibonacci number generator, or another theoretical explanation of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=91&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The &#8220;BuilderBuilder&#8221; posts will be my exploration of one question: &#8220;What does Functional Programming mean to me as a practicing OO software developer?&#8221; Despite contemporary buzz about how FP will save the world from bugs and single-threaded code, I don&#8217;t want to read (or write) another clever Fibonacci number generator, or another theoretical explanation of category theory and monads. I want to see what FP is like when it sits down the aisle from me in work clothes.</p>
<p>To pursue that goal, I&#8217;m going to explore a programming problem that meets these criteria:</p>
<ol>
<li>Its purpose would be recognizable to a working programmer.</li>
<li>It is large enough that a clever one-liner won&#8217;t suffice.</li>
<li>It isn&#8217;t (consciously) chosen to cater to either OO or FP.</li>
<li>It will require me to try things outside my daily comfort zone.</li>
</ol>
<p>That last point means that I&#8217;ll make mistakes and do stupid things. I hope the record, warts and all, helps someone else (including me, later on) avoid them.</p>
<p>The problem I&#8217;ve chosen is a specific code generation task, which I&#8217;ll describe in the next post. There is a wide range of opinions on source code generation, but source code is one thing that any programmer understands, regardless of other application domain specialization.</p>
<p>Because my goal is exploration, and I&#8217;m starting with unequal familiarity with the languages I&#8217;ll use, the code almost certainly won&#8217;t be idiomatic or make best use of all available libraries. In fact, I&#8217;ll probably do more than necessary &#8220;by hand&#8221; to avoid bias toward more familiar tools. I may be able to address some of that along the way, based on accumulated experience (or helpful feedback, hint, hint&#8230; <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>OK, enough meta-talk. Next up, <a href="http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/">the task</a>.</p>
Posted in BuilderBuilder, functional programming, programming languages, tutorial  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=91&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>
	</item>
		<item>
		<title>Jared Richardson at Memphis JUG</title>
		<link>http://joelneely.wordpress.com/2009/04/24/jared-richardson-at-memphis-jug/</link>
		<comments>http://joelneely.wordpress.com/2009/04/24/jared-richardson-at-memphis-jug/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 02:18:43 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MJUG]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=86</guid>
		<description><![CDATA[I&#8217;ve been very happy with the progress of the Memphis Java Users&#8217; Group under Matt Stine&#8217;s leadership, and last night kicked it up a notch, with a visit by Jared Richardson, author of Career 2.0 and Ship It!, and NFJS speaker! 
Jared&#8217;s talk on refactoring your career was full of practical advice, engagingly presented &#8230;
Just [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=86&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve been very happy with the progress of the <a href="http://www.memphisjug.org/">Memphis Java Users&#8217; Group</a> under <a href="http://www.mattstine.com/">Matt Stine</a>&#8217;s leadership, and last night kicked it up a notch, with a visit by <a href="http://www.jaredrichardson.net/">Jared</a> <a href="http://agileartisans.com/main">Richardson</a>, author of <a href="http://www.lulu.com/content/5925115"><em>Career 2.0</em></a> and <a href="http://www.pragprog.com/titles/prj/ship-it"><em>Ship It!</em></a>, and <a href="http://www.nofluffjuststuff.com/conference/speaker/jared_richardson.html">NFJS speaker</a>! </p>
<p>Jared&#8217;s talk on <a href="http://career20.blogspot.com/">refactoring your career</a> was full of practical advice, engagingly presented &#8230;</p>
<blockquote><p><em><strong>Just stop right there!</strong> This is your Second-Guessing-Self speaking. Do you realize that you&#8217;re starting to gush? Even worse, you&#8217;re breaking my adjective-per-paragraph rule. Don&#8217;t tell your readers what to think! Just give them the facts and let them draw their own conclusions.</em></p>
<p>But S-G-S, I really enjoyed his talk, and&mdash;judging from their reactions&mdash;so did everyone else there. He even had <a href="http://www.vaco.com/memphis/team/eudelia-diane-tabulog">Diane</a> talking back to him, and you know how shy and retiring <b>she</b> is.</p>
<p><em>I&#8217;m more concerned about looking professional than about your enjoyment. Be objective!</em></p>
<p>Whatever&#8230;
</p></blockquote>
<p>Jared covered a rich list of specific suggestions for developing one&#8217;s writing and speaking skills; he also discussed a variety of contributions to open-source projects as a way to build visibility, reputation, and connections. These topics were all illustrated with examples from his own career, such as how he wrote <a href="http://www.jaredrichardson.net/articles/junit-tutorial.html">the JUnit totorial</a> that&#8217;s a front-page <a href="http://www.google.com/search?q=junit+tutorial">Google result</a>.</p>
<p>The evening was rounded out with a discussion of goals and how to set and achieve them.</p>
<p>I have to say that hearing Jared was one the most enjoyable&mdash;but effective&mdash;kick in the seat of the pants I&#8217;ve had in quite a while! (Take <b>that</b>, S-G-S!)</p>
<p>Those who heard him speak (especially his advice on writing <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) will understand when I say, &#8220;Thanc yew, Jerrid Rytszchurdsen!&#8221;</p>
Posted in Java, MJUG  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=86&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/04/24/jared-richardson-at-memphis-jug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>
	</item>
		<item>
		<title>On closed range boundaries</title>
		<link>http://joelneely.wordpress.com/2009/03/13/on-closed-range-boundaries/</link>
		<comments>http://joelneely.wordpress.com/2009/03/13/on-closed-range-boundaries/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 13:07:25 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=84</guid>
		<description><![CDATA[Programmers often need to talk about ranges of values (whether or not the language at hand supports the concept explicitly). For example, given the Java array
String[] streetLines = new String[3];
an index i must satisfy
0 &#8804; i &#60; 3 (in normal Mathematical notation)
or
0 &#60;= i &#38;&#38; i &#60; 3 (in Java notation)
to be a valid index [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=84&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Programmers often need to talk about ranges of values (whether or not the language at hand supports the concept explicitly). For example, given the Java array</p>
<blockquote><p><code>String[] streetLines = new String[3];</code></p></blockquote>
<p>an index <code>i</code> must satisfy</p>
<blockquote><p>0 &le; <code>i</code> &lt; 3 (in normal Mathematical notation)</p></blockquote>
<p>or</p>
<blockquote><p><code>0 &lt;= i &amp;&amp; i &lt; 3</code> (in Java notation)</p></blockquote>
<p>to be a valid index on <code>streetLines</code>. But how does one read those expressions aloud?</p>
<p>I learned to read <code>&lt;=</code> and <code>&le;</code> as &#8220;at most&#8221; and <code>&gt;=</code> and <code>&ge;</code> as &#8220;at least&#8221; from Dijkstra. Those readings have at least two benefits:</p>
<ol>
<li><b>They are quite natural.</b> For example, the assertion<br />
<blockquote><p><i>there are at least six eggs left in the carton</i></p></blockquote>
<p>is at least as obvious in normal speech as the equivalent phrase</p>
<blockquote><p><i>there are six or more eggs in the carton</i></p></blockquote>
<p>and is probably <b>much</b> more obvous than other equivalent phrases, such as</p>
<blockquote><p><i>the number of eggs left in the carton is greater than or equal to six</i></p></blockquote>
<p>or</p>
<blockquote><p><i>there are more than five eggs left in the carton</i></p></blockquote>
<p>even though we&#8217;ve seen the equivalent of those in code many times.<br />&nbsp;</li>
<li><b>They avoid introducing extraneous logical operators.</b> The phrasing<br />
<blockquote><p><i>less than <b>or</b> equal to</i></p></blockquote>
<p>seems more complex than</p>
<blockquote><p><i>at most</i></p></blockquote>
<p>because it implies the use of disjunction (logical &#8220;<b>or</b>&#8220;) in its evaluation. The other equivalent phrase</p>
<blockquote><p><i><b>not</b> greater than</i></p></blockquote>
<p>introduces negation into a simple comparison.
</ol>
<p>As a tiny &#8220;easter egg&#8221;, look for uses of &#8220;at least&#8221; <em>en passant</em> in the preceding few sentences! If they went by without being obvious, perhaps that serves as evidence of point 1 above.  <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
Posted in Mathematics, programming languages  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=84&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/03/13/on-closed-range-boundaries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8220;Linguistics&#8221; vs. Mathematics?</title>
		<link>http://joelneely.wordpress.com/2009/03/13/linguistics-vs-mathematics/</link>
		<comments>http://joelneely.wordpress.com/2009/03/13/linguistics-vs-mathematics/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 12:49:09 +0000</pubDate>
		<dc:creator>joelneely</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://joelneely.wordpress.com/?p=80</guid>
		<description><![CDATA[I happened across an interesting post on Chris Okasaki&#8217;s blog, titled Less than vs Greater than. Let me suggest that you read it before continuing here.
I would paraphrase his point about errors he observed in students&#8217; programs as follows:
A student who writes an expression such as
expL &#60; expR
often appears to lock on the concept of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=80&subd=joelneely&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I happened across an interesting post on <a href="http://okasaki.blogspot.com/">Chris Okasaki&#8217;s blog</a>, titled <a href="http://okasaki.blogspot.com/2008/09/less-than-vs-greater-than.html">Less than vs Greater than</a>. Let me suggest that you read it before continuing here.</p>
<p>I would paraphrase his point about errors he observed in students&#8217; programs as follows:</p>
<blockquote><p>A student who writes an expression such as</p>
<blockquote><p><code><i>expL</i> &lt; <i>expR</i></code></p></blockquote>
<p>often appears to lock on the concept of &#8220;something being smaller&#8221; and then become mentally stuck on &#8220;dealing with smaller stuff&#8221;, even if that is not appropriate for the meaning of <code><i>expL</i></code> and <code><i>expR</i></code> at that point in the program.</p></blockquote>
<p>He illustrates his point very nicely with a flawed binary search.</p>
<p>For quite some time I&#8217;ve tended to write expressions of the form</p>
<blockquote><p><code>lowerBound &lt;= someVar &amp;&amp; someVar &lt; upperBound</code></p></blockquote>
<p>to express that <code>someVar</code> is within the half-open range</p>
<blockquote><p><code>[lowerBound..upperBound)</code></p></blockquote>
<p>e.g. array subscripts that must be at least zero, but less than the length of the array. The visual hint of placing the variable textually between the limiting values seemed nicely mnemonic to me. (From there, it has also seemed natural to experiment with preferring left-to-right, lesser-to-greater ordering consistently in comparison expressions, although I&#8217;m not suggesting that as a universal coding convention).</p>
<p>However, I was quite surprised by the first comment, which described as &#8220;linguistically wrong&#8221; the common C-language-based idiom</p>
<blockquote><p><code>if (5 == i) </code>&#8230;</p></blockquote>
<p>(intended to catch typos involving only a single equal sign). The commenter said</p>
<blockquote><p>Linguistically that&#8217;s wrong. You&#8217;re not testing <code>5</code> to see if it&#8217;s equal to <code>i</code>, you&#8217;re testing <code>i</code>.</p></blockquote>
<p>which implies an asymmetrical interpretation of equality as being a &#8220;test&#8221; on its left-hand value!</p>
<p>By ignoring the fact that <code>==</code> is simply an assertion that its operands are equal, that interpretation fails on many completely valid cases, such as</p>
<blockquote><p><code>(a + 1 == b - 1)</code></p></blockquote>
<p>and the first clause</p>
<blockquote><p><code>lowerBound &lt;= someVar</code></p></blockquote>
<p>of the &#8220;within bounds&#8221; clich&eacute; above.</p>
<p>That misinterpretation of <code>==</code> seems consistent with a variety of incorrect or awkward uses of boolean expressions widely seen. Many of us have probably seen code (or read blog posts about code) resembling:</p>
<blockquote><pre><code>boolean fooIsEmpty;
...
if (foo == null) {
    fooIsEmpty = true;
} else {
    if (foo.length() == 0) {
        fooIsEmpty = true;
    } else {
        fooIsEmpty = false;
    }
}
</code></pre>
</blockquote>
<p>I suspect two culprits behind this kind of abuse:</p>
<ol>
<li>Failure to include the right kind and amount of Mathematics in the education of a programmer, and</li>
<li>The C/FORTRAN use of <code>=</code> for assignment.</li>
</ol>
<p>With respect to point 1, I believe that Boolean Algebra is simply a fundamental skill for programming. The ability to manipulate and understand boolean expressions equally important as the ability to deal with numeric expressions for most programming of the kind I see daily. It is understandable that a programmer whose training never addressed boolean expressions except in the context of <code>if(</code>&#8230;<code>)</code> or <code>while(</code>&#8230;<code>)</code> would be uncomfortable with other uses, but that&#8217;s a problem to be solved, not a permanent condition to be endured.</p>
<p>Regarding point 2, much of what I&#8217;ve read or experienced supports the idea that FORTRAN&mdash;and later, C&mdash;were more commonly used in the US than Algol or other alternatives due to political, cultural, and commercial issues rather than technical ones.  (I recommend Dijkstra&#8217;s &#8220;<a href="http://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1024.html">A new science, from birth to maturity</a>&#8221; and Gabriel&#8217;s &#8220;<a>Worse</a> <a href="http://www.codinghorror.com/blog/archives/000047.html">is</a> <a href="http://en.wikipedia.org/wiki/Worse_is_better">Better</a>&#8221; as good starting points.)</p>
<p>Whether that conclusion is valid or flawed, the decision to use <code>=</code> for the (asymmetrical!) &#8220;assignment&#8221; operation immediately creates two new problems:</p>
<ol>
<li>The need to express the &#8220;equality test&#8221; in a way that won&#8217;t be confused with assignment; and</li>
<li>The risk that users of the language become confused about the symmetry of equality, due to guilt by association with the &#8220;assignment&#8221; operator.</li>
</ol>
<p>Algol&mdash;and its descendants, including Pascal&mdash;avoided those problems by using the asymmetrical <code>:=</code> for assignment. The <code>!</code> character is used as an operator or suffix in other languages to express destructive value-setting. But Java, C#, and others, have followed the FORTRAN/C convention, with the predictable effects.</p>
<p>Given such far-reaching consequences from a single character in the source code, I&#8217;m reminded again how important it is to make good choices in coding style, API design, and other naming contexts.</p>
<hr />
<p><b>Postscript:</b></p>
<p>The left-to-right ordering of the number line has some interesting cultural and even individual baggage. I recall reading about a mathematician whose personal mental model was of negative values being behind him and positive values receding into the distance in front of him.</p>
<p>I believe it was in connection with <a href="http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/">the Dutch National Flag Problem</a> that Esdger Dijkstra wrote about a student whose native language was written right-to-left. While other students had designed programs with indices increasing from zero, that student had produced an equally-valid program with an index that decreased from the maximal value.</p>
Posted in education, Mathematics, programming languages  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joelneely.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joelneely.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joelneely.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joelneely.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joelneely.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joelneely.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joelneely.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joelneely.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joelneely.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joelneely.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joelneely.wordpress.com&blog=2876257&post=80&subd=joelneely&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://joelneely.wordpress.com/2009/03/13/linguistics-vs-mathematics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3ae6a1b8d708b79b6b85ecc365266a4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">joelneely</media:title>
		</media:content>
	</item>
	</channel>
</rss>