<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Mark Needham</title>
	
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Wed, 08 Sep 2010 18:55:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MarkNeedham" /><feedburner:info uri="markneedham" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Ruby: Checking an array contains an item</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/YqmEg8BLwhs/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/08/ruby-checking-an-array-contains-an-item/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 18:54:50 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2904</guid>
		<description><![CDATA[A couple of times in the past few days I've wanted to check if a particular item exists in an array and presumably influenced by working for too long with the .NET/Java APIs I keep expecting there to be a 'contains' method that I can call on the array! More as an attempt to help [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of times in the past few days I've wanted to check if a particular item exists in an array and presumably influenced by working for too long with the .NET/Java APIs I keep expecting there to be a 'contains' method that I can call on the array!</p>
<p>More as an attempt to help myself remember than anything else, the method we want is actually called 'include?'.</p>
<p>Therefore&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<p>One other quite neat thing is that we can use that in tests when we want to check that an array contains one item that we're expecting. </p>
<p>This is much better than having to specify a specific index which is what we often seem to end up doing in Java/C#.</p>
<p>We therefore end up with (RSpec) tests similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">response.<span style="color:#9900CC;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Some error message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> be_true</pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/YqmEg8BLwhs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/08/ruby-checking-an-array-contains-an-item/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/09/08/ruby-checking-an-array-contains-an-item/</feedburner:origLink></item>
		<item>
		<title>jQuery UI Tabs: Changing selected tab</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/0qnw0M4PfuY/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 18:32:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2901</guid>
		<description><![CDATA[We're using the tabs part of the jQuery UI library on the project I'm currently working on and one thing we wanted to do was change the default tab that was being selected. The documentation suggested that one way to do this was to give the index of the tab we wanted selected when calling [...]]]></description>
			<content:encoded><![CDATA[<p>We're using the <a href="http://jqueryui.com/demos/tabs/">tabs part of the jQuery UI library</a> on the project I'm currently working on and one thing we wanted to do was change the default tab that was being selected. </p>
<p>The documentation suggested that one way to do this was to give the index of the tab we wanted selected when calling the tabs function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;.selector&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tabs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> selected<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Since we wanted to select the tab by name based on a value from the query string we thought it would probably be simpler if we could just set the selected tab using a css class.</p>
<p>Our initial thought was that we could put the 'ui-tabs-hide' class on the divs that we wanted to hide and then not put that class on the one that we wanted to show.</p>
<p>Unfortunately that didn't work and the first tab was still being selected&#8230;</p>
<p>We downloaded version <a href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.js">1.8.2</a> of the library  via <a href="http://code.google.com/apis/libraries/devguide.html#jqueryUI">Google's CDN</a> (which seems really cool!) and were able to see that our class was actually intentionally being removed!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10523
10524
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">selected</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">anchors</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// check for length avoids error when initializing empty list</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">panels</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">selected</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ui-tabs-hide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Luckily a little further down the file there is a comment which explains some other ways to manipulate the selected tab:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10749
10750
10751
10752
10753
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">			<span style="color: #006600; font-style: italic;">// Selected tab</span>
			<span style="color: #006600; font-style: italic;">// use &quot;selected&quot; option or try to retrieve:</span>
			<span style="color: #006600; font-style: italic;">// 1. from fragment identifier in url</span>
			<span style="color: #006600; font-style: italic;">// 2. from cookie</span>
			<span style="color: #006600; font-style: italic;">// 3. from selected class attribute on &lt;li&gt;</span></pre></td></tr></table></div>

<p>We need to put the class 'ui-tabs-selected' on the appropriate &lt;li&gt; and then that will be the one that gets selected.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/0qnw0M4PfuY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/</feedburner:origLink></item>
		<item>
		<title>Ruby: Hash ordering</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/jMVsm_wMUbY/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/07/ruby-hash-ordering/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 03:52:32 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2896</guid>
		<description><![CDATA[The application that I'm working on at the moment is deployed into production on JRuby but we also use the C Ruby 1.8.7 interpreter when developing locally since this allows us much quicker feedback. As a result we sometimes come across interesting differences in the way that the two runtimes work. One that we noticed [...]]]></description>
			<content:encoded><![CDATA[<p>The application that I'm working on at the moment is deployed into production on JRuby but we also use the C Ruby 1.8.7 interpreter when developing locally since this allows us much quicker feedback.</p>
<p>As a result we sometimes come across interesting differences in the way that the two runtimes work.</p>
<p>One that we noticed yesterday is that if you create a hash, the order of the keys in the hash will be preserved when interpreted on JRuby but not with the C Ruby interpreter.</p>
<p>For example if we create the following hash in Ruby 1.8.7 it will be resorted into alphabetical order:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ruby-1.8.7 &gt; a_hash = { :a =&gt; 1, :d =&gt; 2, :c =&gt; 3 }
 =&gt; {:a=&gt;1, :c=&gt;3, :d=&gt;2}</pre></div></div>

<p>Whereas in JRuby it will maintain its order:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">jruby-1.5.1 &gt; a_hash = { :a =&gt; 1, :d =&gt; 2, :c =&gt; 3 }
 =&gt; {:a=&gt;1, :d=&gt;2, :c=&gt;3}</pre></div></div>

<p>We found <a href="http://www.ruby-forum.com/topic/153146">a post on the Ruby mailing list from a couple of years ago</a> which pointed out that from Ruby 1.9 the order is in fact maintained.</p>
<p>However, Gregory Seidman also pointed out that&#8230;</p>
<blockquote><p>
Hashes are inherently unordered. Hashes provide amortized O(1) insertion and retrieval of elements by key, and that's it. If you need an ordered set of pairs, use an array of arrays. Yes, this is a pet peeve of mine.
</p></blockquote>
<p>Since that is indeed what we want we've created an array of arrays in our code instead. The code to retrieve values from the array of arrays is a bit more verbose but at least the order is now guaranteed!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/jMVsm_wMUbY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/07/ruby-hash-ordering/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/09/07/ruby-hash-ordering/</feedburner:origLink></item>
		<item>
		<title>Flow in software teams</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/KYDkKsQz7Zg/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/05/flow-in-software-teams/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 17:34:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2892</guid>
		<description><![CDATA[My former colleague Greg Gigon has written an interesting blog post where he talks about the pain that we cause ourselves by multi-tasking, a point which Kevin Fox also makes on the Theory of Constraints blog. I think the overall point that he makes is very true: We can switch our attention quickly from one [...]]]></description>
			<content:encoded><![CDATA[<p>My former colleague Greg Gigon has written an interesting blog post where he talks about <a href="http://blog.gigoo.org/2010/08/18/agile-multitasking-context-switching/">the pain that we cause ourselves by multi-tasking</a>, a point which <a href="http://theoryofconstraints.blogspot.com/2007/07/multi-tasking-why-projects-take-so-long.html">Kevin Fox also makes on the Theory of Constraints blog</a>.</p>
<p>I think the overall point that he makes is very true:</p>
<blockquote><p>
We can switch our attention quickly from one task to another. But … is it good for our brain? Is it good for the work we are doing? Are we really more productive?
</p></blockquote>
<p>I've often found that when I try and switch between multiple different tasks I end up finishing none of them and it's certainly true that twitter, facebook and emails can be amazingly distracting. </p>
<p>Hopefully those types of distractions are less of an issue when working on a software development team, particularly if the team are pair programming.</p>
<h3>Project influenced context switching</h3>
<p>While I agree with Greg that it's good to try and make sure that we're only working on one task at a time, I think that to an extent there is always going to be some context switching involved if we have a group of people working together.</p>
<p>Greg touches on this briefly in his post: </p>
<blockquote><p>
When we are in a despair and need answers we are not worrying about anyone else. The goal is to ask or tell to achieve whatever we are after. We are not taking into consideration that we might be disturbing someone else Flow.
</p></blockquote>
<p>To me it seems that focusing on the flow of an individual individual/pair is a bit of a local optimisation when we're talking about software teams.</p>
<p>For example if a pair are working on a piece of functionality orthogonal to what another pair are working on then should they interrupt that pair if they need to talk about something?</p>
<p>Interrupting them possible would break their 'flow' but it might give one or both pairs an insight into the problem which they wouldn't have otherwise had. </p>
<p>From my experience pairs interrupting each other often helps reduce the amount of time that we spend building the wrong thing. </p>
<p>An alternative way to still gain this knowledge without interrupting people straight away would be to delay the conversation until a later time but perhaps the best moment for gaining that knowledge has now gone?</p>
<h3>Counter productive context switching</h3>
<p>The only time when interrupting a pair does seem counter productive is if you're doing so to ask a question whose answer could easily be found out with a quick bit of Googling.</p>
<p>In that case I completely agree with Greg that we should first think about the impact our interruption will have on our colleagues.</p>
<h3>Overall</h3>
<p>The topic of flow/context switching is an interesting one but I think we need to be careful about having it as our main goal on software delivery teams.</p>
<p>I wrote about <a href="http://www.markhneedham.com/blog/2008/10/17/pair-programming-pair-flow/">pair flow</a> a while ago where I described some ways to achieve more 'flow' within a pair but my current line of thinking is that we'll still have some interruption between pairs and that's not necessarily a bad thing.</p>
<h4>An approach to ponder&#8230;</h4>
<p><a href="http://twitter.com/mikewagg">Mike Wagg</a> once suggested that pairs should work in 25 minute pomodoros and only be interruptible when they're on a break from one.</p>
<p>This seems like an interesting idea but communication would end up being much more structured/time boxed/weird?!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/KYDkKsQz7Zg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/05/flow-in-software-teams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/09/05/flow-in-software-teams/</feedburner:origLink></item>
		<item>
		<title>Design Simplicity: Partially updating an object</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/7opksSeYFoo/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/05/design-simplicity-partially-updating-an-object/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 17:32:00 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2887</guid>
		<description><![CDATA[One of the most common discussions that I have with my colleagues is around designing bits of code in the simplest way possible. I've never quite been able to put my finger on exactly what makes a design simple and there is frequently disagreement about what is even considered simple. On the last project I [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common discussions that I have with my colleagues is around designing bits of code in the simplest way possible.</p>
<p>I've never quite been able to put my finger on exactly what makes a design simple and there is frequently disagreement about what is even considered simple.</p>
<p>On the last project I worked on we had an interesting problem where we wanted to partially update different parts of an object from different pages of the application.</p>
<div style="float:right">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2010/08/design-simplicity.jpg" alt="design-simplicity.jpg" border="0" width="457" height="334" />
</div>
<p>We had access to the id of the object from the URL so my initial thought was that when we submitted each page we could load the full object from the database and update it with the values that had just been submitted from that page.</p>
<p>The problem with that solution was that it meant we needed to make another database call without any real benefit from doing so.</p>
<p>We had already created objects representing the data submitted from these pages so <a href="http://twitter.com/christianralph">Christian</a> suggested that an alternative approach would be to create NHibernate mappings for those objects instead so that we could just map the updated values straight to the database.</p>
<p>We had bit of duplication in our objects as we had one object representing every bit of data the user had provided so far and then two smaller objects just representing the data provided for each of the pages.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TheObject
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Page1Property <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Page2Property <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PartialObject1
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Page1Property <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PartialObject2
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Page2Property <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We then created NHibernate mappings for each of those objects:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TheObjectMapping <span style="color: #008000;">:</span> ClassMap<span style="color: #008000;">&lt;</span>TheObject<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	Map<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Page1Property</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	Map<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Page2Property</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PartialObject1Mapping <span style="color: #008000;">:</span> ClassMap<span style="color: #008000;">&lt;</span>PartialObject1<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	Map<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Page1Property</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PartialObject2Mapping <span style="color: #008000;">:</span> ClassMap<span style="color: #008000;">&lt;</span>PartialObject2<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	Map<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Page2Property</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>When loading the object from the database onto the page we used 'TheObject' and its associated mappings and when updating the object from the individual pages we could use the partial object mappings.</p>
<p>I think this was quite a neat approach as it allowed us to reduce the complexity of our controller code when updating an object as well as removing the need for one trip to the database.</p>
<p>The trade off was that we ended up writing more mapping code but that seemed to be a reasonable trade off to make.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/7opksSeYFoo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/05/design-simplicity-partially-updating-an-object/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/09/05/design-simplicity-partially-updating-an-object/</feedburner:origLink></item>
		<item>
		<title>Objective C: Observations</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/szyywRRCh4g/</link>
		<comments>http://www.markhneedham.com/blog/2010/08/31/objective-c-observations/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 18:27:10 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Objective C]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2883</guid>
		<description><![CDATA[I've been playing around with Objective C over the last month or so and although my knowledge of the language is still very much limited I thought it'd be interesting to describe some of the things about the language that I think are quite interesting and others that keep catching me out. Protocols I touched [...]]]></description>
			<content:encoded><![CDATA[<p>I've been playing around with Objective C over the last month or so and  although my knowledge of the language is still very much limited I thought it'd be interesting to describe some of the things about the language that I think are quite interesting and others that keep catching me out.</p>
<h3>Protocols</h3>
<p>I touched on protocols a bit in <a href="http://www.markhneedham.com/blog/2010/08/04/objective-c-parsing-an-xml-file/">my first post</a> but they seem like an interesting middle ground between interfaces and duck typing.</p>
<p>I like the fact that protocols can define optional methods so that if we're not interested in some parts of the protocol we can just ignore those parts.</p>
<p>From the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15">documentation page</a>:</p>
<blockquote><p>
Protocols free method declarations from dependency on the class hierarchy, so they can be used in ways that classes and categories cannot. Protocols list methods that are (or may be) implemented somewhere, but the identity of the class that implements them is not of interest. What is of interest is whether or not a particular class conforms to the protocol
</p></blockquote>
<h3>Smalltalkish style method names</h3>
<p>We <a href="http://www.markhneedham.com/blog/2009/05/21/coding-dojo-15-smalltalk/">played with Smalltalk in a coding dojo a bit last year</a> and the first thing that I noticed with Objective C is that the method names are very similar to those in Smalltalk. </p>
<p>I think this influences the way that we define the method name and its parameters as you try and define those in such a way that when you call the method it will read better.</p>
<p>For example I created the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UILabel <span style="color: #002200;">*</span>aLabel	<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self createLabelFrom<span style="color: #002200;">:</span>project withXCoordinate<span style="color: #002200;">:</span>x withYCoordinate<span style="color: #002200;">:</span>y<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If I didn't have to name the parameters when calling the method I doubt I would have used such descriptive names. I would have just used 'x' and 'y' as the names!</p>
<h3>All methods are public/Defining methods in header files</h3>
<p>As I understand it all the methods defined on an object are available to any other object to call i.e. all the methods are public</p>
<p>I've read about others using <a href="http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/objectivec/Articles/ocCategories.html">categories</a> to simulate the idea of having non public methods but I haven't tried anything myself yet.</p>
<p>Interestingly we get a compiler warning when trying to call methods on an object if those methods haven't been defined in the appropriate header file although the code still seems to execute fine at run time.</p>
<h3>Messages not method calls</h3>
<p>One other thing that I sometimes forget is that we're dealing with messages rather than method calls.</p>
<p>We still need to send the message to 'self' even if it's a message being sent to another method on the same object.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/szyywRRCh4g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/08/31/objective-c-observations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/08/31/objective-c-observations/</feedburner:origLink></item>
		<item>
		<title>Rails: Populating a dropdown list using 'form_for'</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/VAcoyR1StNE/</link>
		<comments>http://www.markhneedham.com/blog/2010/08/31/rails-populating-a-dropdown-list-using-form_for/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 01:22:14 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2876</guid>
		<description><![CDATA[Last week we were trying to make use of Rails' 'form_for' helper to populate a dropdown list with the values of a collection that we'd set to an instance variable in our controller. My colleague pointed out that we'd need to use 'collection_select' in order to do this. We want to put the values in [...]]]></description>
			<content:encoded><![CDATA[<p>Last week we were trying to make use of <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html">Rails' 'form_for' helper</a> to populate a dropdown list with the values of a collection that we'd set to an instance variable in our controller.</p>
<p>My colleague pointed out that we'd need to use '<a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select">collection_select</a>' in order to do this.</p>
<p>We want to put the values in the 'foos' collection onto the page. 'foos' is a hash which defines some display values and their corresponding values like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FooController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> index
		<span style="color:#008000; font-style:italic;"># @mainFoo defined with some value irrelevant to this example</span>
		<span style="color:#0066ff; font-weight:bold;">@foos</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;value1&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">&quot;value2&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The code we need to do this looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;%form_for @mainFoo, :html =&gt; { :name=&gt; &quot;ourForm&quot;, :id=&gt; &quot;ourForm&quot; },:url =&gt; {:controller =&gt; &quot;foo&quot;, :action =&gt; :bar} do | mainFoo |%&gt;
&nbsp;
&lt;%= foo.collection_select :foo_id, {&quot;Please select&quot;=&gt;&quot;&quot;}.merge!(@foos), :last, :first, { :selected =&gt; &quot;Please select&quot; }, {:name =&gt; &quot;foo&quot;, :id =&gt; &quot;foo&quot;} %&gt;</pre></div></div>

<p>The method signature that we're passing those parameters to reads like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> collection_select<span style="color:#006600; font-weight:bold;">&#40;</span>method, collection, value_method, text_method, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, html_options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In this case we want the selected value to always be 'Please select' so we need to specify that in the 'options' hash. If 'selected' wasn't specified in the hash then the code would try to make the selected value @mainFoo.foo_id which in this case has no value anyway.</p>
<p>The other thing which I thought was quite neat is the way that you need to provided the 'value_method' and 'text_method' as parameters so that the dropdown list can be constructed with the appropriate labels and values.</p>
<p>In this case we have the display values as the keys in the hash and the values as the values in the hash so we can retrieve those entries from the collection by using the ':last' and ':first' methods.</p>
<p>We end up in the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">      <span style="color:#9966CC; font-weight:bold;">def</span> options_from_collection_for_select<span style="color:#006600; font-weight:bold;">&#40;</span>collection, value_method, text_method, selected = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        options = collection.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>element<span style="color:#006600; font-weight:bold;">|</span>
          <span style="color:#006600; font-weight:bold;">&#91;</span>element.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>text_method<span style="color:#006600; font-weight:bold;">&#41;</span>, element.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>value_method<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This creates an array of arrays which is used later on.</p>
<p>In our case the values passing through this method would read like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">        collection = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;value1&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">&quot;value2&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
        options = collection.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>element<span style="color:#006600; font-weight:bold;">|</span>
          <span style="color:#006600; font-weight:bold;">&#91;</span>element.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>text_method<span style="color:#006600; font-weight:bold;">&#41;</span>, element.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>value_method<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"> =&gt; [[&quot;value1&quot;, 1], [&quot;value2&quot;, 2]]</pre></div></div>

<p>I was initially a bit confused about how we were able to call the 'collection_select' method on 'mainFoo' but <a href="http://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb">a quick browse of the ActionPack code</a> showed that 'mainFoo' actually represents a wrapper around that object rather than the object itself. </p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/VAcoyR1StNE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/08/31/rails-populating-a-dropdown-list-using-form_for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/08/31/rails-populating-a-dropdown-list-using-form_for/</feedburner:origLink></item>
		<item>
		<title>Coding: Mutating parameters</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/agumGokKNT4/</link>
		<comments>http://www.markhneedham.com/blog/2010/08/26/coding-mutating-parameters/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 07:47:23 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2872</guid>
		<description><![CDATA[One of the earliest rules of thumb that I was taught by my colleagues is the idea that we should try and avoid mutating/changing values passed into a function as a parameter. The underlying reason as I understand it is that if you're just skimming through the code you wouldn't necessarily expect the values of [...]]]></description>
			<content:encoded><![CDATA[<p>One of the earliest rules of thumb that I was taught by my colleagues is the idea that we should try and avoid mutating/changing values passed into a function as a parameter.</p>
<p>The underlying reason as I understand it is that if you're just skimming through the code you wouldn't necessarily expect the values of incoming parameters to be different depending where in the function they're used.</p>
<p>I think the most dangerous example of this is when we completely change the value of a parameter, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClass
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">BigDecimal</span> doSomeCalculationsOn<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
		value <span style="color: #339933;">=</span> value.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3.2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// some other calculation  on value...</span>
		<span style="color: #666666; font-style: italic;">// and we keep on re-assigning value until we return the value</span>
		<span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this case the function is really small so maybe it doesn't make that much difference readability wise but I still think it would be better if we didn't reassign the result of the calculation to 'value' but instead used a new variable name.</p>
<p>It wouldn't require a very big change to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClass
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">BigDecimal</span> doSomeCalculationsOn<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
		<span style="color: #003399;">BigDecimal</span> newValue <span style="color: #339933;">=</span> value.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3.2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// some other calculation  on newValue...</span>
		<span style="color: #666666; font-style: italic;">// and we keep on re-assigning newValue until we return the newValue</span>
		<span style="color: #000000; font-weight: bold;">return</span> newValue<span style="color: #339933;">;</span>  
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Unless the function in question is the <a href="http://en.wikipedia.org/wiki/Identity_function">identity function</a> I find it very weird when I read code which seems to return the same value that's been passed into the function.</p>
<p>The other way that function parameters get changed is when we mutate the values directly. The <a href="http://www.markhneedham.com/blog/2010/01/23/coding-the-collecting-parameter-pattern/">collecting parameter pattern</a> is a good example of this.</p>
<p>That seems to be a more common pattern and since the function names normally reveal intent better it's normally less confusing. </p>
<p>It does become <a href="http://www.markhneedham.com/blog/2009/09/16/coding-watch-out-for-mutable-code/">more problematic if we're mutating an object in loads of places based on conditional statements because we can lose track of how many times it's been changed</a>.</p>
<p>Interestingly some of the code for <a href="http://github.com/rails/rails/blob/master/actionpack">ActionPack</a> makes use of both of these approaches in the same function!</p>
<p><a href="http://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_options_helper.rb">form_options_helper.rb</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>564
565
566
567
568
569
570
571
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">      <span style="color:#9966CC; font-weight:bold;">def</span> to_collection_select_tag<span style="color:#006600; font-weight:bold;">&#40;</span>collection, value_method, text_method, options, html_options<span style="color:#006600; font-weight:bold;">&#41;</span>
        html_options = html_options.<span style="color:#9900CC;">stringify_keys</span>
        add_default_name_and_id<span style="color:#006600; font-weight:bold;">&#40;</span>html_options<span style="color:#006600; font-weight:bold;">&#41;</span>
        ...
        <span style="color:#9900CC;">content_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span>
          <span style="color:#996600;">&quot;select&quot;</span>, add_options<span style="color:#006600; font-weight:bold;">&#40;</span>options_from_collection_for_select<span style="color:#006600; font-weight:bold;">&#40;</span>collection, value_method, text_method, <span style="color:#ff3333; font-weight:bold;">:selected</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> selected_value, <span style="color:#ff3333; font-weight:bold;">:disabled</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> disabled_value<span style="color:#006600; font-weight:bold;">&#41;</span>, options, value<span style="color:#006600; font-weight:bold;">&#41;</span>, html_options
        <span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><a href="http://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb">form_helper.rb</a></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">        <span style="color:#9966CC; font-weight:bold;">def</span> add_default_name_and_id<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= tag_name_with_index<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#008000; font-style:italic;"># and so on</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I'm not sure how exactly I'd change that function so that it didn't mutate 'html_options' but I'm thinking perhaps something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#9966CC; font-weight:bold;">def</span> create_html_options_with_default_name_and_id<span style="color:#006600; font-weight:bold;">&#40;</span>html_options<span style="color:#006600; font-weight:bold;">&#41;</span>
          options = html_options.<span style="color:#9900CC;">stringify_keys</span>
          <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= tag_name_with_index<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#008000; font-style:italic;"># and so on</span>
	<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And we could then change the other method to call it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">      <span style="color:#9966CC; font-weight:bold;">def</span> to_collection_select_tag<span style="color:#006600; font-weight:bold;">&#40;</span>collection, value_method, text_method, options, html_options<span style="color:#006600; font-weight:bold;">&#41;</span>
	   html_options_with_defaults = create_html_options_with_default_name_and_id<span style="color:#006600; font-weight:bold;">&#40;</span>html_options<span style="color:#006600; font-weight:bold;">&#41;</span>
        ...
        <span style="color:#9900CC;">content_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span>
          <span style="color:#996600;">&quot;select&quot;</span>, add_options<span style="color:#006600; font-weight:bold;">&#40;</span>options_from_collection_for_select<span style="color:#006600; font-weight:bold;">&#40;</span>collection, value_method, text_method, <span style="color:#ff3333; font-weight:bold;">:selected</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> selected_value, <span style="color:#ff3333; font-weight:bold;">:disabled</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> disabled_value<span style="color:#006600; font-weight:bold;">&#41;</span>, options, value<span style="color:#006600; font-weight:bold;">&#41;</span>, html_options_with_defaults
        <span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I guess you could argue that the new function is doing more than one thing but I don't think it's too bad.</p>
<p>Looking back on these code examples after writing about them I'm not as confident that mutating parameters is as confusing as I originally thought&#8230;!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/agumGokKNT4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/08/26/coding-mutating-parameters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/08/26/coding-mutating-parameters/</feedburner:origLink></item>
		<item>
		<title>Ruby: 'method_missing' and slightly misled by RubyMine</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/VMwYNdcqXO8/</link>
		<comments>http://www.markhneedham.com/blog/2010/08/23/ruby-method_missing-and-slightly-misled-by-rubymine/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 21:07:46 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2863</guid>
		<description><![CDATA[Another library that we're using on my project is ActionMailer and before reading through the documentation I was confused for quite a while with respect to how it actually worked. We have something similar to the following piece of code&#8230; Emailer.deliver_some_email &#8230;which when you click its definition in RubyMine takes you to this class definition: [...]]]></description>
			<content:encoded><![CDATA[<p>Another library that we're using on my project is <a href="http://am.rubyonrails.org/">ActionMailer</a> and before reading through the documentation I was confused for quite a while with respect to how it actually worked.</p>
<p>We have something similar to the following piece of code&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Emailer.<span style="color:#9900CC;">deliver_some_email</span></pre></div></div>

<p>&#8230;which when you click its definition in RubyMine takes you to this class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Emailer <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> some_email
		recipients <span style="color:#996600;">&quot;some@email.com&quot;</span>
		from <span style="color:#996600;">&quot;some_other_email@whatever.com&quot;</span>
		<span style="color:#008000; font-style:italic;"># and so on</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I initially thought that method was called 'deliver_some_mail' but having realised that it wasn't I was led to the 'magic' that is 'method_missing' on 'ActionMailer::Base' which is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActionMailer
...
	<span style="color:#9966CC; font-weight:bold;">class</span> Base
		<span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>method_symbol, <span style="color:#006600; font-weight:bold;">*</span>parameters<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
        		<span style="color:#9966CC; font-weight:bold;">if</span> match = matches_dynamic_method?<span style="color:#006600; font-weight:bold;">&#40;</span>method_symbol<span style="color:#006600; font-weight:bold;">&#41;</span>
          		<span style="color:#9966CC; font-weight:bold;">case</span> match<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
            			<span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'create'</span>  <span style="color:#9966CC; font-weight:bold;">then</span> new<span style="color:#006600; font-weight:bold;">&#40;</span>match<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">*</span>parameters<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">mail</span>
            			<span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'deliver'</span> <span style="color:#9966CC; font-weight:bold;">then</span> new<span style="color:#006600; font-weight:bold;">&#40;</span>match<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">*</span>parameters<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">deliver</span>!
            			<span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'new'</span>     <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
            			<span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#9966CC; font-weight:bold;">super</span>
          		<span style="color:#9966CC; font-weight:bold;">end</span>
        		<span style="color:#9966CC; font-weight:bold;">else</span>
				<span style="color:#9966CC; font-weight:bold;">super</span>
        		<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The 'matches_dynamic_method?' function allows us to extract 'some_email' from the 'method_symbol'. That value is then passed into the object's initializer method and is eventually called executing all the code inside that method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">        <span style="color:#9966CC; font-weight:bold;">def</span> matches_dynamic_method?<span style="color:#006600; font-weight:bold;">&#40;</span>method_name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
          method_name = method_name.<span style="color:#9900CC;">to_s</span>
          <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>create<span style="color:#006600; font-weight:bold;">|</span>deliver<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>_a<span style="color:#006600; font-weight:bold;">-</span>z<span style="color:#006600; font-weight:bold;">&#93;</span>\w<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>method_name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>new<span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>method_name<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Reading through the documentation, the author gives the following reasons for having separate 'create' and 'deliver' methods:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ApplicationMailer.create_signed_up(&quot;david@loudthinking.com&quot;)  # =&gt; tmail object for testing
ApplicationMailer.deliver_signed_up(&quot;david@loudthinking.com&quot;) # sends the email
ApplicationMailer.new.signed_up(&quot;david@loudthinking.com&quot;)     # won't work!</pre></div></div>

<p>In C# or Java I think we'd probably use another object to build up the message and then pass that to the 'Emailer' to deliver it so it's quite interesting that both these responsibilities are in the same class.</p>
<p>It also takes care of rendering templates and from what I can tell the trade off for having this much complexity in one class is that it makes it quite easy for the library's clients &#8211; we just have to extend 'ActionMailer::Base' and we have access to everything that we need.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/VMwYNdcqXO8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/08/23/ruby-method_missing-and-slightly-misled-by-rubymine/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/08/23/ruby-method_missing-and-slightly-misled-by-rubymine/</feedburner:origLink></item>
		<item>
		<title>Distributed Agile: Initial observations</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/uxz4UkFVj-w/</link>
		<comments>http://www.markhneedham.com/blog/2010/08/23/distributed-agile-initial-observations/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 02:52:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Distributed Agile]]></category>
		<category><![CDATA[distributed-agile]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2859</guid>
		<description><![CDATA[One of the reasons I wanted to come and work for ThoughtWorks in India is that I wanted to see how a distributed agile project is run and see the ways in which it differs to one which is done co-located. I worked on a project which was distributed between Sydney and Melbourne in 2008/2009 [...]]]></description>
			<content:encoded><![CDATA[<p>One of the reasons I wanted to come and work for ThoughtWorks in India is that I wanted to see how a distributed agile project is run and see the ways in which it differs to one which is done co-located.</p>
<p>I worked on a project which was distributed between Sydney and Melbourne in 2008/2009 and while some of the challenges seem to be quite similar to the ones we faced there, some are completely different.</p>
<h3>Emails</h3>
<p>Emails become much more prevalent when there are people in different locations and one thing that I learnt from the distributed project in Australia is that we have to be quite careful about the way that we phrase what we write.</p>
<p>It tends to work much better if we write in quite a defensive way using phrases like 'I think that' or 'it seems that' since it's very easy to misinterpret what's being said as we don't have the chance to understand a person's body language or tone.</p>
<p>A colleague pointed out to me that it's equally important to change the way that you read emails to give the other person the benefit of the doubt otherwise everything that you read may come across as being sarcastic and attacking.</p>
<h3>Conference calls</h3>
<p>As well as emails a lot of the communication between the people in Pune and Chicago is done through conference calls.</p>
<p>We also used this approach on the project I worked on in Australia and while it was still not as effective as face to face communication, the calls were always relatively smooth.</p>
<p>We seem to have more trouble on this project and people's voices often breakup so that you can't understand them at all. This leads to a bit of repetition in conversations and I can easily see how this would become an area of severe frustration.</p>
<p>I also learnt that I need to speak much more slowly on these calls or noone can understand what I'm saying!</p>
<h3>Time zones</h3>
<p>The time zone difference between Chicago and Pune at the moment is 10.5 hours so there is no overlap in the working days in the two locations which means we need to create an artificial overlap.</p>
<p>A normal working day for projects I've worked on in the UK/Australia would be around 9am &#8211; 6pm:</p>
<ul>
<li>9am in Pune is 10.30pm the previous day in Chicago</li>
<li>6pm in Pune is 7.30am in Chicago</li>
</ul>
<p>As a result the people in Chicago are asleep for nearly all of our working day and since the client is there as well the feedback cycle is a bit slower than it would be in the client was in the same building.</p>
<p>There does seem to be more scope to go down the wrong path when building a piece of functionality but as long as the client doesn't change their mind too drastically overnight or have their intent misunderstood then it seems relatively manageable.</p>
<p>A couple of years ago I saw a talk by the former Managing Director of ThoughtWorks India, Sitaraman, in which he outlined some of the challenges of offshore and <a href="http://www.markhneedham.com/blog/2008/09/24/onshore-or-offshore-the-concepts-are-the-same/">I wondered whether the concepts of successful delivery both on and offshore were the same</a>.</p>
<p>I'm still of a similar opinion but by being part of a distributed agile team for a few days I can see how much more tricky it is to achieve this than it would be in a co-located environment.</p>
<p>I'm sure there are many more things I will notice that are different as times goes on!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/uxz4UkFVj-w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/08/23/distributed-agile-initial-observations/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2010/08/23/distributed-agile-initial-observations/</feedburner:origLink></item>
	</channel>
</rss>
