<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1.2" -->
<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/"
	>

<channel>
	<title>Exploration Through Example</title>
	<link>http://www.exampler.com/blog</link>
	<description>Example-driven development, Agile software development, testing, Ruby, and other things of interest to Brian Marick</description>
	<pubDate>Sat, 07 Jan 2012 17:25:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>
	<language>en</language>
			<item>
		<title>Some thoughts on classes after 18 months of Clojure</title>
		<link>http://www.exampler.com/blog/2012/01/07/some-thoughts-on-classes-after-18-months-of-clojure/</link>
		<comments>http://www.exampler.com/blog/2012/01/07/some-thoughts-on-classes-after-18-months-of-clojure/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 16:51:24 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2012/01/07/some-thoughts-on-classes-after-18-months-of-clojure/</guid>
		<description><![CDATA[I had some thoughts about classes that wouldn&#8217;t fit into a talk I&#8217;m building about functional programming in Ruby, so I recorded them as a video. 
Topics:



Using hashes instead of classes.




Classes as a documentation tool&#8212;specifically, as a way of making functions easy to find.




Preferring module inclusion to subclassing (which is akin to preferring adjectives to [...]]]></description>
			<content:encoded><![CDATA[<p>I had some thoughts about classes that wouldn&#8217;t fit into a talk I&#8217;m building about <a href="http://geekfest.gathers.us/events/geekfest-functional-programming-in-ruby">functional programming in Ruby</a>, so I recorded them as <a href="http://vimeo.com/34522837">a video</a>. </p>
<p>Topics:</p>
<ul>
<li>
<p>
Using hashes instead of classes.
</p>
</li>
<li>
<p>
Classes as a documentation tool&#8212;specifically, as a way of making functions easy to find.
</p>
</li>
<li>
<p>
Preferring module inclusion to subclassing (which is akin to preferring adjectives to nouns as a way of organizing the documentation of verbs). (Vaguely similar to <a href="http://t.co/7wqEQTEM">duck-typing in Haskell</a>.)
</p>
</li>
<li>
<p>
Object dot notation as a more readable way of writing function composition. (Similar to the motivation for the <code>-&gt;</code> macro in Clojure or <a href="http://hackage.haskell.org/trac/haskell-prime/wiki/TypeDirectedNameResolution">type-directed name resolution in Haskell</a>.)
</p>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2012/01/07/some-thoughts-on-classes-after-18-months-of-clojure/feed/</wfw:commentRss>
		</item>
		<item>
		<title>On mutable state</title>
		<link>http://www.exampler.com/blog/2011/12/29/on-mutable-state/</link>
		<comments>http://www.exampler.com/blog/2011/12/29/on-mutable-state/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 16:37:38 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/12/29/on-mutable-state/</guid>
		<description><![CDATA[I&#8217;m working on a talk called &#8220;Functional Programming for Ruby Programmers&#8221;. While doing so, I&#8217;ve somewhat changed my opinion about mutable state. Here&#8217;s my argument, for your commentary. Being new, it&#8217;s probably at best half-baked.



Back in the early days of AIDS (the disease), I remember a blood-supply advocate saying &#8220;You&#8217;ve slept with everyone that everyone [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a talk called &#8220;Functional Programming for Ruby Programmers&#8221;. While doing so, I&#8217;ve somewhat changed my opinion about mutable state. Here&#8217;s my argument, for your commentary. Being new, it&#8217;s probably at best half-baked.</p>
<ol>
<li>
<p>
Back in the early days of AIDS (the disease), I remember a blood-supply advocate saying &#8220;You&#8217;ve slept with everyone that everyone you&#8217;ve slept with ever slept with.&#8221;
</p>
<p>
The nice thing about immutable state is that it stays virginal and knowable. It is what it was when it was created. With mutable state, it&#8217;s more like &#8220;Your code might be infected by any code that ever touched your data before (or after!) you first hooked up with it.&#8221;
</p>
<p>
However, I don&#8217;t personally find that a huge problem in programming, debugging, or understanding code. There are other problems I&#8217;d rather see fixed first.
</p>
</li>
<li>
<p>
It&#8217;s often said that &#8220;code with immutable state is easier to reason about&#8221;. I realized a long time ago that there&#8217;s some sleight-of-common-language going on there, like the way people who wanted code to be less branchy got rhetorical leverage by renaming branchiness &#8220;<a href="http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/chapter2.htm">complexity</a>&#8220;. </p>
<p>In the claim, &#8220;reason about&#8221; is (I believe) being taken to be synonymous with &#8220;prove theorems about&#8221;. <i>Pace</i> a whole long trend in artificial intelligence, I don&#8217;t believe theorem proving is the <a href="http://www.amazon.com/Why-Choose-This-Book-Decisions/dp/0525949828">basis for</a>, nor a <a href="http://www.amazon.com/Sources-Power-People-Make-Decisions/dp/0262611465">good analogy to</a>, reasoning. Thus I took the &#8220;reason about&#8221; statement to be a sort of solipsistic wankery from people with <a href="http://en.wikipedia.org/wiki/ML_(programming_language)">roots in the theorem-proving community</a>, not an argument that should sway me, Pragmatic Man!, who left the world of proofs-of-design-correctness <a href="http://www.computer.org/portal/web/csdl/abs/proceedings/sp/1983/0467/00/04670150abs.htm">around 1984</a>.
</p>
<p>
What I&#8217;d forgotten is that <i>optimization</i> is theorem proving. One can only optimize if there&#8217;s a proof that the optimized code computes the same result as the original. If you consider the desire to implement, say, lazy sequences efficiently, you see how the guarantees that immutability gives are important. Ditto for automatically spreading work to multiple cores.
</p>
</li>
<li>
<p>
So:
</p>
<p>
My previous attitude was pretty much &#8220;Yeah, I have a preference for avoiding mutable state. Being immutable isn&#8217;t a <i>huge</i> win, but as long as I have people providing me with data structures like <a href="http://www.exampler.com/blog/2010/09/01/editing-trees-in-clojure-with-clojurezip/">zippers</a>, it&#8217;s not really any harder than fooling with mutable state. Still, I don&#8217;t see why I need an immutable <i>language</i>. If I don&#8217;t want to mutate my hashmaps, I just won&#8217;t mutate my hashmaps.&#8221;
</p>
<p>
My new attitude is: &#8220;Oh! I see why I need an immutable language.&#8221;
</p>
</li>
</ol>
<p>I still don&#8217;t make a huge deal about immutability. Its benefit is greater than its cost, sure, but it&#8217;s not the Thing That Will Save Us.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/12/29/on-mutable-state/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Here are three Ruby functions&#8230;</title>
		<link>http://www.exampler.com/blog/2011/12/27/here-are-three-ruby-functions/</link>
		<comments>http://www.exampler.com/blog/2011/12/27/here-are-three-ruby-functions/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 01:36:06 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/12/27/here-are-three-ruby-functions/</guid>
		<description><![CDATA[Here are three Ruby functions. Each solves this problem: &#8220;You are given a starting and ending date and an increment in days. Produce all incremental dates that don&#8217;t include the starting date but may include the ending date. More formally: produce a list of all the dates such that  for some n >= 1, [...]]]></description>
			<content:encoded><![CDATA[<p>Here are three Ruby functions. Each solves this problem: &#8220;You are given a starting and ending date and an increment in days. Produce all incremental dates that don&#8217;t include the starting date but may include the ending date. More formally: produce a list of all the dates such that <code> for some n >= 1, date = starting_date + (increment * n) &#038;&#038; date < = ending_date</code>. </p>
<p>Solution 1:</p>
<script src="https://gist.github.com/1525608.js?file=gistfile1.rb"></script>
<p>Solution 2: </p>
<script src="https://gist.github.com/1525613.js?file=gistfile1.txt"></script>
<p>Solution 3 depends on a </code><code>lazily</code> function that produces an unbounded list from a starting element and a next-element function. Here&#8217;s a use of <code>lazily</code>:</p>
<script src="https://gist.github.com/1525622.js?file=gistfile1.rb"></script>
<p>As a lazy sequence, <code>integers</code> both (1) doesn&#8217;t do the work of calculating the <code>i</code>th element unless a client of <code>integers</code> asks for it, and also (2) doesn&#8217;t waste effort calculationg any intermediate values more than once.</p>
<p>Solution 3:</p>
<script src="https://gist.github.com/1525682.js?file=gistfile1.rb"></script>
<p>The third solution seems &#8220;intuitively&#8221; better to me, but I&#8217;m having difficulty explaining why. </p>
<p>The first solution fails on three aesthetic grounds:</p>
<ul>
<li>
<p>
It lacks <b>decomposability</b>. There&#8217;s no piece that can be ripped out and used in isolation. (For example, the body of the loop both creates a new element and updates an intermediate value.)
</p>
</li>
<li>
<p>
It lacks <b>flow</b>. It&#8217;s pleasing when you can view a computation as flowing a data structure through a series of functions, each of which changes its &#8220;shape&#8221; to convert a lump of coal into a diamond.
</p>
</li>
<li>
<p>
It has <b>wasted motion</b>: it puts an element at the front of the array, then throws it away. (Note: you can eliminate that by having <code>current</code> start out as <code>exclusive+increment</code> but that code duplicates the later +=increment. Arguably, that duplicated increment-date action is wasted (programmer) motion, in the sense that the same action is done twice. (Or: <a href="http://www.artima.com/intv/dry.html">don&#8217;t repeat yourself</a> / <a href="http://xprogramming.com/classics/expemergentdesign/">Eliminate duplication</a>.))
</p>
</li>
</ul>
<p>The second solution has flow of values through functions, but it wastes a lot of motion. A bunch of dates are created, only to be thrown away in the next step of the computation. Also, in some way I cannot clearly express, it seems wrong to stick the <code>inclusive_end</code> between the <code>exclusive_start</code> and the <code>increment</code>, given that the latter two are what was originally presented to the user and the <code>inclusive_end</code> is a user choice. (Therefore shouldn&#8217;t the <code>exclusive_start</code> and <code>increment</code> be more visually bound together than this solution does?)</p>
<p>The third solution &#8230;</p>
<ul>
<li>
<p>
&#8230; is decomposable: the sequence of dates is distinct from the decision about which subset to use. (You could, for example, pass in the whole lazy sequence instead of a <code>exclusive_start/increment</code> pair, something that couldn&#8217;t be done with the other solutions.)
</p>
</li>
<li>
<p>
&#8230; eliminates wasted work, in that only the dates that are required are generated. (Well, it does store away a first date &#8212; <code>excluded_start</code> &#8212; that is then dropped. But it doesn&#8217;t create an excess new date.)
</p>
</li>
<li>
<p>
&#8230; has the same feel of a data structure flowing through functions that #2 has.
</p>
</li>
</ul>
<p>So: #3 seems best to me, but the advantages over the other two seem unconvincing (especially given that programmers of my generation are likely to see closure-evaluation-requiring-heap-allocation-because-of-who-knows-what-bound-variables as scarily expensive). </p>
<p>Have you better arguments? Can you refute my arguments?</p>
<p>I&#8217;m trying to show the virtues of a lazy functional style. Perhaps this is a bad example? [It&#8217;s a real one, though, where I really do prefer the third solution.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/12/27/here-are-three-ruby-functions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>TDD Workflow (Sinatra / Haml / jQuery) Part 1</title>
		<link>http://www.exampler.com/blog/2011/12/17/tdd-workflow-sinatra-haml-jquery-part-1/</link>
		<comments>http://www.exampler.com/blog/2011/12/17/tdd-workflow-sinatra-haml-jquery-part-1/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 23:08:25 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[agile]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/12/17/tdd-workflow-sinatra-haml-jquery-part-1/</guid>
		<description><![CDATA[Introduction
This is a draft. Worth continuing the series? Let me know. 
Critter4Us is a webapp used to schedule teaching animals at the University of Illinois Vet School. Its original version was a Ruby/Sinatra application with a Cappuccino front end. Cappuccino lets you write desktop-like webapps using a framework modeled after Apple&#8217;s Cocoa. I chose it [...]]]></description>
			<content:encoded><![CDATA[<p><b>Introduction</b></p>
<p><i>This is a draft. Worth continuing the series? <a href="mailto:marick@exampler.com">Let me know.</a> </i></p>
<p>Critter4Us is a webapp used to schedule teaching animals at the University of Illinois Vet School. Its original version was a Ruby/<a href="http://www.sinatrarb.com/">Sinatra</a> application with a <a href="http://cappuccino.org/">Cappuccino</a> front end. Cappuccino lets you write desktop-like webapps using a framework modeled after Apple&#8217;s <a href="http://developer.apple.com/technologies/mac/cocoa.html">Cocoa</a>. I chose it for two reasons: it made it easy to test front-end code headlessly (which was harder back then than it is now), and it let me reuse my <a href="http://pragprog.com/book/bmrc/programming-cocoa-with-ruby">RubyCocoa experience</a>.</p>
<p>Earlier this year, I decided it was time for another bout of experimentation. I decided to switch from Cappuccino to jQuery, <a href="http://jashkenas.github.com/coffee-script/">Coffeescript</a>, <a href="http://haml-lang.com/">Haml</a> because I thought they were technologies I should know and because they&#8217;d force me to develop a new TDD workflow. I&#8217;d never gotten comfortable with the testing&#8212;or for that matter, any of the design&#8212;of &#8220;traditional&#8221; front-end webapp code and the parts of the backend from the controller layer up. </p>
<p>I now think I&#8217;ve reached a plateau at which I&#8217;m temporarily comfortable, so this is a good time to report. Other people might find the approach and the tooling useful. And other people might explain places where my inexperience has led me astray.</p>
<p><b>The example</b></p>
<p>Suppose an instructor wants to reserve the horses Genesis and Guicho each Tuesday morning from October 8 through the 29th. For historical reasons, that&#8217;s done by making a reservation for the 8th and then making three copies. Here&#8217;s what that page looks like today:</p>
<p><img src="http://www.exampler.com/images/reservation.png"/></p>
<p>(As you can see, I haven&#8217;t gotten to making the UI attractive yet.)</p>
<p>After the stop date is chosen, a click of the &#8220;Duplicate&#8221; button causes three successive Ajax calls to be sent to the server. (That&#8217;s a bit gratuitous - I just like the idea of the UI showing each reservation &#8220;coming to life&#8221; in turn.)</p>
<p>Ideally, Genesis and Guicho will be reserved for each Tuesday. However, it&#8217;s possible that one of them has already been reserved for the 22nd. In that case, the reservation is still made, but the animal is dropped from it. The user has to edit that reservation to pick a replacement animal. </p>
<p><b>Testing the Haml</b></p>
<p>I usually test in an outside-in, mock-heavy style derived from <i><a href="http://www.growing-object-oriented-software.com/">Growing Object-Oriented Software</a></i> (GOOS). &#8220;Derived&#8221;, naturally, means I depart from GOOS in my very first step: I <a href="http://www.exampler.com/blog/2010/01/08/some-preliminary-thoughts-on-end-to-end-testing-in-growing-object-oriented-software/">avoid automating end-to-end tests</a>. For my small-scale purposes, I don&#8217;t find GUI tests (<a href="http://watir.com/">Watir</a>, <a href="http://seleniumhq.org/">Selenium</a>, etc.) worth the price. To be clear: I <i>think about</i> GOOS-style tests, and I <i>execute them</i>&#8212;I <a href="http://www.exampler.com/blog/2008/12/12/interview-alternatives-to-automated-acceptance-test-driven-design/">just don&#8217;t <i>automate</i> them</a>.</p>
<p>Having thought of an end-to-end test for a particular user task, the next step is to sketch out a page that supports it. I sketch it out in Haml to be the beginnings of the page above.</p>
<p>My page testing is driven by this observation: the user will use the same page for many different reservations. It has a constant part that&#8217;s independent of the reservation, and a part that depends on the particular reservation. I only write automated tests for the variable part. I test the constant part by looking at it, by looking at the generated HTML, and by trying the finished story by hand. </p>
<p>There are three parts of this particular page that vary:</p>
<ul>
<li>
<p>
There&#8217;s a snippet of text that reminds the user what reservation she&#8217;s working with.
</p>
</li>
<li>
<p>
There has to be a starting date used to set up the calendar.
</p>
</li>
<li>
<p>
There has to be some URI that&#8217;s used to <code>PUT</code> a new repetition into the system. That URI will look something like <code>/reservations/441/repetitions</code>.
</p>
</li>
</ul>
<p>Here&#8217;s the setup for tests to check those three claims:</p>
<script src="https://gist.github.com/1491503.js?file=gistfile1.rb"></script>
<ol>
<li>
<p>
Whenever I create a model object, I usually give it a <code>random</code> constructor that builds it with innocuous values unless I give it explicit ones. I&#8217;ll explain why I&#8217;m giving it only one value below.
</p>
</li>
<li>
<p>
I <i>hate</i> the <a href="http://www.popsci.com/scitech/article/2009-06/quantum-entaglement">spooky action at a distance</a> of setting instance variables in a controller in order to have some effect way off in another file written in a different language. I&#8217;d much rather have a nice explicit function-call interface and a <a href="http://erector.rubyforge.org/userguide.html">builder</a> or <a href="https://github.com/cgrand/enlive">transformation</a> approach. Most of the world thinks differently, so I concede. However, I do try to come closer to a function-call interface by passing in locals instead of setting instance variables.
</p>
</li>
<li>
<p>
Passing in a whole <code>Reservation</code> model object makes me queasy for reasons <a href="http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html">Bob Martin has explained</a>.  I&#8217;m intruding the model into the view rather than passing in just what the view needs. This is a consequence of some amount of laziness after bowing to the peer pressure to use partials. I do make <i>some</i> effort to separate presentation-relevant data from model-relevant objects, as you&#8217;ll see later.
</p>
</li>
<li>
<p>
In order <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">not to repeat myself</a>, I make one Ruby file the authority over all URIs and links. Therefore, any URI gets passed into the HAML. In pages like these, which exist to aid some specific user task and have a single URI that they use when the appropriate data has been supplied by the user, I call that single URI the &#8220;fulfillment&#8221; link.
</p>
</li>
</ol>
<p>That given, here&#8217;s the test in which I claim that a reservation summary appears on the page:</p>
<script src="https://gist.github.com/1491555.js?file=gistfile1.rb"></script>
<p>I use <a href="https://github.com/thoughtbot/shoulda">shoulda</a> because I like nested contexts and strings to name tests, but I don&#8217;t need the rest of Rspec. I use <a href="http://assert2.rubyforge.org/">Assert{2.0}</a> because I find Hamcrest-style matchers too wordy and Assert{2.0} gives me usefully detailed failure messages. (It prints every subexpression of a false expression. <a href="https://github.com/sconover/wrong">Wrong</a> is a more recent implementation of this idea.)</p>
<p>The actual reservation summary is created with a partial. By checking for &#8216;Dr. Dawn&#8217;, this test claims the partial has been &#8220;called&#8221;. (It&#8217;s thus a clumsier and less explicit example of a mockish test.) I&#8217;m testing only that here because I want the freedom to what the summary looks like without having to change this test.</p>
<p>The next test claims that a correct Javascript date exists in the HTML (presumably in some Javascript code):</p>
<script src="https://gist.github.com/1491622.js?file=gistfile1.rb"></script>
<p>Notice that the check is, again, minimalist. The test isn&#8217;t about how the date is used, just that it exists. Peeking ahead, it&#8217;s actually used like this:</p>
<script src="https://gist.github.com/1491630.js?file=gistfile1.js"></script>
<p>There&#8217;s a gap in the test coverage, in that no automated test checks that this initialization code is correct. Pretty low risk, given that it&#8217;s straight-line code that cannot avoid execution whenever the page is loaded. So long as I try this page out once, and remember to try the page again if I change the initialization code, I should be safe.</p>
<p>The final test is this:</p>
<script src="https://gist.github.com/1491638.js?file=gistfile1.rb"></script>
<p><code>assert_text_has_attributes</code> is part of a thin wrapper on top of the HTML parsing part of <a href="https://github.com/jnicklas/capybara">Capybara</a> (<i>not</i> the integration testing part). I used to use <a href="http://assert2.rubyforge.org/assert2_xpath.html">assert_xhtml</a>, a sweet package, but it&#8217;s dead in Ruby 1.9 and I haven&#8217;t found another one like it. <a href="mailto:marick@exampler.com">Let me know</a> if you have one.</p>
<p>The Haml code that passes these tests looks like this:</p>
<script src="https://gist.github.com/1491700.js?file=gistfile1.haml"></script>
<p>The <code>&lt;head&gt;</code> is, annoyingly, generated by another action at a distance, with a <code>layout.haml</code> file that gets wrapped around the above code. That file uses the third of the &#8220;parameters&#8221;:</p>
<script src="https://gist.github.com/1491716.js?file=gistfile1.haml"></script>
<p><b>What now?</b></p>
<p>The static page and the acceptance test I have in mind call for some Javascript. That needs to be tested into existence.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/12/17/tdd-workflow-sinatra-haml-jquery-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Anarchy of Evasion</title>
		<link>http://www.exampler.com/blog/2011/12/15/anarchy-of-evasion/</link>
		<comments>http://www.exampler.com/blog/2011/12/15/anarchy-of-evasion/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 01:30:25 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/12/15/anarchy-of-evasion/</guid>
		<description><![CDATA[&#8220;The anarchy of evasion&#8221; is a term that (I think) I read a few years ago. I occasionally search for it, but I&#8217;ve never been able to find anything about it again. As I recall, the idea is a reaction to the fact that an anarchist movement that forms an independent new society or transforms [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;The anarchy of evasion&#8221; is a term that (I think) I read a few years ago. I occasionally search for it, but I&#8217;ve never been able to find anything about it again. As I recall, the idea is a reaction to the fact that an anarchist movement that forms an independent new society or transforms an existing society is&#8230; hard. So an alternative is to live within an existing society but try to make as many as possible of your activities and interactions run according to the principles you wished the whole society ran with. You behave as you wish, but in a way and context that increases the chance you won&#8217;t be noticed and therefore stopped.</p>
<p>The idea appeals to me, perhaps because I&#8217;ve always liked the idea of complex and interesting things happening unnoticed in the cracks. Probably a result of reading <a href="http://en.wikipedia.org/wiki/The_Littles"><i>The Littles</i></a> as a kid and books like <a href="http://www.depressedmetabolism.com/2009/05/29/youre-all-alone/"><i>You&#8217;re All Alone</i></a> and <a href="http://en.wikipedia.org/wiki/Neverwhere"><i>Neverwhere</i></a> later.</p>
<p>Parallels with things like open source, <a href="http://en.wikipedia.org/wiki/Intentional_community">intentional communities</a>, <a href="http://en.wikipedia.org/wiki/Squatting">squatting</a>, and <a href="http://en.wikipedia.org/wiki/Freaks_and_Geeks">freaks and geeks</a> in US high schools are left as an exercise for the reader.</p>
<p>If anyone has links to the idea, <a href="mailto:marick@exampler.com">let me know</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/12/15/anarchy-of-evasion/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using functional style in a Ruby webapp</title>
		<link>http://www.exampler.com/blog/2011/10/31/using-functional-style-in-a-ruby-webapp/</link>
		<comments>http://www.exampler.com/blog/2011/10/31/using-functional-style-in-a-ruby-webapp/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 06:54:45 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/10/31/using-functional-style-in-a-ruby-webapp/</guid>
		<description><![CDATA[Motivation
Consider a Ruby backend that communicates with its frontend via JSON. It sends (and perhaps receives) strings like this:

Let&#8217;s suppose it also communicates with a relational database. A simple translation of query results into Ruby looks like this:

(I&#8217;m using the Sequel gem to talk to Postgres.)
On the face of it, it seems odd for our [...]]]></description>
			<content:encoded><![CDATA[<p><b>Motivation</b></p>
<p>Consider a Ruby backend that communicates with its frontend via JSON. It sends (and perhaps receives) strings like this:</p>
<script src="https://gist.github.com/1327796.js?file=gistfile1.rb"></script>
<p>Let&#8217;s suppose it also communicates with a relational database. A simple translation of query results into Ruby looks like this:</p>
<script src="https://gist.github.com/1327804.js?file=gistfile1.rb"></script>
<p>(I&#8217;m using the <a href="http://sequel.rubyforge.org/">Sequel</a> gem to talk to Postgres.)</p>
<p>On the face of it, it seems odd for our code to receive dumb hashes and arrays, laboriously turn them into model objects with rich behavior, fling some messages at them to transform their state, and then convert the resulting object graph back into dumb hashes and arrays. There are strong historical reasons for that choice&#8212;see <a href="http://martinfowler.com/">Fowler</a>&#8217;s <i><a href="http://martinfowler.com/books.html#eaa">Patterns of Enterprise Application Architecture</a></i>&#8212;but I&#8217;m starting to wonder if it&#8217;s as clear a <i>default</i> choice as it used to be. Perhaps a functional approach could work well:</p>
<ul>
<li>
<p>
Functional programs focus on the flow of data through code, rather than on objects with changing state. The former seems more of a match for a typical webapp.
</p>
</li>
<li>
<p>
It&#8217;s common in functional languages to lean toward a few core datatypes&#8212;like hashes and arrays&#8212;that are operated on by a wealth of functions. We could skip the conversion step into objects. Rather than having to deal with the <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">leaky abstraction</a> of an object-relational mapping layer, we&#8217;d embrace the nature of our data.
</p>
</li>
</ul>
<p>Seems plausible, I&#8217;ve been thinking. However, I&#8217;ve never been wildly good at understanding the problems of an approach just <a href="http://incanter.org/images/misc/hammock-driven-dev.png">by thinking about it</a>. It&#8217;s more efficient for me to learn by doing. So I&#8217;ve decided to <a href="http://martinfowler.com/bliki/StranglerApplication.html">strangle</a> an application whose communication with its database is, um, labored.  </p>
<p>I&#8217;m going to concentrate on two things: </p>
<ul>
<li>
<p>
<i>Structuring the code</i>. More than a year of work on <a href="https://github.com/marick/Midje">Midje</a> has left me still unhappy about the organization of its code, despite my using <a href="http://www.raggedclown.com/">Kevin Lawrence</a>&#8217;s <a href="http://www.exampler.com/old-blog/2005/06/16/">guideline</a>: if you have trouble finding a piece of code, move it to where you first looked. I have some hope that Ruby&#8217;s structuring tools (classes, modules, <code>include</code>, etc.) will be useful.</p>
</li>
<li>
<p>
<i>Dependencies</i>. As you&#8217;ll see, I&#8217;ll be writing code with a lot of <a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx">temporal coupling</a>. Is that and <a href="http://en.wikipedia.org/wiki/Coupling_(computer_programming)#Types_of_coupling">other kinds of coupling</a> dooming me to a <a href="http://en.wikipedia.org/wiki/Intertwingularity">deeply intertwingled</a> mess that I can&#8217;t change safely or quickly?
</p>
</li>
</ul>
<p>This blog post is about where I stand so far, after adding just one new feature.</p>
<p><b>A path through the app</b></p>
<p><a href="https://github.com/marick/critter4us">Critter4us</a> is an app that&#8217;s used to reserve teaching animals at the University of Illinois vet school. Reserving animals is like reserving meeting rooms, but with some different business rules. For example, Boombird the horse doesn&#8217;t care if students practice bandaging on him every day. However, it would be inhumane to practice giving him injections every day, so that can be done at most twice a week. </p>
<p>The story I&#8217;ve been working on is one that makes a copy of an existing reservation but with a new &#8220;timeslice&#8221; (something like &#8220;January 1st through 3d, in the mornings&#8221;). Ideally, the same animals will be assigned to the copy, but that&#8217;s not always possible. If someone else has already reserved the animal for an overlapping timeslice, a new animal has to be found. Or if a procedure (like giving injections) would be within an animal&#8217;s &#8220;blackout period&#8221;, a new animal has to be found for it. For historical reasons, the reservation is made without the unusable animals and the user is alerted to edit it to add new ones.</p>
<p>Here&#8217;s the heart of the code for the feature:</p>
<script src="https://gist.github.com/1326261.js?file=gistfile1.rb"></script>
<p>The rest of this post will explain how that works and how it&#8217;s in a functional style. When I mention classes, I&#8217;ll link to the source.</p>
<p><b>FullReservation</b></p>
<p>The object model for the old program starts with a <i>reservation</i>, which contains information like &#8220;Who made the reservation?&#8221; and &#8220;For when?&#8221;. It also contains zero or more <i>groups</i>, each of which contains zero or more <i>uses</i>. A use links an animal to a procedure to be performed on it. I defined the class structure first, then mapped it onto a database schema, deliberately deferring any worries about efficiency:</p>
<script src="https://gist.github.com/1326270.js?file=gistfile1.rb"></script>
<p>The object-to-relational mapping library (<a href="http://sequel.rubyforge.org/">Sequel</a>) let me work with the data in a way that hid (&#8221;unflattened&#8221;) the table structure:</p>
<script src="https://gist.github.com/1326274.js?file=gistfile1.rb"></script>
<p>The existence of three tables is made somewhat implicit.</p>
<p>I&#8217;m replacing that old <code><a href="https://github.com/marick/critter4us/blob/functional/strangled-src/model/reservation.rb">Reservation</a></code> object with a new <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code>. In <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code>, I chose instead to make them explicit:</p>
<script src="https://gist.github.com/1326276.js?file=gistfile1.rb"></script>
<p>That notation is awkward to type and it doesn&#8217;t lend itself to the <a href="http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html">Symbol#to_proc hack</a>, so I follow Javascript by allowing dot notation as a pun for key lookup:</p>
<script src="https://gist.github.com/1326277.js?file=gistfile1.rb"></script>
<p><b>Namespacing</b></p>
<p>I&#8217;m supposedly doing this in a functional style, and the very first thing I&#8217;ve done is make a class? What&#8217;s up with that?</p>
<p>I have two reasons. First, I think having the order of function application flow left to right fits the (Western-language-speaker) perception that time flows from left to right and from top to bottom. That makes this:</p>
<script src="https://gist.github.com/1328061.js?file=gistfile1.rb"></script>
<p>&#8230; easier to read than this:</p>
<script src="https://gist.github.com/1328068.js?file=gistfile1.rb"></script>
<p>The second reason is namespacing. You&#8217;ll shortly see that everything is built on top of an immutable, lazy <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> object. A <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> just collects those methods that wouldn&#8217;t make sense for anything but a <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> being treated as a reservation. It&#8217;s about avoiding name collisions more than about modeling the world.</p>
<p>Inheritance gives me nested namespaces, something I dearly wish I had in Clojure. For example, there are a variety of functions that apply to <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a>es</code> that represent database tables, but are irrelevant to other ones. That code is contained in <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code>&#8217;s superclass, <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/db_hash.rb">DBHash</a></code>.</p>
<p>Actually: not quite. The text of the code is found in three different modules that are <code>included</code> into <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/db_hash.rb">DBHash</a></code>. I expect to do a lot of mixing-and-matching to create namespaces for particular <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/db_hash.rb">DBHash</a></code> classes and even particular objects (via <code>extend</code>). </p>
<p><b>Extracting a FunctionalTimeslice from a FullReservation</b></p>
<p>In the <code>reservations</code> table, there are three columns devoted to &#8220;when is the reservation for?&#8221; They are <code>:first_date</code>, <code>:last_date</code>, and <code>:time_bits</code>. (The first two are Date objects; the last represents the set <code>{morning, afternoon, evening}</code>. In a proper object-oriented design, you&#8217;d expect a <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> to <a href="https://github.com/marick/critter4us/blob/functional/strangled-src/model/reservation.rb#L78">contain</a> a <code><a href="https://github.com/marick/critter4us/blob/functional/strangled-src/model/timeslice.rb">Timeslice</a></code> that in turn contains those three values and some timeslice-specific methods as well. I chose to handle such sub-objects differently. Instead of asking a reservation for its timeslice, you make a timeslice from a reservation using setlike operations.</p>
<p><code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> has an <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L70">only</a></code> method that produces a smaller <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> containing <i>only</i> the named key-value pairs. So this is a timeslice:</p>
<script src="https://gist.github.com/1328691.js?file=gistfile1.rb"></script>
<p>I give the timeslice access to a timeslice-specific namespace by wrapping it in a class: </p>
<script src="https://gist.github.com/1328692.js?file=gistfile1.rb"></script>
<p><b>Adding a new Timeslice to Full Reservation</b></p>
<p>It&#8217;s often said that code without mutable state is easier to reason about. I don&#8217;t personally find that as big a deal as other people do, but I&#8217;ve gotten used to immutability from my Clojure programming. So <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> disallows messages like this:</p>
<script src="https://gist.github.com/1328713.js?file=gistfile1.rb"></script>
<p>The equivalent of assigning a value to a key is done by merging it and creating a new <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a>. The equivalent of deleting a key is done by making a copy of </code><code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> without the given key. At the moment, this implementation is grossly space-inefficient. Eventually, I&#8217;ll port it over to <a href="http://www.harukizaemon.com/">Simon Harris</a>&#8217;s <a href="https://github.com/harukizaemon/hamster">Hamster</a>, which implements structure sharing and other optimization techniques. I&#8217;m even thinking I might port his code to C.</p>
<p>Here&#8217;s the way to change a reservation&#8217;s date:</p>
<script src="https://gist.github.com/1328728.js?file=gistfile1.rb"></script>
<p>A few notes:</p>
<ul>
<li>
<p>
<code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L35">change_within</a></code> is a way of &#8220;merging&#8221; into a nested hash. It&#8217;s the equivalent of this:</p>
<script src="https://gist.github.com/1328732.js?file=gistfile1.rb"></script>
</p>
</li>
<li>
<p>
Remember that, here, <code>timeslice</code> is a three-key hash, not an object.
</p>
</li>
<li>
<p>
I&#8217;m also removing the <code>id</code> to remind myself that the reservation produced here no longer corresponds to one in the database.
</p>
</li>
</ul>
<p><b>Working with disallowed animals</b></p>
<p>Any time a reservation is made, it disallows some animals (because they&#8217;re now in use) and may disallow some animal/procedure pairs (because of rules about how frequently a procedure can be performed). That information is calculated once and stored in Postgres tables named <code>excluded_because_in_use</code> and <code>excluded_because_of_blackout_period</code>.</p>
<p>The code to look up which animals are in use during a timeslice is factored into three pieces. Inlined, it would look like this:</p>
<script src="https://gist.github.com/1328803.js?file=gistfile1.rb"></script>
<p>(<code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L87">Fall</a></code> turns an array of hashes into an array of <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHashes</a></code>.) </p>
<p>Because that code doesn&#8217;t refer to a reservation at all, it seems reasonable to put it in the <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/functional_timeslice.rb">FunctionalTimeslice</a></code> namespace.</p>
<p>The <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> can use the list of animal ids to prune out its uses:</p>
<script src="https://gist.github.com/1328833.js?file=gistfile1.rb"></script>
<p>That&#8217;s all easy enough, but the contract with the user is that she&#8217;ll see a list of names of animals that couldn&#8217;t be included in the reservation. Getting that list is easy enough, given that we have ready access to the rejected uses. Here&#8217;s the code, with changes to the previous version highlighted:</p>
<script src="https://gist.github.com/1328848.js?file=gistfile1.rb"></script>
<p>That&#8217;s fine, but what do we do with the value named by <code>___animals_already_in_use___</code>? I&#8217;d hate to return it along with the new version of the reservation because its caller would have to look like this:</p>
<script src="https://gist.github.com/1328875.js?file=gistfile1.rb"></script>
<p>I&#8217;d rather avoid names for intermediate steps in the creation of the reservation copy. I want the various versions to flow anonymously through a chain of functions so that I need only name the original and the final copy. (<code>original</code> and <code>copy</code> would be better names than <code>reservation</code> and <code>new_reservation</code>, it occurs to me, but I&#8217;m not going to go back now and change all these gists.)</p>
<p>That suggests slamming the animal list into the next version of the reservation, like this:</p>
<script src="https://gist.github.com/1328919.js?file=gistfile1.rb"></script>
<p>That kind of creeps me out, and it exacerbates temporal coupling. Nevertheless, it lets the caller <i>look nice</i>, which might mean something. (If mathematicians can go on about elegance, why can&#8217;t I?) I hope my tests will loudly tell me when coupling causes a change to function X to break function Y.  </p>
<p><b>as_saved</b></p>
<p>The last step of creating the copied reservation is oddly named:</p>
<script src="https://gist.github.com/1328974.js?file=gistfile1.rb"></script>
<p><i>as_saved?</i> This is a stylistic affectation that I&#8217;m not sure is a good idea. What I&#8217;m trying to imply is that the <i>main</i> thing this function does is create a new <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> with a bit of extra data, namely <code>data.id</code>, merged in. (The id needs to be sent off to the front end.) The fact that the id is created by changing persistent state somewhere is just an implementation detail. It could just as well be that every possible reservation always already exists somewhere as a big immutable pool, so <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb#L67">as_saved</a></code> just does a lookup to find the matching id.</p>
<p>(Which, it again occurs to me too late, perhaps makes as_saved a name that, strictly, reveals too much about the implementation.)</p>
<p>(Interestingly, I understand that the human immune system works roughly like the silly implementation above: you&#8217;re born with some <a href="http://en.wikipedia.org/wiki/Antibody#Immunoglobulin_diversity">10 billion different antibodies</a> and the response to infection (mostly) involves finding the useful ones, not creating new ones that match the foreign agent.)</p>
<p><b><code>only</code> and the nature of classes</b></p>
<p>After the as-it-appears-in-the-database <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> is created, the pieces that the frontend code care about are extracted and returned to the controller code, which turns them into JSON:</p>
<script src="https://gist.github.com/1328962.js?file=gistfile1.rb"></script>
<p>But something creepy is going on here. What&#8217;s the type of the result of <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L70">only</a></code>?</p>
<script src="https://gist.github.com/1329092.js?file=gistfile1.rb"></script>
<p>How can anyone possibly believe that the two-element hash, containing nothing about the reservation in question (but only about the difference between it and its original) is a <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code>?  </p>
<p>For a time, I considered changing <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L70">only</a></code> to produce a <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code>, rather than (as it does) an object of the same class as the receiver of the method. Then I smacked myself and reminded myself that I&#8217;m using classes to identify namespaces, not <a href="http://plato.stanford.edu/entries/natural-kinds/">natural kinds</a>. Saying that the result of <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L70">only</a></code> &#8220;is a&#8221; <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> would be absurd. But it&#8217;s less absurd to say that (1) we started with a hash, (2) the functions in the namespace <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> applied to it, (3) we derived a second hash from the first, so (4) it&#8217;s probably a good guess that the same namespace will be also useful for the second hash.</p>
<p>That is, it&#8217;s all about conservation of work. If I stripped every result of <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb#L70">only</a></code> down to a bare <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code>, I&#8217;d sometimes have to add a namespace back. By not stripping it, sure, I may get irrelevant functions in the easily-accessible namespace, but I can just ignore them.</p>
<p><b>Laziness</b></p>
<p>I chose the name <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> not just because <code>Reservation</code> was already taken. It&#8217;s because a <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> contains all the values that can possibly be relevant to a reservation. But some HTTP requests only care about the reservation&#8217;s id. Some only care about some of the data (like the timeslice). Only a few care about the uses and the groups.</p>
<p>Laziness of the sort implemented in Clojure and Haskell seems a nice match for this. When a <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> key is assigned a block/lambda, it doesn&#8217;t treat that as a value. Rather, the <code><a href="https://github.com/marick/critter4us/blob/functional/src/functional/functional_hash.rb">FunctionalHash</a></code> runs that block to calculate the value when the key is dereferenced. After that, the value is cached (and is immutable, just like any other value).</p>
<p>So consider these two steps from our controller:</p>
<script src="https://gist.github.com/1329344.js?file=gistfile1.rb"></script>
<p>We create a new reservation by saving a modified reservation to disk. That gives us a new row in the <code>reservations</code> table, some new rows in the <code>groups</code> table, and some new rows in the <code>uses</code> table. But nothing of the <code>groups</code> or <code>uses</code> is used from then on, so it would be a waste to populate the <code>new_reservation</code> with them. How is that avoided? By creating a <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> like this:</p>
<script src="https://gist.github.com/1329455.js?file=gistfile1.rb"></script>
<p>The uses and groups and even the row in the <code>:reservations</code> table are only loaded when they&#8217;re needed, so it costs little to use a <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> for everything. With this structure, I&#8217;m trying to gain more control than an object-to-relational mapping library gives me, while still freeing myself from the micromanagement of loading. Time will tell if that works.</p>
<p>(Note: I stash the original <code>id</code> that led to the <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> in <code>:starting_id</code>. Part of the motivation was to allow a completely fresh <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code> to return its id without going to the database at all, and another part was to retain the original id even after later changes made it no longer an index into the reservation contents. This dual purpose makes the code confused, I think.)</p>
<p>(Note: Postgres supports the SQL <a href="http://www.postgresql.org/docs/8.3/interactive/sql-insert.html">RETURNING</a> extension. So it&#8217;d be relatively easy to fully populate a saved <code><a href="https://github.com/marick/critter4us/blob/functional/src/db/full_reservation.rb">FullReservation</a></code>. I&#8217;ve used RETURNING several times, but always later discarded it for one reason or another.)</p>
<p><b>The grand conclusion</b></p>
<p>I&#8217;ve always hated end-of-talk or end-of-post summations. So I don&#8217;t really have one here, except that this approach feels promising, I want to continue trying it, I&#8217;d like to hear your comments (sorry about the antiquated blog software), and I&#8217;d especially like to hear what happens if you try out this approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/10/31/using-functional-style-in-a-ruby-webapp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Top-down design in &#8220;functional classic&#8221; programming</title>
		<link>http://www.exampler.com/blog/2011/10/07/top-down-design-in-functional-classic-programming-2/</link>
		<comments>http://www.exampler.com/blog/2011/10/07/top-down-design-in-functional-classic-programming-2/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 12:47:33 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[TDD]]></category>

		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/10/07/top-down-design-in-functional-classic-programming-2/</guid>
		<description><![CDATA[While waiting for my product owner to get back to me, I was going through open browser tabs. I read this from Swizec Teller:

The problem is one of turning a list of values, say, [A, B, C, D] into a list of pairs with itself, like so [[A,B], [A,C], [A,D], [B, C], [B,D], [C,D]].

He had [...]]]></description>
			<content:encoded><![CDATA[<p>While waiting for my product owner to get back to me, I was going through open browser tabs. I read <a href="http://swizec.com/blog/functional-isnt-always-better/swizec/2591">this</a> from Swizec Teller:</p>
<blockquote><p>
The problem is one of turning a list of values, say, [A, B, C, D] into a list of pairs with itself, like so [[A,B], [A,C], [A,D], [B, C], [B,D], [C,D]].
</p></blockquote>
<p>He had problems coming up with a good solution. &#8220;I can do that!&#8221; I said, launched a Clojure REPL, and started typing the whole function out. I quickly got bogged down. </p>
<p>This, I think, is a problem with a common functional approach. Even with this kind of problem&#8212;the sort of list manipulation that functional programming lives for&#8212;there&#8217;s a temptation to build a single function bottom up. &#8220;I need to create the tails of the sequence,&#8221; I thought, &#8220;because I know roughly how I&#8217;ll use them.&#8221;</p>
<p><i>For me</i> (and let&#8217;s not get into <a href="http://www.exampler.com/blog/2011/09/29/my-clojure-problem/">all that again</a>), it usually works better to go top down, mainly because it lets me think of discrete, meaningful functions, give them names, and write facts about how they relate to one another. So that&#8217;s what I did.</p>
<p>First, what am I trying to do? Not create all the pairs, but only ones in which an element is combined with another element further down the list. Like this:</p>
<script src="https://gist.github.com/1270705.js?file=gistfile1.clj"></script>
<p>As I often do when doing list-manipulation problems, I lay things out to visually emphasize the &#8220;shape&#8221; of the solution. That helps me see more clearly what has to be done to create that solution. There&#8217;s one set of pairs, each <i>headed</i> by the first element of the list, then another set, each headed by the second element. That is, I can say the above fact is true <i>provided</i> two other facts are true:</p>
<script src="https://gist.github.com/1270709.js?file=gistfile1.clj"></script>
<p>It&#8217;s easy to see how I get the heads&#8212;just map down the list (maybe I have to worry about the last element, maybe not&#8212;I&#8217;ll worry about it if it comes up). What are those heads combined with? Just the successive <i>tails</i> of the original list: <code>[2 3], [3]</code>. I&#8217;ll assume there&#8217;s a function that does that for me. That gives me this entire fact-about-the-world-of-this-program:</p>
<script src="https://gist.github.com/1270720.js?file=gistfile1.clj"></script>
<p>Given all that, <code>downward-pairs</code> is easy enough to write:</p>
<script src="https://gist.github.com/1270722.js?file=gistfile1.clj"></script>
<p>It&#8217;s important to me that I have reached a stable point here. When building up a complicated function from snippets in a REPL, I more often create the wrong snippets than when I <i>define</i> what those snippets need to do via a function that uses them. (I&#8217;m not saying that I never backtrack: I might find a subfunction too hard to write, or I may have <a href="http://ask.metafilter.com/58170/What-is-the-origin-of-the-phrase-Carving-nature-by-its-joints">carved up the world</a> in a way that leads to gross inefficiency, etc. I&#8217;m saying that I seem to end up with correct answers faster this way.)</p>
<p>Now I just have to solve two simpler problems. Tails I&#8217;ve done before, and here&#8217;s a solution I like: </p>
<script src="https://gist.github.com/1270736.js?file=gistfile1.clj"></script>
<p>For those who don&#8217;t know Clojure well, this produces this sequence of calls to <code>drop</code>:</p>
<script src="https://gist.github.com/1270744.js?file=gistfile1.clj"></script>
<p>This does roughly the same work as an iteration would do because of how laziness is implemented.</p>
<p>That leaves me <code>headed-pairs</code>, which is a pretty straightforward map:</p>
<script src="https://gist.github.com/1270748.js?file=gistfile1.clj"></script>
<p>This seems like a reasonable solution, doesn&#8217;t strike me as being terribly inefficient (given laziness), has a readability that I like, doubtless took me less time than developing it bottom-up would have, and comes with tests (including an end-to-end test that I won&#8217;t bother showing).</p>
<p>The whole solution is <a href="https://gist.github.com/1270755">here</a>.</p>
<p>UPDATE: Yes, I could have used <code>do</code> or even gotten explicit with the <code>sequence-m</code> monad, but that wouldn&#8217;t have addressed the original poster&#8217;s point, which I took to be how to think about functional problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/10/07/top-down-design-in-functional-classic-programming-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How mocks can cut down on test maintenance</title>
		<link>http://www.exampler.com/blog/2011/10/06/how-mocks-can-cut-down-on-test-maintenance/</link>
		<comments>http://www.exampler.com/blog/2011/10/06/how-mocks-can-cut-down-on-test-maintenance/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 13:28:39 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/10/06/how-mocks-can-cut-down-on-test-maintenance/</guid>
		<description><![CDATA[After around 11 months of not working on it, I needed to make a change to Critter4us, an app I wrote for the University of Illinois vet school. The change was simple. When I tried to push it to Heroku, though, I discovered that my Ruby gems were too out of date. So, I ended [...]]]></description>
			<content:encoded><![CDATA[<p>After around 11 months of not working on it, I needed to make a change to Critter4us, an app I wrote for the University of Illinois vet school. The change was simple. When I tried to push it to <a href="http://www.heroku.com/">Heroku</a>, though, I discovered that my Ruby gems were too out of date. So, I ended up upgrading from Ruby 1.8 to 1.9, to <a href="http://www.sinatrarb.com/">Sinatra</a> 1.3 from a Sinatra less than 1.0, to a modern version of <a href="http://rack.rubyforge.org/">Rack</a>, etc. etc. In essence, I replaced all the <a href="http://en.wikipedia.org/wiki/Turtles_all_the_way_down">turtles</a> upon which my code-world was resting. There were some backwards-compatibility problems.</p>
<p>One incompatibility was that I was sending an incorrectly formatted URI to fetch some JSON data. The old version of Rack accepted it, but the new one rejected it. The easy fix was to split what had been a single `timeslice` parameter up into multiple parameters. [Update: I later did something more sensible, but it doesn&#8217;t affect the point of this post.] &#8220;Crap!&#8221;, I thought. &#8220;I&#8217;m going to have to convert who knows how much test data.&#8221; But I was pleased when I looked at the first test and saw this:</p>
<script src="https://gist.github.com/1267790.js?file=gistfile1.rb"></script>
<p>The key point here is that neither the format of the URI parameters nor the resulting timeslice object is given in its real form. Instead, they&#8217;re represented by strings that basically name their type. (In my Clojure testing framework, <a href="https://github.com/marick/Midje">Midje</a>, I refer to these things as &#8220;<a href="https://github.com/marick/Midje/wiki/Metaconstants">metaconstants</a>&#8220;.)</p>
<p>The only responsibility this code has toward timeslices is to pass them to another object. That object, the `internalizer`, has the responsibility for understanding formats. The test (and code) change is trivial:</p>
<script src="https://gist.github.com/1267811.js?file=gistfile1.rb"></script>
<p>The test is even (and appropriately) less specific than before. It says only that the GET parameters (a hash) will contain some key/value pairs of use to the internalizer. It&#8217;s up to the internalizer to know which those are and do the right thing.</p>
<p>The point here is that the size of the test change is in keeping with the size of the code change. It is unaffected by the nature of the change to the data&#8212;which is as it should be.</p>
<p>This application is the one where I finally made the important decision to use mocks heavily in the <a href="http://www.growing-object-oriented-software.com/">Freeman and Pryce</a> &#8220;London&#8221; style and&#8212;most importantly&#8212;to not fall into the trap of thinking &#8220;Mocks are stupid!&#8221; when I ran into problems. Instead, I said &#8220;<i>I&#8217;m</i> stupid!&#8221; and, working on that assumption, figured out what I was doing wrong.</p>
<p>I made that decision halfway through writing the app. One of the happy results of the mocking that followed was that a vast amount of test setup code devoted to constructing complete data structures went away. No more &#8220;fixtures&#8221; or &#8220;object mothers&#8221; or &#8220;test factories.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/10/06/how-mocks-can-cut-down-on-test-maintenance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A postscript on expressiveness and performance</title>
		<link>http://www.exampler.com/blog/2011/09/30/a-postscript-on-expressiveness-and-performance/</link>
		<comments>http://www.exampler.com/blog/2011/09/30/a-postscript-on-expressiveness-and-performance/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 14:40:57 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/09/30/a-postscript-on-expressiveness-and-performance/</guid>
		<description><![CDATA[In some way, my original post makes it seem as if normal programmers are like the drug dealer D&#8217;Angelo Barksdale eating in a fancy restaurant in &#8220;The Wire&#8220;&#8212;everyone stared, the waiter rubbed D&#8217;Angelo&#8217;s ignorance of fine-restaurant customs in his face, and D&#8217;Angelo&#8217;s introspective attitude doesn&#8217;t hide a desperate desire to leave. Well, yes (though nothing [...]]]></description>
			<content:encoded><![CDATA[<p>In some way, <a href="http://www.exampler.com/blog/2011/09/29/my-clojure-problem/">my original post</a> makes it seem as if normal programmers are like the drug dealer D&#8217;Angelo Barksdale eating in a <a href="http://www.youtube.com/watch?v=xhhJVYK4nvQ">fancy restaurant</a> in &#8220;<a href="http://en.wikipedia.org/wiki/The_Wire">The Wire</a>&#8220;&#8212;everyone stared, the waiter rubbed D&#8217;Angelo&#8217;s ignorance of fine-restaurant customs in his face, and D&#8217;Angelo&#8217;s introspective attitude doesn&#8217;t hide a desperate desire to leave. Well, yes (though nothing in a programmer&#8217;s life is as bad as <i>anything</i> in a &#8220;The Wire&#8221; character&#8217;s life), but there&#8217;s more to it.</p>
<p>I think Clojure already suffers from being a language for hardcore programmers to solve hardcore problems. For example, that hardcore attitude makes things like nice stack traces seem unimportant. More: fishing through stack traces can almost become a perverse right of passage. <i>Do you have what it takes to program in Clojure, punk?</i> </p>
<p>The history of programming, as I&#8217;ve lived it, has been a march where expressiveness led the way and performance followed. I came of age when the big debate was whether us new-fangled C programmers and our limited compilers could ever replace the skilled assembly-language programmer. C won the debate. When Java came out, the debate was whether a language with byte codes and garbage collection could be fast enough to be a justifiable choice over C/C++. Java won the debate. Ruby is still being held up as too slow in comparison with Java, but even the pokey MRI runtime is fast enough for a whole lot of apps. Perhaps the emerging consensus is that Twitter-esque success will require migrating from Ruby to JVM infrastructure&#8212;and that&#8217;s exactly the kind of problem you want to have.</p>
<p>Against that background, it&#8217;s notable that Clojure 1.3 is taking a step backward in expressiveness in the service of performance. (By this, I mostly mean that performance&#8212;calling through vars, arithmetic operations&#8212;is the default and people who are worried about rebinding and bignums have to act specially. So my super-cool introduction-to-Clojure &#8220;Let&#8217;s take the sum of the first 3000 elements of an infinite sequence of factorials&#8221; example will become less impressive because arithmetic overflow is now in the programmer&#8217;s face.) That&#8217;s worrisome. There&#8217;s not much new in 1.3 to tempt the hitherto-reluctant Java or Ruby programmer. And unless I care about performance or am a utility library maintainer, I don&#8217;t see very much in 1.3 to tempt me away from 1.2. </p>
<p>Now, 1.3 is not a huge hit to expressiveness, and Clojure already has pretty much all the expressive language constructs that have stood the test of time (except, I suppose, Haskell-style pattern matching). So what else is left for someone of Rich Hickey&#8217;s talents to work on <i>but</i> performance? Documentation? </p>
<p>That&#8217;s a good point, but I worry about how performance will drive the constructs available to Clojure users. For example, multimethods feel a bit orphaned because of the drive to take advantage of how good the JVM is at type dispatch. There&#8217;s a whole parallel structure of genericity that&#8217;s getting the attention. And <code>conj</code> is a historical example: it&#8217;s hard to explain to someone how these two things both make sense:</p>
<pre>
user=> (conj [1 2 3] 4)
[1 2 3 4]
user=> (conj (map identity [1 2 3]) 4)
(4 1 2 3)
</pre>
<p>As with arithmetic overflow, it strikes those not performance-minded (those not hardcore) as odd to have lots of generic operations on sequences except for the case of putting something on the end. &#8220;So I have to keep track of what&#8217;s a vector? In a language that creates lazy sequences whenever it gets an excuse to?&#8221; </p>
<p>Catering to performance increases the cognitive load a language puts on its users. Again, the hardcore programmer will take it in stride. But Lisp and functional languages are different enough that I think Clojure needs more focused attention on the not-hardcore programmers.</p>
<p>Now, the quick retort is that Clojure isn&#8217;t my language. It&#8217;s a gift from Rich Hickie to the world. Who am I to demand <i>more</i> from him? That&#8217;s a good retort. But my counter-retort is that I <i>demand</i> nothing. I only suggest, based on my experience with the adoption of ideas and languages. It&#8217;s hard making something. More information (from users like me) is probably better than less.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/09/30/a-postscript-on-expressiveness-and-performance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A postscript about the validity of claims</title>
		<link>http://www.exampler.com/blog/2011/09/30/a-postscript-about-the-validity-of-claims/</link>
		<comments>http://www.exampler.com/blog/2011/09/30/a-postscript-about-the-validity-of-claims/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 13:42:16 +0000</pubDate>
		<dc:creator>Brian Marick</dc:creator>
		
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://www.exampler.com/blog/2011/09/30/a-postscript-about-the-validity-of-claims/</guid>
		<description><![CDATA[The problem with talking about the tone of the Clojure community is that almost no statement can ever be  proven. Rich Hickey and I went around and around about this on Twitter. So after citing this statement of his:

If people think that spending fifty percent of their time writing tests maximizes their results—okay for [...]]]></description>
			<content:encoded><![CDATA[<p>The problem with <a href="http://www.exampler.com/blog/2011/09/29/my-clojure-problem/">talking about the tone of the Clojure community</a> is that almost no statement can ever be  <i>proven</i>. Rich Hickey and I went around and around about this on Twitter. So after citing this <a href="http://www.codequarterly.com/2011/rich-hickey/">statement</a> of his:</p>
<blockquote><p>
If people think that spending fifty percent of their time writing tests maximizes their results—okay for them. I’m sure that’s not true for me—I’d rather spend that time thinking about my problem.
</p></blockquote>
<p>&#8230; I could say that I read a snarky &#8220;[those losers]&#8221; implicitly attached to &#8220;okay for them.&#8221; I could say that &#8220;fifty percent of your time writing tests&#8221; and &#8220;rather spend that time thinking about my problem&#8221; are complete mischaracterizations of TDD as it&#8217;s actually done. And I can say that those mischaracterizations and that snark will be aped by programmers who want to be Rich Hickey. </p>
<p>I could say all those things, but the statement as written isn&#8217;t <i>false</i>. It can even be given a generous reading. But&#8212;if I&#8217;m right&#8212;programmers like me won&#8217;t stick around to hear defensible statements defended. They&#8217;ll just look for friendlier places where they aren&#8217;t expected to deal with decade-old arguments as if they were new and insightful.</p>
<p><i>Please note that I&#8217;m not saying Rich Hickie&#8217;s design approach is wrong. I suspect for the kinds of problems he solves, it may even be true that his approach is better across all designers than TDD is.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exampler.com/blog/2011/09/30/a-postscript-about-the-validity-of-claims/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

