<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>3hv &#187; Beautiful Code</title>
	<atom:link href="http://www.3hv.co.uk/blog/category/beautiful-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3hv.co.uk/blog</link>
	<description>beautiful code for elegant web sites</description>
	<lastBuildDate>Mon, 21 Dec 2009 21:21:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Favourite code</title>
		<link>http://www.3hv.co.uk/blog/2009/11/12/favourite-code/</link>
		<comments>http://www.3hv.co.uk/blog/2009/11/12/favourite-code/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 22:31:53 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[bigwig]]></category>
		<category><![CDATA[brightbox]]></category>
		<category><![CDATA[caius durling]]></category>
		<category><![CDATA[david smalley]]></category>
		<category><![CDATA[delphi]]></category>
		<category><![CDATA[object factory]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=607</guid>
		<description><![CDATA[I think Bigwig has become my favourite bit of code that I&#8217;ve ever worked on.  
Before Bigwig, it was Object Factory, before that it was a Delphi class that I used to create tree-structured data (imaginatively called TNode).  
Bigwig&#8217;s doesn&#8217;t have a test suite and it&#8217;s not even my code &#8211; it was [...]]]></description>
			<content:encoded><![CDATA[<p>I think <a href="http://github.com/brightbox/bigwig">Bigwig</a> has become my favourite bit of code that I&#8217;ve ever worked on.  </p>
<p>Before Bigwig, it was <a href="http://github.com/brightbox/object-factory">Object Factory</a>, before that it was a Delphi class that I used to create tree-structured data (imaginatively called TNode).  </p>
<p>Bigwig&#8217;s doesn&#8217;t have a test suite and it&#8217;s not even my code &#8211; it was started by <a href="http://davidsmalley.com/">David Smalley</a> and has contributions from <a href="http://caius.name/">Caius Durling</a> and the rest of the Brightbox team.  </p>
<p>But there&#8217;s just something about it &#8211; it&#8217;s a daemon (start it running and forget about it); it&#8217;s been running in production for months without a single glitch; and probably most importantly, the code is so simple that there&#8217;s almost nothing to it. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/11/12/favourite-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Free Software and Brightbox</title>
		<link>http://www.3hv.co.uk/blog/2009/03/09/free-software-and-brightbox/</link>
		<comments>http://www.3hv.co.uk/blog/2009/03/09/free-software-and-brightbox/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 11:26:28 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[bigwig]]></category>
		<category><![CDATA[brightbox]]></category>
		<category><![CDATA[free software]]></category>
		<category><![CDATA[object factory]]></category>
		<category><![CDATA[warren]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=422</guid>
		<description><![CDATA[I&#8217;ve just done a quick post at the Brightbox blog detailing their use of free and open source software and showing some of the free Brightbox projects.  
In particular, Warren &#038; Bigwig and Object Factory could be of use to many people and are probably worth a look.  
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just done a quick post at the <a href="http://blog.brightbox.co.uk/posts/free-software-and-brightbox">Brightbox blog</a> detailing their use of free and open source software and showing some of the free Brightbox projects.  </p>
<p>In particular,<a href="http://github.com/brightbox/warren/tree/master"> Warren &#038; Bigwig</a> and <a href="http://github.com/brightbox/object-factory/tree">Object Factory</a> could be of use to many people and are probably worth a look.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/03/09/free-software-and-brightbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC: A brief history of Models, Views and Controllers</title>
		<link>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/</link>
		<comments>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:12:58 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Smalltalk]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=367</guid>
		<description><![CDATA[Any web-developer Rubyist knows about models, views and controllers.  The MVC paradigm is embedded in the structure of Rails and Merb and encouraged by Ramaze and Sinatra.  If you&#8217;re a Mac developer or an iPhone bod then MVC is common practice there as well.  Same goes for Sproutcore and even Microsoft is [...]]]></description>
			<content:encoded><![CDATA[<p>Any web-developer Rubyist knows about models, views and controllers.  The MVC paradigm is embedded in the structure of <a href="http://rubyonrails.org/">Rails</a> and <a href="http://merbivore.com/">Merb</a> and encouraged by <a href="http://ramaze.net/">Ramaze</a> and <a href="http://www.sinatrarb.com/">Sinatra</a>.  If you&#8217;re a <a href="http://developer.apple.com/cocoa/">Mac developer</a> or an <a href="http://developer.apple.com/iphone/">iPhone bod</a> then MVC is common practice there as well.  Same goes for <a href="http://www.sproutcore.com/">Sproutcore</a> and even Microsoft is <a href="http://www.asp.net/mvc/">getting in on the act</a>.  </p>
<p>But where does MVC come from?</p>
<p>Well, like most things I enjoy in the world of software development, MVC has its roots in Smalltalk. However, things used to look slightly different to the MVC we know and love.  In Smalltalk, the basic Object has a feature known as &#8220;dependencies&#8221; built-in; nowadays, we would call this the observer pattern.  Basically, any object can register an interest in any other object and will be notified whenever the target changes.  </p>
<pre><code>
target addDependent: listener.
</code></pre>
<p>Later, when something changes within target, it can decide to notify all its dependents (observers): </p>
<pre><code>
self changed: #SomeArbitraryMessage.
</code></pre>
<p>and the listener has its <tt>update: aSymbol</tt> method called.  </p>
<p>Building this notification mechanism right into the core of the system means that it can be used everywhere &#8211; especially when designing user-interfaces (remember, Smalltalk <em>was</em> the GUI for the Xerox Star, which his Steveness bought and adapted for the Lisa).  Models were the system components, the pieces of the system that actually do the work.  Views represented portions of the screen and controllers represented the keyboard and the mouse.  These parts were arranged as follows: <div id="attachment_368" class="wp-caption alignleft" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-smalltalk.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-smalltalk-300x145.png" alt="MVC in Smalltalk" title="MVC in Smalltalk" width="300" height="145" class="size-medium wp-image-368" /></a><p class="wp-caption-text">MVC in Smalltalk</p></div>  </p>
<p>The view would add itself as a dependent to the model and display the relevant aspects of the model on-screen.  The user would see this, manipulate it via the mouse and keyboard, which triggers the controller.  The controller sends messages to the model, which triggers its notifications, prompting the dependent view to redraw itself.  In other words, the controller and view are independent objects that know nothing about each other &#8211; the only tie between them is the controller&#8217;s knowledge of the model and the dependency mechanism.  </p>
<p>Contrast this to the MVC we know today: <div id="attachment_371" class="wp-caption alignright" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-rails.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-rails-300x144.png" alt="MVC in Rails" title="MVC in Rails" width="300" height="144" class="size-medium wp-image-371" /></a><p class="wp-caption-text">MVC in Rails</p></div></p>
<p>Here the user pokes the controller, which in turn sends messages to the model.  The model does its thing and then the controller extracts the relevant information and passes it to the view, which is then rendered to the user.  In this manner, the controller lives up to its name, orchestrating the entire cycle &#8211; and is the centre of all the coupling (as it knows about the view and the model).  </p>
<p>So why did things change (first in Smalltalk and then the following MVC frameworks)?</p>
<p>The first problem is that the controller takes input and the view handles output &#8211; as separate objects.  Which, especially for desktop applications, doesn&#8217;t really work; if you click on a text-field you expect different behaviour to clicking on a scrollbar.  </p>
<p>Secondly, imagine an array of models being shown in a list box.  The list box has the concept of a current selection.  So your array model (a model of models) needs to have a current selection, so that the view knows which is which.  But what if the same list is rendered in two places at the same time.  If you point both views at the same array model, they would both share the same selection properties.  So you actually end up with something more like: <div id="attachment_374" class="wp-caption alignnone" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-getting-complex.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-getting-complex-300x166.png" alt="Getting Complicated" title="Getting Complicated" width="300" height="166" class="size-medium wp-image-374" /></a><p class="wp-caption-text">Getting Complicated</p></div>  </p>
<p>As you can see it gets nasty pretty quickly.  And actually the controller is simply passing its messages directly through to the array model which is actually doing the routing of the other messages &#8211; the controller is pretty much redundant.  </p>
<p>So if you swap things around, merge the controller and array model (into some sort of array controller) and then add multiple array controllers &#8211; one per view &#8211; to deal with the selection issue, suddenly you have the newer style object layout.  Requests go into one of the controllers, it routes it to the model, then parcels up the results and invokes the view.  </p>
<p>Evolution in action.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The curious case of beauty in Ruby (or Rails vs Merb part 2)</title>
		<link>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/</link>
		<comments>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 00:51:41 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=349</guid>
		<description><![CDATA[I&#8217;m sure you&#8217;ve all heard the Rails 3 announcement.  When I first found out my initial reaction was &#8220;fuck me&#8220;.  But shortly after I was filled with a feeling of dread and general unease.  And I didn&#8217;t know why &#8230;.
Firstly, a bit of history.  
I first tried programming on a Commodore [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you&#8217;ve all heard the <a href="http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3">Rails 3 announcement</a>.  When I first found out my initial reaction was &#8220;<a href="http://twitter.com/xbaz/status/1074944824">fuck me</a>&#8220;.  But shortly after I was filled with a feeling of <a href="http://twitter.com/xbaz/status/1076051798">dread and general unease</a>.  And I didn&#8217;t know why &#8230;.</p>
<p>Firstly, a bit of history.  </p>
<p>I first tried programming on a Commodore Vic 20, and then after that a C64.  C64 BASIC was very simple &#8211; if you wanted to do anything beyond PRINT statements you needed to POKE values into registers and control the hardware directly.  Great for learning how things actually worked.  And, to be fair, I was shit at it.  </p>
<p>But I do remember reading an article on a system called &#8220;Smalltalk&#8221; and its &#8220;Object Oriented Programming&#8221;.  Suddenly, programming made sense.  It read a bit like English.  You sent messages to the thing that knows how to answer your question.  It was like talking to people.  You ask Dave a <a href="http://www.eighteensixtyfive.co.uk">football</a> question.  You ask George a <a href="http://www.theresplendents.com">music</a> question.  Cos Dave knows crap all about music and George knows nothing about football.  </p>
<p>But, in those days, Smalltalk cost a fortune; there was no way a child like me could get hold of a Smalltalk environment.  So instead, I got hold of Turbo Pascal 6 With Objects (thanks Dad).  It was not Smalltalk but it read a bit like English and it had objects.  I played about with Turbo Pascal, went to university (where I didn&#8217;t do computing but did do some C++) and then got a job doing Delphi (Turbo Pascal for the 90s).  This object-oriented stuff really worked for me; I put a lot of effort into writing classes that had really simple public interfaces and with code that read like English.  And Pascal (the language underlying Delphi) was great for that.  Then I discovered Java, which meant I could write Delphi-like code but with having to deal with memory management.  I also discovered PHP, Python and Ruby.  None of which clicked with me; dynamic typing made me nervous (and PHP and Ruby seemed a bit ugly).  </p>
<p>However, I needed an ORM for a Delphi project and I thought I should try to copy an open source project.  Whilst searching I discovered Rails and thought &#8220;this is the one to copy&#8221;.  But a day into my &#8220;copy ActiveRecord into Delphi&#8221; plan I thought &#8220;this is just like Smalltalk&#8221;.  Why make an inferior copy when I can use something that&#8217;s not far off the Holy Grail.  Writing an application on Rails had the same effect on me as my original discovery of Smalltalk &#8211; it read like English, it felt fantastic.  So I gave up on Delphi and became a Rails programmer.  </p>
<p>What I liked about Rails was its emphasis on happiness.  When I wrote Rails code I felt like I was writing beautiful prose.  I would go back and refactor it until it read correctly.  This was not like pure Ruby, which was often ugly.  No; Rails had this idea about beauty in code that really got me excited.  It made me happy.  It also made decisions for me &#8211; put your code here, test it like this, set up your database this way.  But Rails had performance problems &#8211; so Merb was born.  A ground-up rewrite of many of Rails&#8217; ideas but with an emphasis on configurability and performance.  </p>
<p>Maybe it&#8217;s the <a href="http://engineyard.com">Engine Yard</a> connection (I turned Engine Yard down for a job because it didn&#8217;t &#8220;feel right&#8221;) &#8211; and now I work for <a href="http://www.brightbox.co.uk">Brightbox</a>, one of their competitors &#8211; but for some reason, every time I tried Merb I just couldn&#8217;t get into it.  It was weird.  Structurally and functionally it was the same as Rails &#8211; but it was Rails plus performance plus options.  And I didn&#8217;t like it.  I never got past the tutorials.  Merb emphasises clear and understandable code and was tested with RSpec (which I love).  Rails is hard to understand and uses Test::Unit (which is ugly).  But I love Rails and I can&#8217;t get into Merb.  I just couldn&#8217;t figure out why.  </p>
<p>Until today.  </p>
<p>Mr Hanson did a <a href="http://loudthinking.com/posts/37-bringing-merbs-providesdisplay-into-rails-3">blog post</a> on his first piece of Rails-Merb integration.  And something stood out at me.  As he was describing Merb&#8217;s provides/display functionality I noticed that I didn&#8217;t really &#8220;get it&#8221;.  <tt>provides</tt> made sense, but how does that relate to <tt>display</tt>.  Mr H addresses that directly: </p>
<blockquote><p>There were a couple of drawbacks with the provides/display duo, though, that we could deal with at the same time. The first was the lack of symmetry in the method names. The words &#8220;provides&#8221; and &#8220;display&#8221; doesn&#8217;t reflect their close relationship and if you throw in the fact that they&#8217;re actually both related to rendering, it&#8217;s gets even more muddy.</p></blockquote>
<p>And then he describes the Rails 3 version of the same functionality.  Instead of provides/display it becomes respond_to/respond_with.  In particular <tt>display @users</tt> becomes <tt>respond_with @users</tt>.  </p>
<p>It&#8217;s only a tiny thing.  Logically and functionally, they are exactly the same.  But DHH&#8217;s version has an emphasis <strong>on the words</strong> that are used.  How they couple together (display/provide versus respond_to/respond_with).  </p>
<p>And there is the reason that I was uneasy about Rails 3.  What if Rails lost this emphasis on the human factors &#8211; how the words mesh together &#8211; in the search of performance.  Merb is written functionally, Rails is written emotionally &#8211; Merb is about performance, Rails is about feelings.  </p>
<p>But DHH has made me feel much better about Rails 3 &#8211; he has shown that he will take Merb constructs and Railsify them, humanise them.  Because, although code is executed by computers, it is written, and more importantly, <em>read</em> by people like me.  </p>
<p>If you find this useful then please take a look at some of my other <a href="http://www.3hv.co.uk/blog">writing</a> &#8211; or recommend me on <a href="http://www.workingwithrails.com/person/8382-rahoul-baruah">Working with Rails</a>.  Cheers. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Acceptance Testing in Ruby, Rails, RSpec and Cucumber</title>
		<link>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/</link>
		<comments>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 18:16:13 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=328</guid>
		<description><![CDATA[I&#8217;ve written up a new post at the Brightbox blog detailing how we are using RSpec and Cucumber to build acceptance tests for the next generation Brightbox systems.  
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written up a new post at the <a href="http://blog.brightbox.co.uk/posts/using-rspec-cucumber-and-user-stories-to-build-our-internal-systems">Brightbox blog</a> detailing how we are using <a href="http://rspec.info/">RSpec</a> and <a href="http://github.com/aslakhellesoy/cucumber/tree/master">Cucumber</a> to build acceptance tests for the next generation Brightbox systems.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading the MySQL drivers into GNU Smalltalk</title>
		<link>http://www.3hv.co.uk/blog/2008/11/14/loading-the-mysql-drivers-into-gnu-smalltalk/</link>
		<comments>http://www.3hv.co.uk/blog/2008/11/14/loading-the-mysql-drivers-into-gnu-smalltalk/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 12:50:04 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Smalltalk]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=310</guid>
		<description><![CDATA[It&#8217;s an unfortunate fact that many Open Source projects have documentation that is sadly lacking.  A case in point is GNU Smalltalk.  
Smalltalk is one of my favourite languages but a decent Smalltalk implementation that fits with your native window manager is hard to find.  The point of GNU Smalltalk is that [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s an unfortunate fact that many Open Source projects have documentation that is sadly lacking.  A case in point is GNU Smalltalk.  </p>
<p>Smalltalk is one of my favourite languages but a decent Smalltalk implementation that fits with your native window manager is hard to find.  The point of GNU Smalltalk is that it works &#8220;headlessly&#8221; (Smalltalk invented the graphical user-interface and the integrated development environment so this is quite a departure), so I can still use my favourite text editor and run stuff from the command line.  Just like Ruby!</p>
<p>However, it&#8217;s taken me a couple of hours to figure out how to simply connect to a database.  But now I have, here it is: </p>
<ul>
<li>Install GNU Smalltalk (in my case using MacPorts):<br />
    <code>sudo port install gst</code>
  </li>
<li>Start GNU Smalltalk and create a working image:<br />
<code>cd /Users/rahoulb/source/st<br />
gst<br />
st> ObjectMemory snapshot: 'work.im'.</code>
</li>
<li>Ctrl-D to exit Smalltalk and then restart using your new image:<br />
<code>gst -I work.im</code>
</li>
<li>Load the DBI database driver:<br />
<code>st> PackageLoader fileInPackage: 'DBI'.</code><br />
Load the MySQL driver:<br />
<code>st> PackageLoader fileInPackage: 'DBD-MySQL'.</code><br />
Save your image so you don&#8217;t need to reload these packages again:<br />
<code>st> ObjectMemory snapshot.</code>
</li>
<li>
Open a connection to your database:<br />
<code>st>con:= DBI.Connection connect: 'dbi:MySQL:dbname=mydatabase' user: 'myuser' password: 'mypassword'.</code><br />
Grab some data:<br />
<code>st>results:= con select: 'select name from customers limit 10;'</code>
</li>
<li>Bask in the glory of your data</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/11/14/loading-the-mysql-drivers-into-gnu-smalltalk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Showing the Git branch in your bash prompt</title>
		<link>http://www.3hv.co.uk/blog/2008/10/29/showing-the-git-branch-in-your-bash-prompt/</link>
		<comments>http://www.3hv.co.uk/blog/2008/10/29/showing-the-git-branch-in-your-bash-prompt/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 22:02:19 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=295</guid>
		<description><![CDATA[y first adventure in source control was many years ago.  It was my first proper job and I was the sole developer in a tiny company.  To keep the source code safe, it was all stored on a network share, and the file server was backed up at least once a day.  [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_297" class="wp-caption alignleft" style="width: 210px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2008/10/962334_57900306-1.jpg"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2008/10/962334_57900306-1-200x300.jpg" alt="Safe and Secure - Image by frko: http://www.sxc.hu/photo/962334" title="Secure" width="200" height="300" class="size-medium wp-image-297" /></a><p class="wp-caption-text">Safe and Secure - Image by frko: http://www.sxc.hu/photo/962334</p></div>My first adventure in source control was many years ago.  It was my first proper job and I was the sole developer in a tiny company.  To keep the source code safe, it was all stored on a network share, and the file server was backed up at least once a day.  </p>
<p>The problems started when two other developers joined the team.  Within a week we repeatedly had the issue of two people editing the same file at the same time and one of them losing their changes.  So we devised our own source control system.  Every file had a piece of cardboard with its name written on it, about 2cm by 10cm.  These were stuck, with blu-tak, to a wall.  If anyone wanted to edit a file they must stand up, go to the board, take the piece of cardboard for that particular file and stick it to their monitor.  So we had an at a glance view of who was working on what and also made sure that the files were kept safe.  </p>
<p>Since then I&#8217;ve used Visual Sourcesafe (don&#8217;t laugh).  To my mind, it&#8217;s not completely awful, but I could never get my head around branching and merging.  I then moved to Subversion which is both incredibly simple to use and free.  And I understood branching and merging, but it was just a bit too long-winded to use.  </p>
<p>However now I, like all the Rails-kids, use git.  Which is both great and awful.  It&#8217;s taken me weeks to even begin to get my head around it.  I still lose code every now and then.  But it&#8217;s so easy to branch and merge; for the first time, branching is an integral part of the way I work.  There is danger in branching though.  With svn it&#8217;s easy to see which branch you are in &#8211; it&#8217;s the folder name.  In git, it&#8217;s not so easy.  </p>
<p>So, my extremely smart colleague, <a href="http://davidsmalley.com/">David Smalley</a>, showed me this amendment to your .bashrc (or in my case .profile): </p>
<pre><code>function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h:\w\$(parse_git_branch)\$ "
</code></pre>
<p>The function asks git which branch we are currently in.  We then set PS1, the variable for the command prompt, asking it to show the username, host, path and branch.  </p>
<p>So, if you are not in a git-managed folder you see: </p>
<p><code>rahoulb@monster:/Volumes/src$ </code></p>
<p>And if you are in a git-managed folder you see: </p>
<p><code>rahoulb@monster:/Volumes/src/bb-billing(master)$</code></p>
<p>So now you have no excuse!</p>
<p>Photo by <a href="http://www.sxc.hu/photo/962334">frko</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/10/29/showing-the-git-branch-in-your-bash-prompt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Specification is the Documentation Part Two</title>
		<link>http://www.3hv.co.uk/blog/2008/08/05/the-specification-is-the-documentation-part-two/</link>
		<comments>http://www.3hv.co.uk/blog/2008/08/05/the-specification-is-the-documentation-part-two/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 11:15:16 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>

		<guid isPermaLink="false">http://blog.3hv.co.uk/?p=257</guid>
		<description><![CDATA[Two (related) thoughts on &#8220;The Specification is the Documentation&#8220;.
One of the things that I like to do, when developing, is to start with a sketch (you know, with 95g/m2 paper and a 6B pencil) of how the UI will look.  There are two reasons for this.  Firstly, it helps communications with the client [...]]]></description>
			<content:encoded><![CDATA[<p>Two (related) thoughts on &#8220;<a href="http://blog.3hv.co.uk/2008/08/01/the-specification-is-the-documentation/">The Specification is the Documentation</a>&#8220;.</p>
<p>One of the things that I like to do, when developing, is to start with a sketch (you know, with 95g/m<sup>2</sup> paper and a 6B pencil) of how the UI will look.  There are two reasons for this.  Firstly, it helps communications with the client &#8211; they can see something concrete, while it&#8217;s also blatantly apparent that there&#8217;s a long way to go yet.  Secondly, it puts me in the position of starting from the ideal position and working &#8220;downwards&#8221; to what is possible, rather than starting from what is easy and working &#8220;upwards&#8221; to what is nice.  In other words it forces me to raise my standards.  </p>
<p>Writing my specifications with the helper methods as <a href="http://blog.3hv.co.uk/2008/08/01/the-specification-is-the-documentation/">described</a> is the coding equivalent.  I start with a high-level, abstract, description of the problem I am trying to solve (&#8221;given a logged in user and three gizmos, expect the gizmos to be tagged when I go to the &#8216;tag-gizmos&#8217; page&#8221;).  I then work on setting up the environment (implementing the helpers) and the getting the specification to pass (implementing the code).  Again, I start with the ideal position and work downwards as I implement.  </p>
<p>The second thing is that it is one of those techniques where I am sure that I am along the right lines.  How can I tell?  Because as soon as I wrote my first specification in this way it &#8220;felt right&#8221;.  After I had written a couple more I wanted to go back to every piece of code I had ever written and rewrite it all in this new way.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/08/05/the-specification-is-the-documentation-part-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Specification is the Documentation</title>
		<link>http://www.3hv.co.uk/blog/2008/08/01/the-specification-is-the-documentation/</link>
		<comments>http://www.3hv.co.uk/blog/2008/08/01/the-specification-is-the-documentation/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 22:14:30 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>

		<guid isPermaLink="false">http://blog.3hv.co.uk/?p=255</guid>
		<description><![CDATA[In a former life I used to write &#8220;functional specifications&#8221;. These were long, dense, hard-to-read documents that detailed what an application (not yet written) was supposed to do.  I would spend (literally) weeks typing these things up, the customer would read it, think they understand and I would quote them based upon the document. [...]]]></description>
			<content:encoded><![CDATA[<p>In a former life I used to write &#8220;functional specifications&#8221;. These were long, dense, hard-to-read documents that detailed what an application (not yet written) was supposed to do.  I would spend (literally) weeks typing these things up, the customer would read it, think they understand and I would quote them based upon the document. And then the project would go over budget as all the tiny subtle details became apparent about two weeks before deadline day. But, even worse, the document would slowly become out of date, as changes were made in response to feedback &#8211; while the spec stayed unchanged.</p>
<p>As you might expect, this lead to general disillusionment with functional specifications.  What&#8217;s the point if they didn&#8217;t help with the budget and didn&#8217;t reflect the actual application?</p>
<p>But when I read about Test-Driven Development the key thing that struck me was the tests became a living embodiment of what the application is <em>supposed</em> to do.  They are a specification; but not only that, they are a specification that <em>has</em> to remain up to date.</p>
<p>Only one problem.  They are written in code.  Making them meaningless to the client.  In fact, often, some tests were so obscure they were only meaningful to me; as I was writing them.  Come back to it six months later and try and figure out why that change has made it fail?  No idea.</p>
<p>Which is why <a href="http://rspec.info">RSpec</a> and its Stories are so exciting.  A Story is a text document that describes the required behaviour of the application.  Read that again.  A <em>text document</em>; written in English.  So your clients can read them.  Can help to write them.  You then supply some Ruby code, that matches the sentences in your stories and associates them with code.  That code is run, testing your application and proves that it does what it is supposed to (providing you&#8217;ve written your test code correctly of course).</p>
<pre><code>Story: measure progress towards registration goals
  As a conference organizer
  I want to see a report of registrations
  So that I can measure progress towards registration goals
  Scenario: one registration shows as 1%
    Given a goal of 200 registrations
    When 1 attendee registers
    Then the goal should be 1% achieved 

  Scenario: one registration less than the goal shows as 99%
    Given a goal of 200 registrations
    When 199 attendees register
    Then the goal should be 99% achieved
</code></pre>
<p>Now, I have to admit, I&#8217;ve not used RSpec Stories in anger yet.  But it has had a strong effect on my &#8220;unit&#8221; tests.</p>
<p>The difference between stories and unit tests are that stories test your full stack.  Go to &#8216;/&#8217;, fill out the text fields, click the submit button, it should insert into the database successfully and then show these three records on the &#8216;/whatever&#8217; page.  Unit tests will check that the form is shown when you go to &#8216;/&#8217;.  A separate test will check that your record can be inserted into the database.  Another test will prove that &#8216;/whatever&#8217; asks the database for three records.</p>
<p>But, having read about Stories, the way I write my unit tests have changed.  Check this controller specification out:</p>
<pre><code>describe ReportsController, "at the admin site" do
  it "should show this month by default" do
    given_the_admin_site
    given_a_system_user

    when_getting :index do</code></pre>
<pre><span style="font-family: -webkit-monospace;">
<pre><code>      expect_to_find_orders_for Date.today</code></pre>
<p></span></pre>
<pre><code>    end

    assigns[:date].should be_today
    assigns[:orders].should == @orders
  end
end
</code></pre>
<p>Not quite plain English.  But, when it comes to maintenance, using <tt>given</tt>, <tt>expect</tt> and <tt>when</tt> as prefixes for your helpers makes a world of difference.</p>
<p> </p>
<p>UPDATE: now using block syntax around the &#8220;when_getting&#8221; statement</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/08/01/the-specification-is-the-documentation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting up a mock object to test a :dependent =&gt; :destroy association in RSpec and Rails</title>
		<link>http://www.3hv.co.uk/blog/2008/07/10/setting-up-a-mock-object-to-test-a-dependent-destroy-association-in-rspec-and-rails/</link>
		<comments>http://www.3hv.co.uk/blog/2008/07/10/setting-up-a-mock-object-to-test-a-dependent-destroy-association-in-rspec-and-rails/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 14:31:26 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>

		<guid isPermaLink="false">http://blog.3hv.co.uk/?p=245</guid>
		<description><![CDATA[One of the great advantages of using mock objects to test and specify your objects is that you concentrate solely on the thing you are testing.  
If you weren&#8217;t using mocks to tests that a controller re-shows the &#8220;new&#8221; form if given an invalid object, you would do post :create, :model => { ... } where [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great advantages of using mock objects to test and specify your objects is that you concentrate solely on the thing you are testing.  </p>
<p>If you weren&#8217;t using mocks to tests that a controller re-shows the &#8220;new&#8221; form if given an invalid object, you would do <tt>post :create, :model => { ... }</tt> where &#8230; is a set of fields that are invalid.  This means that, when writing your spec, you have to remember what it is that makes that model invalid.  This also means that, every time you change that model, and its rules for validity, you potentially have to amend the controller <em>test</em> as well.  In other words, you are not actually testing the controller in isolation, you are also testing the model.  </p>
<p>Using mocks lets you write the following: </p>
<pre><code>
  @model = mock Model
  Model.should_receive(:new).and_return(@model)
  @model.should_receive(:save).and_return(false)
  post :create, :model => { :some => :fields }
  response.should render_template('/models/new')
</code></pre>
<p>In other words, it doesn&#8217;t matter what parameters you send to create.  The Model (class) will return you a mock instance of model, and the mock instance will return false on save (you can actually get save! to raise an ActiveRecord::RecordInvalid &#8211; but I&#8217;ve had some difficulties with that).  In other words, the <em>real</em> model is no longer part of the test and you are concentrating on the behaviour of the controller alone.  </p>
<p>This concentrating on what is important is a vital advantage when using mocks.  </p>
<p>But how does this work on testing models?  </p>
<p>I wanted to test that a default value was copied from one field to another under certain circumstances.  So I set up a method, called <tt>set_default_value</tt> and wrote some specs to ensure that it was working.  Then I wrote the following: </p>
<pre><code>
  it "should set the default value before validation" do
    @model = Model.new
    @model.should_receive(:set_default_value)
    @model.valid?
  end
</code></pre>
<p>This failed, as it should do.  Then I set a <tt>before_validation :set_default_value</tt> on the model and the spec passed.  Each spec concentrates purely on what is important &#8211; a couple to show that <tt>set_default_value</tt> works under different circumstances, and one to show that it is called when it is supposed to be.  </p>
<p>What about testing a <tt>:dependent => :destroy</tt> association?  </p>
<p>Unfortunately, that can&#8217;t be done without saving at least one of the objects in the association (which means knowing enough about it to make it valid).  But, as <a href="http://blog.davidchelimsky.net/">David Chelimsky</a> (Mister RSpec) points out on the <a href="http://thr3ads.net/rspec-users/2007/06/13589-checking-associated-objects-have-been-deleted">RSpec mailing list</a>, you can do it using mock objects for part of the association.  Which was a relief as my &#8220;child&#8221; object was complex with a whole set of interdependencies of its own.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/07/10/setting-up-a-mock-object-to-test-a-dependent-destroy-association-in-rspec-and-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
