<?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>Kickass Labs &#187; Programming</title>
	<atom:link href="http://www.kickasslabs.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kickasslabs.com</link>
	<description>We &#9829; code.</description>
	<lastBuildDate>Sun, 04 Jul 2010 03:00:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quick Hits: Becareful when returning inc&#8217;ed vars</title>
		<link>http://www.kickasslabs.com/2010/05/01/quick-hits-becareful-when-returning-inced-vars/</link>
		<comments>http://www.kickasslabs.com/2010/05/01/quick-hits-becareful-when-returning-inced-vars/#comments</comments>
		<pubDate>Sat, 01 May 2010 05:01:44 +0000</pubDate>
		<dc:creator>abel</dc:creator>
				<category><![CDATA[MonoTouch]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quick Hits]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[noob]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=473</guid>
		<description><![CDATA[I noticed this in some MonoTouch code that I wrote recently, but I think it makes sense across other languages as well.  Let&#8217;s say you write a method that returns an incremented variable like this:
private int MyAwesomeFunction(int pVar)
{
// coding magic
  return pVar++;
}
&#8230;where pVar = 10, the returned value will NOT be 11!  The returned [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed this in some MonoTouch code that I wrote recently, but I think it makes sense across other languages as well.  Let&#8217;s say you write a method that returns an incremented variable like this:</p>
<p><span style="color: #0000ff;">private int</span> MyAwesomeFunction(int pVar)<br />
{<br />
<span style="color: #008000;">// coding magic</span><br />
<span style="color: #008000;"> <span style="color: #0000ff;"> return</span></span> pVar++;<br />
}</p>
<p>&#8230;where pVar = 10, the returned value will NOT be 11!  The returned value will still be 10.  If you want to return an incremented value for pVar, do the following:</p>
<p><code><span style="color: #0000ff;">private  int</span> MyAwesomeFunction(int pVar)<br />
{<br />
<span style="color: #008000;">//  coding magic</span></code><br />
<code> <span style="color: #0000ff;">return <span style="color: #000000;">++</span></span>pVar; <span style="color: #008000;">// or </span></code><span style="color: #008000;"><code>return   pVar+1</code></span>;<br />
<code>}</code></p>
<p>Just a case of postfix vs prefix.  In the previous example, the variable never gets incremented because the postfix operator would get evaluated after the line is processed, but that never happens because we&#8217;re returning the pVar.  By flipping to a prefix operator, we guarantee that we evaluate the opperator against the variable before we return it.</p>
<p>It&#8217;s also worth mentioning that being a little verbose here is also good.  Explicitly stating that you&#8217;re adding 1 to the variable (as displayed in the commented code) is the same number of characters and we eliminate the confusion over what&#8217;s getting returned.</p>
<p>Happy Coding!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2010%2F05%2F01%2Fquick-hits-becareful-when-returning-inced-vars%2F&amp;linkname=Quick%20Hits%3A%20Becareful%20when%20returning%20inc%26%238217%3Bed%20vars"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2010/05/01/quick-hits-becareful-when-returning-inced-vars/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TDD Makes Software Go Faster</title>
		<link>http://www.kickasslabs.com/2009/08/24/tdd-makes-software-go-faster/</link>
		<comments>http://www.kickasslabs.com/2009/08/24/tdd-makes-software-go-faster/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 01:09:57 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=392</guid>
		<description><![CDATA[One of the gripes I hear about TDD is that it takes too much time.  People look at the idea of it and think, &#8220;So I have to write code and tests and update the tests when the code changes? WTF?&#8221;
My message to you is: TDD takes less time, not more.
Today&#8217;s example: I&#8217;m writing [...]]]></description>
			<content:encoded><![CDATA[<p>One of the gripes I hear about <a href="http://en.wikipedia.org/wiki/Test-driven_development" title="TDD on Wikipedia" target="tddwiki">TDD</a> is that it takes too much time.  People look at the idea of it and think, &#8220;So I have to write code <i>and</i> tests and update the tests when the code changes? WTF?&#8221;</p>
<p>My message to you is: TDD takes <i>less</i> time, not more.</p>
<p>Today&#8217;s example: I&#8217;m writing some code that uses a parsing library written by a cow-orker.  He&#8217;s a sharp guy, and the library generally works well, but it has a couple of undocumented behaviors &#8211; they&#8217;re mostly benign, and probably work for other users of the library, but for me it&#8217;s stuff that would result in misbehavior <i>way</i> up the call stack, or in another module entirely.</p>
<p>While I&#8217;m test-driving the code that uses this library, I notice the library itself does not have automated tests.  The natural-feeling thing for me to do here is to test some sample calls to the lib to make sure it does what I expect.  I know it&#8217;s not exactly canonical behavior to test outside libs that are assumed to be functioning, but it <i>is</i> good form to write tests that teach you about the system you&#8217;re building, and I needed to learn whether this library was going to parse my input in the way that I expected.  The documentation wasn&#8217;t there, I wasn&#8217;t going to figure it out by inspecting the code (which is relatively complex), so tests it was.</p>
<p>Surprise, surprise &#8211; I found out that the library did NOT behave as I expected in every case.  Had I not taken twenty minutes to write tests to introduce confidence in the parts of the system I didn&#8217;t know well, I&#8217;d probably have spent a couple hours debugging and looking in the wrong places for the problems I was having downstream.  It also led me to a relatively simple solution where I extended the library class and just overrode the elements related to my handful of problematic cases, without changing his library or breaking code that depends on it.</p>
<p>As an added bonus, not only do I know what the documentation does not tell me, but I&#8217;ll also know if and when my colleague makes changes to his library that break the expectations of <i>my</i> code.</p>
<p>So I&#8217;ve reduced debugging time, increased my confidence in my code, and protected myself against future changes.  I see it time and again &#8211; the time <i>saved</i> by writing the extra test code is well out of proportion to the time spent writing it.</p>
<p>There&#8217;s an exception here for developers or shops that are fetishistic about 100% test coverage, and do not judiciously use automated testing to learn about the system, drive design, and catch regressions.  Writing tests to cover trivial/obvious code (i.e., writing tests that do not <i>teach you about the system)</i> is a time-suck.  I&#8217;d avoid it, if I were you.</p>
<p>As long as I&#8217;m raving about TDD, I have to recommend the work of Kent Beck.  I <a href="http://twitter.com/KentBeck" title="Kent Beck on Twitter" target="kbtwitter">follow him on Twitter</a>, I <a href="http://www.threeriversinstitute.org/blog/" title="Three Rivers Institute Blog" target="kbblog">read his blog</a>, and <a href="http://www.amazon.com/gp/product/0321146530?ie=UTF8&#038;tag=kickasslabs-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321146530" title="Test-Driven Development: By Example" target="kbbook">his book on TDD</a> changed how I write software &#8211; I count it among the indispensible books on my shelf, with the GoF Design Patterns and Knuth.  Check him out if you&#8217;re interested in deep thought about how software gets made.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F08%2F24%2Ftdd-makes-software-go-faster%2F&amp;linkname=TDD%20Makes%20Software%20Go%20Faster"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/08/24/tdd-makes-software-go-faster/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Hits: Unit Testing iPhone Apps</title>
		<link>http://www.kickasslabs.com/2009/07/05/quick-hits-unit-testing-iphone-apps/</link>
		<comments>http://www.kickasslabs.com/2009/07/05/quick-hits-unit-testing-iphone-apps/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 19:04:35 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=381</guid>
		<description><![CDATA[I have a few things to add to the woefully incomplete official documentation on setting up automated tests in your iPhone apps:

You need to add your main application executable target as a direct dependency of the test target, so that you&#8217;re always testing against your latest build.  Do this by double-clicking on the test [...]]]></description>
			<content:encoded><![CDATA[<p>I have a few things to add to the woefully incomplete <a href="http://developer.apple.com/IPhone/library/documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html" title="iPhone Development Guide: Unit Testing Applications" target="iphonetestdocs">official documentation on setting up automated tests in your iPhone apps</a>:</p>
<ol>
<li>You need to add your main application executable target as a direct dependency of the test target, so that you&#8217;re always testing against your latest build.  Do this by double-clicking on the test target, going to the &#8220;General&#8221; pane in the properties dialog, and adding your app under &#8220;Direct Dependencies&#8221;.  (This was actually mentioned in the OCUnit tutorial for Cocoa, but not the one about iPhone testing.)</li>
<li>Your linker needs to know about the objects you&#8217;re testing.  An easy way to do this is to add the .m files for those classes to the &#8220;Compile Sources&#8221; group in your test target.  You&#8217;ll also have to make sure you link against any frameworks used by these objects.  (You could also tell your app target to export all symbols, then link your test target to it as you would a library.) (Thanks to Chris Hanson for pointing out this procedural improvement in comments.) <del datetime="2009-07-06T02:48:59+00:00">You need to explicitly link the object files of the classes you&#8217;re testing.  These are the &#8220;.o&#8221; files in your build folder.  To do this: Double-click on the test target, go to the &#8220;General&#8221; pane, add a new item under &#8220;Linked Libraries&#8221;.  In the dialog that pops up, choose &#8220;Add Other&#8230;&#8221; and add your class&#8217;s .o file.</del></li>
<li>When you run your tests, one failure looks like two:  Failed tests show up in Xcode as errors (just like compile errors, &#038;c). Any test failure triggers a second error, and you&#8217;ll see something like &#8220;Failed tests for architecture &#8216;i386&#8242; (GC OFF)&#8221;. The docs never say so, but this appears to be normal.  Fix the failing test, and it goes away.</li>
</ol>
<p>Anything else to add?  Drop us a comment!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F07%2F05%2Fquick-hits-unit-testing-iphone-apps%2F&amp;linkname=Quick%20Hits%3A%20Unit%20Testing%20iPhone%20Apps"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/07/05/quick-hits-unit-testing-iphone-apps/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>UINavigationController Tricks</title>
		<link>http://www.kickasslabs.com/2009/07/03/uinavigationcontroller-tricks/</link>
		<comments>http://www.kickasslabs.com/2009/07/03/uinavigationcontroller-tricks/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 22:39:03 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=366</guid>
		<description><![CDATA[For an iPhone UI I&#8217;m developing, I need to have one UINavigationController nested inside another, and to have the inner UINavigationController&#8217;s events push a view on to the outer one&#8217;s stack.  CocoaTouch didn&#8217;t give this to me for free, but there was a simple solution.
This post assumes that you&#8217;re familiar with the fundamentals of [...]]]></description>
			<content:encoded><![CDATA[<p>For an iPhone UI I&#8217;m developing, I need to have one UINavigationController nested inside another, and to have the inner UINavigationController&#8217;s events push a view on to the outer one&#8217;s stack.  CocoaTouch didn&#8217;t give this to me for free, but there <i>was</i> a simple solution.</p>
<p>This post assumes that you&#8217;re familiar with the fundamentals of iPhone programming, including view controllers, UINavigationController, and delegates.  There is <a href="http://www.kickasslabs.com/~kal_www/kickasslabs.com/wp-content/uploads/2009/07/navtest.tgz" title="UINavigationController Tricks sample code">sample code for this post</a>, which is released under version 2 of the <a href="http://sam.zoy.org/wtfpl/" title="WTFPL">WTFPL</a>.  </p>
<p><b>The Problem</b></p>
<p>I&#8217;m working on a multi-step UI for a game.  Expressing these steps as a regular drill-down table-style UI on the iPhone felt cumbersome, and games can&#8217;t afford for processes to feel cumbersome; people will stop playing.  One solution that occurred to me was batching related sets of steps in a smaller navigation table &#8211; so in Step 1 you&#8217;d drill through substeps 1A, 1B, and 1C before moving to Step 2.  The fact that the whole view wouldn&#8217;t be replaced with every choice seemed like it would be less destructive of the user&#8217;s mental context, and the chunking of substeps should make it easier for the users to wrap their heads around the process.  (No word yet on whether this solves my UI problem, but I like it so far.)</p>
<p>My UI solution contained its own technical problem, though: If I&#8217;m expressing the process steps in a UINavigationController, and expressing the substeps in a nested UINavigationController, how does the inner navigation controller notify the outer one that the user&#8217;s last substep choice completes that step and it&#8217;s time to move to the next step &#8211; or in programmatic terms, how does the inner UINavigationController tell the outer one to push a new view onto the outer one&#8217;s stack?</p>
<p>Or, putting it more visually, how do I get from here:</p>
<p><img src="http://www.kickasslabs.com/~kal_www/kickasslabs.com/wp-content/uploads/2009/07/navcontricks1.png" alt="navcontricks1" title="navcontricks1" width="414" height="770" class="aligncenter size-full wp-image-373" /></p>
<p>&#8230;to here:</p>
<p><img src="http://www.kickasslabs.com/~kal_www/kickasslabs.com/wp-content/uploads/2009/07/navcontricks2.png" alt="navcontricks2" title="navcontricks2" width="414" height="770" class="aligncenter size-full wp-image-374" /></p>
<p>In the non-nested situation when you wanted to push a new view onto a UINavigationController&#8217;s stack, you&#8217;d do the following in the current view:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self.navigationController pushViewController<span style="color: #002200;">:</span>nextViewController animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>So in my nested case, I need to do that, but pushing onto the outer navigation controller&#8217;s stack from a view controller on the inner navigation controller&#8217;s stack.  I tried a number of naive (but sensible-seeming) targets for the pushViewController:animated: action, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">self.navigationController.navigationController
self.navigationController.parentViewController.navigationController</pre></div></div>

<p>Nothing worked.  I set a breakpoint and drilled down through the members of self.navigationController, and no path to that outer UINavigationController was apparent.</p>
<p><b>The Solution</b></p>
<p>While investigating the innards of UINavigationController, I stumbled upon a writable property that looked like it might help: The delegate.  When creating the inner UINavigationController, I added one line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">innerNavCntlr.delegate <span style="color: #002200;">=</span> self; <span style="color: #11740a; font-style: italic;">// THIS IS THE MAGIC, PART 1</span></pre></div></div>

<p>Keep in mind that this is called in the view controller on top of the outer navigation controller&#8217;s stack, so &#8220;self&#8221; has access to the outer navigation controller.</p>
<p>When the view on top of my inner navigation controller&#8217;s stack is ready to signal the outer navigation controller, I do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// THIS IS THE MAGIC PART 2</span>
UIViewController <span style="color: #002200;">*</span>topVC <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UIViewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>self.navigationController.delegate;
<span style="color: #002200;">&#91;</span>topVC.navigationController pushViewController<span style="color: #002200;">:</span>nextCntlr animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>A couple of notes:  First, the view controller that creates the inner UINavigationController must implement UINavigationControllerDelegate, or you&#8217;ll get a compiler warning &#8211; but you can just declare that it does so in the header file, as none of the methods in that interface are required.</p>
<p>Secondly: If you&#8217;re like me, this feels like an abuse of the delegate property.  Now there&#8217;s no reason that you couldn&#8217;t actually use that object productively as the inner navigation controller&#8217;s delegate &#8211; I just haven&#8217;t done so here.  And the fact that the delegate property has to be casted to be used this way says to me that it wasn&#8217;t meant for this &#8211; explicit casts are always a code smell.  (Anal-retentive types might want to put in some type-checking around that cast for safety.)</p>
<p>That said, it works, and I haven&#8217;t run into a maintainability problem yet, as this code doesn&#8217;t really get re-used in many places.  Were I expecting to use this trick often, I might package up a subclass of UINavigationController (call it NestedNavigationController) that actually took an outer UIViewController or UINavigationController property.  Then again I&#8217;m finding that in Cocoa, subclassing is often a code smell&#8230;</p>
<p>Got a better solution?  I&#8217;m interested &#8211; please leave a comment below!</p>
<p><b>Update:</b>  Whoops.  Comments are enabled now.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F07%2F03%2Fuinavigationcontroller-tricks%2F&amp;linkname=UINavigationController%20Tricks"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/07/03/uinavigationcontroller-tricks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google Maps events in Mobile Safari and PhoneGap for iPhone</title>
		<link>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/</link>
		<comments>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 20:23:27 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[gmaps]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=255</guid>
		<description><![CDATA[Having trouble getting your Google Maps div to respond to events like you want in Mobile Safari or a PhoneGap app for iPhone?  So was I.  Disabling pinch-zoom is simple enough, but getting a finger drag to (a) not move the map and (b) draw something on the map was a major pain. [...]]]></description>
			<content:encoded><![CDATA[<p>Having trouble getting your <a href="http://code.google.com/apis/maps/" target="gmaps" title="Google Maps">Google Maps</a> div to respond to events like you want in Mobile Safari or a <a href="http://www.phonegap.com/" target="phonegap" title="PhoneGap">PhoneGap</a> app for iPhone?  So was I.  Disabling pinch-zoom is simple enough, but getting a finger drag to (a) not move the map and (b) draw something on the map was a major pain.  I share my solution below:  Proxying events through a transparent div overlaid on the Google Maps div.</p>
<p>To play along at home, you&#8217;ll need your own <a href="http://code.google.com/apis/maps/signup.html" target="gmaps_key" title="Google Maps API Key signup">Google Maps key</a> or you can just aim your mobile browser to my <a href="http://kickasslabs.com/examples/gmap_events.html" target="gmaps_example" title="Google Maps Event Proxying Example">example code at http://kickasslabs.com/examples/gmap_events.html</a>.  (Note that this will <i>not</i> work in a regular desktop browser.)  Besides a <tt>&lt;script&gt;</tt> tag importing the GMaps JavaScript, you should not need any other libraries or tools besides Mobile Safari to view and use the example page.  If the page is zoomed way out when you start, just double-tap the map tile in the upper-left corner &#8211; it should zoom to fill your viewport.</p>
<p>What you should see is a map tile with a line painted on it.  If you touch the screen, you should see one end of the line follow your finger.</p>
<p>It looks simple but I had a bit of trouble getting it to work, because touch events are not like mouse events (curious as to <a href="http://www.quirksmode.org/blog/archives/2008/08/iphone_events.html" target="mouse_vs_touch" title="Mouse vs. Touch">why?</a>), and GMaps doesn&#8217;t respond to touch events by default.</p>
<p><b>Step by Step</b></p>
<p>First, you need 2 divs:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    &lt;!-- This div holds our map --&gt;
    &lt;div id=&quot;map&quot; style=&quot;width: 320; height: 320px&quot;&gt;&lt;/div&gt;
&nbsp;
    &lt;!-- This div lies on top of the map and acts as an event proxy --&gt;
    &lt;div id=&quot;mapoverlay&quot; style=&quot;height:320px; width: 320px; position: absolute;&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>And you need to position one div over the other:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    mapDiv = document.getElementById('map');
    mapOverlayDiv = document.getElementById('mapoverlay');
    mapOverlayDiv.style.top = (mapDiv.offsetTop + 0) + 'px';
    mapOverlayDiv.style.left = (mapDiv.offsetLeft + 0) + 'px';</pre></div></div>

<p>You set up the map div like you would for any other GMaps-enabled page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    // the map
    _map = new GMap2(document.getElementById(&quot;map&quot;));
&nbsp;
    // coordinates for home base
    _lat = 37;
    _lng = -95;
    mapCenter = new GLatLng(_lat, _lng);
    _map.setCenter(mapCenter, 15);</pre></div></div>

<p>Set up the overlay div to respond to touch events by firing a custom event for your map, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    mapOverlayDiv.ontouchstart = function(e) {
      GEvent.trigger(_map, 'customTouchStart',
        (e.touches[0].pageX - mapDiv.offsetLeft),
        (e.touches[0].pageY - mapDiv.offsetTop));
    }</pre></div></div>

<p>Now have your map respond to that event:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    GEvent.addListener(_map, 'customTouchStart', mapTouchStart);</pre></div></div>

<p>&#8230;where <tt>mapTouchStart</tt> is a callback function that does something useful in response to a touch:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    function mapTouchStart(xPixel, yPixel) {
      redrawLine(xPixel, yPixel);
    }</pre></div></div>

<p>And&#8230; well, then you&#8217;re done.  My example also responds to the touchMove event and has a little code for drawing lines, but you&#8217;ve seen all you need to know to get event proxying up and running for your app.</p>
<p>Got a question?  Got working code for an easier way?  Drop me a line in the comments!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F04%2F29%2Fgoogle-maps-events-in-mobile-safari-and-phonegap-for-iphone%2F&amp;linkname=Google%20Maps%20events%20in%20Mobile%20Safari%20and%20PhoneGap%20for%20iPhone"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FasterCSV and noob-ish silliness</title>
		<link>http://www.kickasslabs.com/2009/03/23/fastercsv-and-noob-ish-silliness/</link>
		<comments>http://www.kickasslabs.com/2009/03/23/fastercsv-and-noob-ish-silliness/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 03:44:52 +0000</pubDate>
		<dc:creator>abel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quick Hits]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[fastercsv]]></category>
		<category><![CDATA[noob]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=238</guid>
		<description><![CDATA[Here are two things I discovered this weekend:
Thing 1: FasterCSV is really cool!  It&#8217;s easy to use and does exactly what it should.  Here&#8217;s Scott Becker&#8217;s exporting tutorial.  For importing, i&#8217;ve hacked together bits from Peter Larkmund&#8217;s travels and the FasterCSV documentation.  Thanks to these tutorials, AMB will give users the ability to upload and [...]]]></description>
			<content:encoded><![CDATA[<p>Here are two things I discovered this weekend:</p>
<p><strong>Thing 1:</strong> <a href="http://fastercsv.rubyforge.org/" target="_blank">FasterCSV</a> is really cool!  It&#8217;s easy to use and does exactly what it should.  Here&#8217;s Scott Becker&#8217;s <a href="http://synthesis.sbecker.net/articles/2007/06/07/how-to-generate-csv-files-in-rails" target="_blank">exporting tutorial</a>.  For importing, i&#8217;ve hacked together bits from Peter Larkmund&#8217;s <a href="http://marklunds.com/articles/one/310" target="_blank">travels</a> and the FasterCSV <a href="http://fastercsv.rubyforge.org/classes/FasterCSV.html" target="_blank">documentation</a>.  Thanks to these tutorials, <a href="http://www.am-budget.com" target="_blank">AMB</a> will give users the ability to upload and export CSVs.</p>
<p><strong>Thing 2:</strong> Default values for the win!  If you have a table that has a column for an amount, feel free to set the default value to 0.00.  Otherwise you won&#8217;t be able to do the following:</p>
<p>a = Account.new<br />
a.amount += 20</p>
<p>&#8230;because a.amount will be nil.  You&#8217;ll throw an error and if you just updated a bunch of code, you might waste an hour trying to figure out why you&#8217;re throwing an exception.  However, if you declare a default value, you&#8217;ll be able to avoid any issues.</p>
<p>If you want me to go into further detail on either thing, just leave a comment.</p>
<p>Thanks for reading and happy hacking.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F03%2F23%2Ffastercsv-and-noob-ish-silliness%2F&amp;linkname=FasterCSV%20and%20noob-ish%20silliness"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/03/23/fastercsv-and-noob-ish-silliness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>F@#$ing get V1 out</title>
		<link>http://www.kickasslabs.com/2009/03/15/fing-get-v1-out/</link>
		<comments>http://www.kickasslabs.com/2009/03/15/fing-get-v1-out/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 18:45:51 +0000</pubDate>
		<dc:creator>abel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ambudget]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=204</guid>
		<description><![CDATA[I’m not new to software development, but unlike Gabe and Brad I’m new to hacking rails.  I&#8217;ve created simple tutorial apps in rails (an online store, a blog, etc.) and I now feel comfortable enough to  create something for the world to see.  At the same time, I realized that I would need to care [...]]]></description>
			<content:encoded><![CDATA[<p>I’m not new to software development, but unlike Gabe and Brad I’m new to hacking rails.  I&#8217;ve created simple tutorial apps in rails (an online store, a blog, etc.) and I now feel comfortable enough to  create something for the world to see.  At the same time, I realized that I would need to care about the project in order to keep coding it.  I decided to turn the spreadsheet that I use for budgeting into a rails app.  <a href="http://www.am-budget.com" target="_blank">AM-Budget</a> (which I like to call AMB) is my first rails app and I’ve learned a lot leading up to its deployment.  I’d like to share the following with you in the hopes that I can encourage other ruby-newbies to release a v1.</p>
<p><span id="more-204"></span><span style="text-decoration: underline;"><strong>Reduce your feature list</strong></span><br />
When I first started AMB, I wanted to do a TON of things:</p>
<ul>
<li> Implement Calendar &amp; tabular layouts for my transactions.</li>
<li> Add AJAX sliders, like Kayak.com, which would allow users to view a range of transactions and dynamically update the sum of those transactions.</li>
<li> Add Dynamic charts of data like Finance.Google.com where users could dynamically add tracked months to the chart to compare trends.</li>
<li> Create a way to track my checking, savings, &amp; credit cards as individual accounts with individual transactions.</li>
<li>Create a way to track credit card payments or moves from checking/savings.</li>
<li>Track an estimated balance &amp; current balance for each month for each account.</li>
<li>Create a way to display and create recurring transactions.</li>
</ul>
<p>Yeah&#8230;while ambition is important, limits are also important.  Getting all of these things done for v1 is possible of an experienced web team, but as a sole developer trying to scratch an itch and learn something at the same time, it’s not realistic.  Implementing all of these features and fixing bugs would take way too much time.  I needed to remember the original mission of replacing my budgeting spreadsheet.  To help reorient myself with my goal, I took another look at my spreadsheet and compared it against my feature list.  I noticed the following:</p>
<ul>
<li> I only have a tabular view of my transactions.</li>
<li>I don’t have any AJAX sliders.</li>
<li> I don’t have any charts.</li>
<li> I don’t track my credit card or savings balances in the spreadsheet.  In fact, 90% of the time I use my debit card so it’s really all coming out of checking anyway.  Once I put money into savings, I don’t touch it again.</li>
<li> I do track my current monthly estimated and running balances for my checking account.</li>
<li> I implement next month’s budget by copying the previous month and editing values/dates where necessary.</li>
</ul>
<p>This really helped to put things in perspective.  V1 really needs to give me the ability to enter and track the transactions of 1 account without any sexy AJAX or Flash.  The 1 feature I did want to implement was recurring transactions, but I&#8217;ve worked around this limitation already and clearly it&#8217;s not critical for v1.<br />
<strong><br />
<span style="text-decoration: underline;">Do it the wrong way, but get something out</span></strong><br />
The beauty of web development is that it’s not like building a car.  Car manufactures deploy once a year.  Websites can be deployed and re-deployed every hour on the hour if we want.  Implementing new features and optimizing code is part of the fun.  As a newbie, you’re going to put code that should be in your model in your view or helper code in your controller.  It’s ok.  Getting yourself into the correct mind set for where code lives takes time.  For v1, if you can get the desired effect, call it a victory and move on.  If this project becomes something that you want to maintain and improve upon, you will.<br />
Also, though test driven development (TDD) is ideal, feel free to get something out without it.  TDD is something that you can come back to if you’ve started without it.  HOWEVER, I’d advise that you stop yourself from advancing to v2 without creating a few REAL tests first.<br />
<strong><br />
<span style="text-decoration: underline;"> No one will judge you harshly</span></strong><br />
Well, that’s not true.  There will be jerks to will give you a ton of crap for not having a “MUST HAVE” feature in your app and how you&#8217;re dumb for not implementing it first.  Honestly, F@#$ those people.  You have to realize that your first rails app isn’t about pleasing the world.  It’s about the realization of your idea and getting it out the door.  That&#8217;s not to say you shouldn&#8217;t listen to a good idea here or there, but don&#8217;t feel bad if everyone isn&#8217;t a fan.</p>
<p>That&#8217;s it.  Now get out there and get v1 f@#$ing done and enjoy your release!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F03%2F15%2Ffing-get-v1-out%2F&amp;linkname=F%40%23%24ing%20get%20V1%20out"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/03/15/fing-get-v1-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing with JRuby, Part 1A</title>
		<link>http://www.kickasslabs.com/2009/02/18/playing-with-jruby-part-1a/</link>
		<comments>http://www.kickasslabs.com/2009/02/18/playing-with-jruby-part-1a/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 15:59:08 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=164</guid>
		<description><![CDATA[First, thanks to Lopex, Charles/Headius, and Thomas for their well-coordinated effort to alleviate some of my JRuby ignorance in my earlier post. And for not going out of their way to make fun of my janky &#8220;benchmarks&#8221; &#8211; like I said, those weren&#8217;t meant to be real benchmarks, just something to give me an idea [...]]]></description>
			<content:encoded><![CDATA[<p>First, thanks to Lopex, Charles/Headius, and Thomas for their well-coordinated effort to alleviate some of my JRuby ignorance in <a href="http://www.kickasslabs.com/2009/02/16/playing-with-jruby-part-1/" title="Playing with JRuby, Part 1">my earlier post</a>. And for not going out of their way to make fun of my janky &#8220;benchmarks&#8221; &#8211; like I said, those weren&#8217;t meant to be real benchmarks, just something to give me an idea of relative performance with some operations I care about.</p>
<p>In that same spirit, I took Lopex&#8217;s advice and turned on the &#8211;server flag; performance gain went from almost 2-to-1 to almost 3-to-1, with no other changes or optimizations.</p>
<p>Before:</p>
<pre>
[~/project/jrubytest/primes]> ruby ./primes.rb
FINDING first 100000 PRIMES
1299709
Time: 57.716853s
[~/project/jrubytest/primes]> jruby ./primes.rb
FINDING first 100000 PRIMES
1299709
Time: 33.885273s
</pre>
<p>After:</p>
<pre>
[~/project/jrubytest/primes]> ruby primes.rb
FINDING first 100000 PRIMES
1299709
Time: 57.204178s
[~/project/jrubytest/primes]> jruby --server primes.rb
FINDING first 100000 PRIMES
1299709
Time: 21.067204s
</pre>
<p>Thanks to the JRuby crew for stopping by here, and for being so helpful generally.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F02%2F18%2Fplaying-with-jruby-part-1a%2F&amp;linkname=Playing%20with%20JRuby%2C%20Part%201A"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/02/18/playing-with-jruby-part-1a/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing with JRuby, Part 1</title>
		<link>http://www.kickasslabs.com/2009/02/16/playing-with-jruby-part-1/</link>
		<comments>http://www.kickasslabs.com/2009/02/16/playing-with-jruby-part-1/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 05:37:12 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=157</guid>
		<description><![CDATA[As part of a new project, I&#8217;m experimenting with JRuby, to see (a) if the alleged performance gains are all that and (b) see how far the Java integration &#8211; especially interface implementation &#8211; can be pushed.
On the performance front, I did a quick-and-dirty test with a CPU-expensive script that finds prime numbers &#8211; not [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a new project, I&#8217;m experimenting with <a title="JRuby" href="http://jruby.codehaus.org/">JRuby</a>, to see (a) if the alleged performance gains are all that and (b) see how far the Java integration &#8211; especially interface implementation &#8211; can be pushed.</p>
<p>On the performance front, I did a quick-and-dirty test with a CPU-expensive script that finds prime numbers &#8211; not a real benchmark, but something to give me an idea of how JRuby held up under a steady load of basic math operations.  Details follow.</p>
<p><span id="more-157"></span></p>
<p><em><strong>Disclaimer:</strong> I know this is not the optimal algorithm for finding prime numbers. The point here is not to find prime numbers, but to tax my CPU predictably.  If you post a comment with the general intent of HEY FINDING PRIMES UR DOIN IT WRONG, I&#8217;ll just delete it.</em></p>
<p>The CPU is an Intel Core Duo in an old Mac Mini.  The Ruby and JRuby versions under consideration were:</p>
<pre>[~/project/jrubytest/primes]&gt; ruby -v ; jruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
jruby 1.1.6 (ruby 1.8.6 patchlevel 114) (2008-12-17 rev 8388) [i386-java]</pre>
<p>The code for the test was:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
limit = <span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">100000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;FINDING first #{limit} PRIMES&quot;</span>
&nbsp;
t0 = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
primes = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>
counter = <span style="color:#006666;">2</span> <span style="color:#008000; font-style:italic;"># starting with 2 and 3</span>
&nbsp;
currnum = primes.<span style="color:#9900CC;">last</span>
<span style="color:#9966CC; font-weight:bold;">while</span> counter <span style="color:#006600; font-weight:bold;">&amp;</span>lt; limit
  currnum <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>
  primes.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span> == <span style="color:#006600; font-weight:bold;">&#40;</span>currnum <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&amp;</span>lt;= <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>currnum.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&amp;</span>gt;= <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>currnum.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      primes <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt; currnum
      counter <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">break</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>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> primes.<span style="color:#9900CC;">last</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Time: #{Time.now - t0}s&quot;</span></pre></div></div>

<p>The results showed just shy of a factor of 2 gain using JRuby (which is consistent with other tests I&#8217;ve run since writing this up):</p>
<pre>[~/project/jrubytest/primes]&gt; ruby ./primes.rb
FINDING first 100000 PRIMES
1299709
Time: 57.716853s
[~/project/jrubytest/primes]&gt; jruby ./primes.rb
FINDING first 100000 PRIMES
1299709
Time: 33.885273s</pre>
<p>These results were gotten after I&#8217;d made sure I had plenty of free RAM so I wasn&#8217;t I/O bound; the difference you&#8217;re seeing is all CPU.  Note also that the timers are <em>inside</em> the Ruby code &#8211; they don&#8217;t account for JVM startup time.</p>
<p>Just for laughs, let&#8217;s see what the difference is with JVM overhead taken into account:</p>
<pre>[~/project/jrubytest/primes]&gt; date ; ruby ./primes.rb ; date ; jruby ./primes.rb ; date
Sun Feb 15 22:26:36 EST 2009
FINDING first 100000 PRIMES
1299709
Time: 54.065758s
Sun Feb 15 22:27:30 EST 2009
FINDING first 100000 PRIMES
1299709
Time: 34.427694s
Sun Feb 15 22:28:05 EST 2009</pre>
<p>So startup overhead is about a second for JRuby.  Not too shabby.</p>
<p>Part 2 of this post is going to cover some of the hairier tasks involved in:</p>
<ul>
<li>Subclassing Java classes and implementing Java interfaces.</li>
<li>Packaging JRuby code in JARs with <a title="Rawr" href="http://rawr.rubyforge.org/">Rawr</a></li>
<li>Calling code in those JARs from Java</li>
</ul>
<p>More to come&#8230;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F02%2F16%2Fplaying-with-jruby-part-1%2F&amp;linkname=Playing%20with%20JRuby%2C%20Part%201"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/02/16/playing-with-jruby-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Hadoop Streaming for Rapid Prototyping of Distributed Algorithms</title>
		<link>http://www.kickasslabs.com/2009/01/04/hadoop-streaming-for-rapid-prototyping-of-distributed-algorithms/</link>
		<comments>http://www.kickasslabs.com/2009/01/04/hadoop-streaming-for-rapid-prototyping-of-distributed-algorithms/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 22:44:06 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[distributed computing]]></category>
		<category><![CDATA[ga]]></category>
		<category><![CDATA[genetic algorithms]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[hadoop streaming]]></category>
		<category><![CDATA[hadoop streaming tutorial]]></category>
		<category><![CDATA[hadoop tutorial]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=132</guid>
		<description><![CDATA[Note: This article assumes that you know a little about MapReduce, or that if you don&#8217;t, you might skim the enclosed links so you know what I&#8217;m talking about when I get to the examples, or check out the Hadoop Tutorial.  It also assumes that you have Hadoop set up &#8211; either clustered or [...]]]></description>
			<content:encoded><![CDATA[<p><i><b>Note:</b> This article assumes that you know a little about MapReduce, or that if you don&#8217;t, you might skim the enclosed links so you know what I&#8217;m talking about when I get to the examples, or check out the <a href="http://hadoop.apache.org/core/docs/current/mapred_tutorial.html" title="Hadoop tutorial" target="hadooptutorial">Hadoop Tutorial</a>.  It also assumes that you have Hadoop set up &#8211; either clustered or pseudo-clustered &#8211; if you&#8217;re going to run the examples.  Or you can just read along.</i></p>
<p><a href="http://hadoop.apache.org/" title="Hadoop" target="hadoop">Hadoop</a> is a framework (written in Java) that supports distributed computing &#8211; specifically Google&#8217;s <a href="http://labs.google.com/papers/mapreduce.html" title="MapReduce" target="mapreduce">MapReduce</a> algorithm.  It also comprises <a href="http://hadoop.apache.org/core/docs/current/hdfs_design.html" title="HDFS" target="hdfs">HDFS</a> (the Hadoop Distrubuted File System), which allows you to redundantly store large quantities of data across multiple disconnected disks as if they were a single storage unit.  I&#8217;ve used Hadoop at two jobs and at home, and it <i>rocks.</i></p>
<p>It comes with a problem, though, which you may spot in the first paragraph:  It&#8217;s written in <i>Java.</i>  Now, don&#8217;t get me wrong &#8211; Java&#8217;s a great language.  But developing software in Java with the most commonly used tools (Eclipse, Ant/Maven, &amp;c) is a monumental pain (and you&#8217;re hearing this from an old Visual C++ hand).  I&#8217;m the first to admit that I should shore up my skills with the Java tools, but even if I were better at it, getting a non-trivial Java project from zero to first runnable build is still about as complex as the invasion of Normandy, and the proliferation of XML config files is just inhumane.</p>
<p>Still, the performance and solidity of Java make it the right choice for a production Hadoop project.  But what if you just want to kick around an idea or test an algorithm?  Wouldn&#8217;t it be nice if you could do that in 2 hours instead of a day and a half?  Wouldn&#8217;t it be nicer still if you could do it in your language of choice?</p>
<p><span id="more-132"></span></p>
<p><a href="http://hadoop.apache.org/core/docs/r0.19.0/streaming.html" title="Hadoop Streaming" target="hadoopstreaming">Hadoop Streaming</a> has made me a very happy man.  Any language that can take data from <code>stdin</code> and give it to <code>stdout</code> can be used to make Hadoop MapReduce jobs.</p>
<p>So of course, I&#8217;m doing mine in Ruby.</p>
<p>As an example, I&#8217;ll transliterate the <strike>trivial</strike> canonical Hadoop word counting example into Ruby.  This example involves taking a large text, and counting the number of instances of each word it contains.  The mapper takes in rows of text, and emits key-value pairs where the key is a word and the value is the number of times that word has occurred in a given row of text.  It would look something like this:</p>
<p><b>word_count_mapper.rb:</b></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
STDIN.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
  word_count = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  line.<span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>word<span style="color:#006600; font-weight:bold;">|</span>
    word_count<span style="color:#006600; font-weight:bold;">&#91;</span>word<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006666;">0</span>
    word_count<span style="color:#006600; font-weight:bold;">&#91;</span>word<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  word_count.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{k}<span style="color:#000099;">\t</span>#{v}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Already, we&#8217;re at our first non-trivial design decision, and it&#8217;s due to a significant design difference between Hadoop Streaming and regular Hadoop jobs:  When you&#8217;re streaming, a single instance of your script will handle many pieces of input &#8211; it&#8217;s a bit like a filter-style Unix command (e.g., <code>grep</code>).  A regular Hadoop mapping job is more firmly seated in the functional paradigm &#8211; each call to your mapper gets one line of data, with no knowledge of others.  Because of this, <a href="http://www.raja-gopal.com/?p=42" title="Raja Gopal on Hadoop Streaming with Ruby" target="gopal">some tutorials</a> will suggest shortcuts such as keeping the hash-based accumulator outside the <code>STDIN</code> loop, and emitting all your rows of <code>[word, count]</code> pairs at the end of processing.  There are advantages to this, but I&#8217;m going to stick with my code above, for two reasons:</p>
<ol>
<li>If you maintain global state in your mapper, you&#8217;ll incur significant rework if you try to port the code to a more purely functional Java Hadoop mapper.</li>
<li>This could get damagingly memory-intensive on large data sets.  Going functional and streaming everything straight to HDFS will add some storage cost, but for large jobs I&#8217;m much more worried about RAM than disk.</li>
</ol>
<p>The purpose of the reduce step is to bring together all the per-line counts for each word, and reduce them to a single, global count per word.  This is facilitated by the fact that Hadoop orders all the records by key before handing it to the reducer (where by default the key is everything before the first tab character &#8211; in this case, the word being counted.)  My version of the reducer looks like this:</p>
<p><b>word_count_reducer.rb:</b></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
current_word = <span style="color:#0000FF; font-weight:bold;">nil</span>
current_count = <span style="color:#006666;">0</span>
STDIN.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
  word, count = line.<span style="color:#9900CC;">strip</span>.<span style="color:#CC0066; font-weight:bold;">split</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> word != current_word
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{current_word}<span style="color:#000099;">\t</span>#{current_count}&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> current_word.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    current_word = word
    current_count = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  current_count <span style="color:#006600; font-weight:bold;">+</span>= count.<span style="color:#9900CC;">to_i</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{current_word}<span style="color:#000099;">\t</span>#{current_count}&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> current_word.<span style="color:#0000FF; font-weight:bold;">nil</span>?</pre></div></div>

<p>The whole business with <code>current_count</code> is necessitated by another Hadoop Streaming-vs.-Hadoop quirk:  In a regular Hadoop reducer, one call to the reducing function gets a collection of all rows associated with a particular key.  When you&#8217;re streaming, you&#8217;re getting only one row at a time, but you&#8217;re guaranteed that the rows will be ordered on keys, and that the rows for a particular key will not be split up across reducer instances.  Again, the temptation might be to place a single accumulator outside the main <code>STDIN</code> loop, but I&#8217;m going to stick with my strategy of lean runtime footprint and going straight to storage.  The price for that is keeping track of which key you&#8217;re on.</p>
<p>I have placed the mapper and reducer in a folder called <code>scripts</code> in my <code>$HADOOP_HOME</code> folder.  To run them against an existing file in HDFS, I do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">bin<span style="color: #000000; font-weight: bold;">/</span>hadoop jar contrib<span style="color: #000000; font-weight: bold;">/</span>streaming<span style="color: #000000; font-weight: bold;">/</span>hadoop-0.19.0-streaming.jar \
  <span style="color: #660033;">-mapper</span> scripts<span style="color: #000000; font-weight: bold;">/</span>word_count_mapper.rb <span style="color: #660033;">-reducer</span> scripts<span style="color: #000000; font-weight: bold;">/</span>word_count_reducer.rb \
  <span style="color: #660033;">-file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>word_count_mapper.rb \
  <span style="color: #660033;">-file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>word_count_reducer.rb \
  <span style="color: #660033;">-input</span> texts<span style="color: #000000; font-weight: bold;">/</span>my_text <span style="color: #660033;">-output</span> word_counts</pre></div></div>

<p>&#8230;and results may be extracted from the <code>word_counts</code> folder in HDFS.</p>
<p>This is all there is to the trivial example.  There&#8217;s more, of course &#8211; I&#8217;m currently using this technology to prototype a much more ambitious project that I will eventually port to Java (more on that later).  While I&#8217;m finding that there is (at least subjectively) some performance overhead associated with streaming that I don&#8217;t see otherwise, it&#8217;s proving incredibly useful and speedy for testing out algorithms and designs.</p>
<p>Are you working with Hadoop Streaming?  Do you have questions?  Drop a line in the comments &#8211; I&#8217;d love to swap learnings.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F01%2F04%2Fhadoop-streaming-for-rapid-prototyping-of-distributed-algorithms%2F&amp;linkname=Hadoop%20Streaming%20for%20Rapid%20Prototyping%20of%20Distributed%20Algorithms"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/01/04/hadoop-streaming-for-rapid-prototyping-of-distributed-algorithms/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
