<?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>mccv</title>
	<atom:link href="http://tardigra.de/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://tardigra.de</link>
	<description>Various Software Engineering Ramblings</description>
	<lastBuildDate>Fri, 07 Aug 2009 23:51:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exception Erasure</title>
		<link>http://tardigra.de/?p=80</link>
		<comments>http://tardigra.de/?p=80#comments</comments>
		<pubDate>Fri, 07 Aug 2009 23:49:39 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=80</guid>
		<description><![CDATA[One of Scala&#8217;s biggest selling points is its seamless integration with regular Java code.  Another, albeit much smaller selling point is the lack of checked exceptions.  However when mixed, these two features can cause some confusion when Scala code is called from Java code.
Let&#8217;s take the following small example.  I have some [...]]]></description>
			<content:encoded><![CDATA[<p>One of Scala&#8217;s biggest selling points is its seamless integration with regular Java code.  Another, albeit much smaller selling point is the lack of checked exceptions.  However when mixed, these two features can cause some confusion when Scala code is called from Java code.</p>
<p>Let&#8217;s take the following small example.  I have some Scala library code that pulls the protocol off a URL represented as a String.  Nothing complicated, it just news up a URL object and returns whatever getProtocol does.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> org.<span style="color: #000000;">mccv</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">net</span>.<span style="color: #000000;">URL</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> CheckedEraser<span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getProtocol<span style="color: #F78811;">&#40;</span>urlStr<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>String <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> url <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> URL<span style="color: #F78811;">&#40;</span>urlStr<span style="color: #F78811;">&#41;</span>
    url.<span style="color: #000000;">getProtocol</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>What does the same thing in Java look like?  Something like this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">orc.mccv.CheckedEraser</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.MalformedURLException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CheckedJava <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getProtocol<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> urlStr<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">MalformedURLException</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">URL</span> u <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>urlStr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> u.<span style="color: #006633;">getProtocol</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that I have to declare that it throws MalformedURLException here.  This isn&#8217;t surprising&#8230; as a Java developer working with URLs I&#8217;m probably all too familiar with MalformedURLException.  Now comes the tricky part.  I want to call my Scala code from Java.  Here we go&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">orc.mccv.CheckedEraser</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.MalformedURLException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CheckedCaller <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> urlStr <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;www.google.com&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> protocol <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Scala</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;trying with Scala...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        CheckedEraser ce <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CheckedEraser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        protocol <span style="color: #339933;">=</span> ce.<span style="color: #006633;">getProtocol</span><span style="color: #009900;">&#40;</span>urlStr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, I didn&#8217;t actually pass in a valid URL&#8230; so what happens?  I get a MalformedURLException thrown.  Ok, so I should catch that.  But does this work?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            protocol <span style="color: #339933;">=</span> ce.<span style="color: #006633;">getProtocol</span><span style="color: #009900;">&#40;</span>urlStr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;caught an exception of type &quot;</span> 
               <span style="color: #339933;">+</span> e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Nope, won&#8217;t compile.  My Scala method doesn&#8217;t throw a MalformedURLException, because I was lazy and didn&#8217;t add my <a href="http://www.scala-lang.org/node/106">throws annotation</a>.  I&#8217;m forced to just catch Exception, and deal with refinement of that exception in my catch block, which is pretty ugly.</p>
<p>So a word of caution when handing Scala libraries over to Java developers&#8230; if you aren&#8217;t rigorous on your throws annotations, you are handing them a different exception handling contract.  This may be ok, but it almost certainly needs to be explicitly stated.  Note that this same exception erasure issue applies to Groovy, and likely other JVM languages that lack typed exceptions.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=80</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things that are easier in Scala vol. 2</title>
		<link>http://tardigra.de/?p=74</link>
		<comments>http://tardigra.de/?p=74#comments</comments>
		<pubDate>Tue, 04 Aug 2009 17:22:06 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[cassandra]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=74</guid>
		<description><![CDATA[I&#8217;ve been working with Cassandra recently, and have been using its Thrift interface quite a bit.  Cassandra stores values as byte arrays, which offers a lot of room for flexibility, but also makes it a bit clumsy to get values in.  The following Java code shows a minimal Cassadra example, opening a connection [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with Cassandra recently, and have been using its <a href="http://wiki.apache.org/cassandra/ThriftInterface">Thrift interface</a> quite a bit.  Cassandra stores values as byte arrays, which offers a lot of room for flexibility, but also makes it a bit clumsy to get values in.  The following Java code shows a minimal Cassadra example, opening a connection and mutating a few rows.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.thrift.transport.TSocket</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.thrift.transport.TTransportException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.thrift.protocol.TBinaryProtocol</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.thrift.TException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.cassandra.service.Cassandra</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.cassandra.service.InvalidRequestException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.cassandra.service.UnavailableException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.UnsupportedEncodingException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CassandraJava <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insertCassandra<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cassHost, <span style="color: #000066; font-weight: bold;">int</span> cassPort<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// boilerplate to get Cassandra set up</span>
            TSocket sock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TSocket<span style="color: #009900;">&#40;</span>cassHost, cassPort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            TBinaryProtocol tr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TBinaryProtocol<span style="color: #009900;">&#40;</span>sock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Cassandra.<span style="color: #006633;">Client</span> c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cassandra.<span style="color: #006633;">Client</span><span style="color: #009900;">&#40;</span>tr,tr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            sock.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// run the inserts</span>
            c.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyTable&quot;</span>,<span style="color: #0000ff;">&quot;key&quot;</span>,<span style="color: #0000ff;">&quot;colFamily:colName1&quot;</span>,
                    <span style="color: #0000ff;">&quot;foo&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span>,
                    <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            c.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyTable&quot;</span>,<span style="color: #0000ff;">&quot;key&quot;</span>,<span style="color: #0000ff;">&quot;colFamily:colName2&quot;</span>,
                    <span style="color: #0000ff;">&quot;bar&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span>,
                    <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            c.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyTable&quot;</span>,<span style="color: #0000ff;">&quot;key&quot;</span>,<span style="color: #0000ff;">&quot;colFamily:colName3&quot;</span>,
                    <span style="color: #0000ff;">&quot;baz&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span>,
                    <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>TTransportException e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>TException te<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            te.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>InvalidRequestException ire<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            ire.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>UnavailableException ue<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            ue.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">UnsupportedEncodingException</span> uee<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            uee.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A few things to note.  First, calling getBytes(&#8220;UTF-8&#8243;) on strings is sorta ugly.  Note that this gets even worse when you <a href="http://wiki.apache.org/cassandra/ClientExamples">switch to Cassandra trunk/0.4</a>, as the column names also become byte arrays.  Second, I have to pass in my table, key, timestamp and blocking arguments on each call even though they don&#8217;t change.  </p>
<p>Let&#8217;s see if we can solve the first problem with Scala.  The following sample is a first attempt.  Note the implicit function defined at the beginning of the object.  This tells the Scala compiler that anywhere (well, anywhere that implicit function is in scope) that you need an Array[Byte] and have String, you can execute the function to do the conversion.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> orc.<span style="color: #000000;">mccv</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">transport</span>.<span style="color: #F78811;">&#123;</span>TSocket,TTransportException<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">protocol</span>.<span style="color: #000000;">TBinaryProtocol</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">TException</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">cassandra</span>.<span style="color: #000000;">service</span>.<span style="color: #F78811;">&#123;</span>Cassandra,
  InvalidRequestException,UnavailableException<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> CassandraScala<span style="color: #F78811;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// create a transparent conversion of strings to byte arrays</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> string2ByteArray<span style="color: #F78811;">&#40;</span>s<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>Byte<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span>
    s.<span style="color: #000000;">getBytes</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;UTF-8&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> cassInsert<span style="color: #F78811;">&#40;</span>cassHost<span style="color: #000080;">:</span>String,cassPort<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// boilerplate to get cassandra set up</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> sock <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> TSocket<span style="color: #F78811;">&#40;</span>cassHost, cassPort<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> tr <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> TBinaryProtocol<span style="color: #F78811;">&#40;</span>sock<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> c <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Cassandra.<span style="color: #000000;">Client</span><span style="color: #F78811;">&#40;</span>tr,tr<span style="color: #F78811;">&#41;</span>
    sock.<span style="color: #000000;">open</span>
    <span style="color: #008000; font-style: italic;">// run the inserts</span>
    c.<span style="color: #000000;">insert</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;MyTable&quot;</span>,<span style="color: #6666FF;">&quot;key&quot;</span>,<span style="color: #6666FF;">&quot;colFamily:colName1&quot;</span>,
            <span style="color: #6666FF;">&quot;foo&quot;</span>,System.<span style="color: #000000;">currentTimeMillis</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,<span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#41;</span>
    c.<span style="color: #000000;">insert</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;MyTable&quot;</span>,<span style="color: #6666FF;">&quot;key&quot;</span>,<span style="color: #6666FF;">&quot;colFamily:colName2&quot;</span>,
            <span style="color: #6666FF;">&quot;bar&quot;</span>,System.<span style="color: #000000;">currentTimeMillis</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,<span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#41;</span>
    c.<span style="color: #000000;">insert</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;MyTable&quot;</span>,<span style="color: #6666FF;">&quot;key&quot;</span>,<span style="color: #6666FF;">&quot;colFamily:colName3&quot;</span>,
            <span style="color: #6666FF;">&quot;baz&quot;</span>,System.<span style="color: #000000;">currentTimeMillis</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,<span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The resulting code is quite a bit shorter.  Note that I&#8217;m cheating a bit by not catching all the exceptions&#8230; but it&#8217;s still a fair bit more concise.</p>
<p>But what about all the duplicate arguments?  Here we can use <a href="http://en.wikipedia.org/wiki/Currying">currying</a> to create a new function with our default arguments applied, leaving us to just supply the changing arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> orc.<span style="color: #000000;">mccv</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">transport</span>.<span style="color: #F78811;">&#123;</span>TSocket,TTransportException<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">protocol</span>.<span style="color: #000000;">TBinaryProtocol</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">thrift</span>.<span style="color: #000000;">TException</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">cassandra</span>.<span style="color: #000000;">service</span>.<span style="color: #F78811;">&#123;</span>Cassandra,
  InvalidRequestException,UnavailableException<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> CassandraScala<span style="color: #F78811;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// create a transparent conversion of strings to byte arrays</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> string2ByteArray<span style="color: #F78811;">&#40;</span>s<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>Byte<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span>
    s.<span style="color: #000000;">getBytes</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;UTF-8&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> cassInsertCurried<span style="color: #F78811;">&#40;</span>cassHost<span style="color: #000080;">:</span>String,cassPort<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// boilerplate to get cassandra set up</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> sock <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> TSocket<span style="color: #F78811;">&#40;</span>cassHost, cassPort<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> tr <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> TBinaryProtocol<span style="color: #F78811;">&#40;</span>sock<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> c <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Cassandra.<span style="color: #000000;">Client</span><span style="color: #F78811;">&#40;</span>tr,tr<span style="color: #F78811;">&#41;</span>
    sock.<span style="color: #000000;">open</span>
    <span style="color: #008000; font-style: italic;">// create a partially applied function with our</span>
    <span style="color: #008000; font-style: italic;">// default arguments in place</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> insert <span style="color: #000080;">=</span> c.<span style="color: #000000;">insert</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;MyTable&quot;</span>,<span style="color: #6666FF;">&quot;key&quot;</span>,<span style="color: #000080;">_:</span>String,<span style="color: #000080;">_:</span>String,
      System.<span style="color: #000000;">currentTimeMillis</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,<span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#41;</span>
    insert<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;colFamily:colName1&quot;</span>,<span style="color: #6666FF;">&quot;foo&quot;</span><span style="color: #F78811;">&#41;</span>
    insert<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;colFamily:colName2&quot;</span>,<span style="color: #6666FF;">&quot;bar&quot;</span><span style="color: #F78811;">&#41;</span>
    insert<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;colFamily:colName3&quot;</span>,<span style="color: #6666FF;">&quot;baz&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Here we create a new Function object call ins.  The syntax _:String in our call means that those arguments are not applied, and will need to be supplied in calls to ins.  Since Function defines an apply method, you can call it just like a normal method.  The currying call may throw some people, but the resulting insert calls are almost certainly more readable than the version that has six args.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=74</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What is timestamp used for in all those Cassandra calls?</title>
		<link>http://tardigra.de/?p=71</link>
		<comments>http://tardigra.de/?p=71#comments</comments>
		<pubDate>Mon, 03 Aug 2009 17:50:13 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[cassandra]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=71</guid>
		<description><![CDATA[While working on scassandra, I ran across a mailing list thread asking about Cassandra&#8217;s MVCC support.  I figured it had it, as most of the column data structures and most of the thrift API calls have it.  Wrong.  Timestamp is just used for eventual consistency support, as a way to track which [...]]]></description>
			<content:encoded><![CDATA[<p>While working on <a href="http://github.com/mccv/scassandra/tree/master">scassandra</a>, I ran across a mailing list thread asking about Cassandra&#8217;s <a href="http://en.wikipedia.org/wiki/Multiversion_concurrency_control">MVCC</a> support.  I figured it had it, as most of the column data structures and most of the thrift API calls have it.  Wrong.  Timestamp is just used for <a href="http://www.allthingsdistributed.com/2008/12/eventually_consistent.html">eventual consistency</a> support, as a way to track which mutations are older than others.  The timestamp on a mutation is always provided by the client.  In general this should be one of</p>
<ul>
<li>The time at which the data to be mutated was generated</li>
<li>The current system time if the former is not available</li>
</ul>
<p>Note that a lot of the Cassandra examples use a timestamp of zero, but this is just to make the examples independent of system clocks.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=71</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cassandra get_columns_since gotcha</title>
		<link>http://tardigra.de/?p=69</link>
		<comments>http://tardigra.de/?p=69#comments</comments>
		<pubDate>Sun, 26 Jul 2009 04:28:49 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[cassandra]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=69</guid>
		<description><![CDATA[This makes sense once you think about it, but it doesn&#8217;t appear to show up anywhere in the doc.  If you call get_columns_since on a column family that is not ordered by Time, the call succeeds but always returns zero rows.  To get this working you&#8217;ll need a column defined like this

  [...]]]></description>
			<content:encoded><![CDATA[<p>This makes sense once you think about it, but it doesn&#8217;t appear to show up anywhere in the doc.  If you call get_columns_since on a column family that is not ordered by Time, the call succeeds but always returns zero rows.  To get this working you&#8217;ll need a column defined like this</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnFamily</span> <span style="color: #000066;">ColumnSort</span>=<span style="color: #ff0000;">&quot;Time&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Statuses&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=69</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Scala specs tests in Maven with JUnit 4</title>
		<link>http://tardigra.de/?p=66</link>
		<comments>http://tardigra.de/?p=66#comments</comments>
		<pubDate>Fri, 24 Jul 2009 19:09:41 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[maven]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=66</guid>
		<description><![CDATA[My first foray into Scala unit testing wasn&#8217;t quite as smooth as I&#8217;d wanted, but I have it working now.  The specs doc is pretty good.   However what it doesn&#8217;t tell you is that JUnit 4.0 doesn&#8217;t work.  Or 4.1.  If you try these you&#8217;ll run into a &#8220;No runnable [...]]]></description>
			<content:encoded><![CDATA[<p>My first foray into Scala unit testing wasn&#8217;t quite as smooth as I&#8217;d wanted, but I have it working now.  The <a href="http://code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_with_JUnit4">specs doc</a> is pretty good.   However what it doesn&#8217;t tell you is that JUnit 4.0 doesn&#8217;t work.  Or 4.1.  If you try these you&#8217;ll run into a &#8220;No runnable methods&#8221; found, with the following trace</p>
<pre>
java.lang.Exception: No runnable methods
        at org.junit.internal.runners.TestClassMethodsRunner.testAborted(TestClassMethodsRunner.java:42)
        at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:33)
        at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
        at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
        at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
</pre>
<p>Upgrading to JUnit 4.4 did the trick.  Pom snippet is</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.scala-tools.testing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>specs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Test snippet is</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> org.<span style="color: #000000;">mccv</span>.<span style="color: #000000;">cassandra</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">specs</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">specs</span>.<span style="color: #000000;">runner</span>.<span style="color: #000080;">_</span>
<span style="color: #00ff00; font-style: italic;">/**
 * Created by IntelliJ IDEA.
 * User: mmcbride
 * Date: Jul 24, 2009
 * Time: 10:43:31 AM
 * To change this template use File | Settings | File Templates.
 */</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> cassandraTest <span style="color: #0000ff; font-weight: bold;">extends</span> JUnit4<span style="color: #F78811;">&#40;</span>BasicCassandra<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> BasicCassandra <span style="color: #0000ff; font-weight: bold;">extends</span> Specification <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> cass <span style="color: #000080;">=</span> Cassandra<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;localhost&quot;</span>,<span style="color: #F78811;">9160</span><span style="color: #F78811;">&#41;</span>
  doBefore <span style="color: #F78811;">&#123;</span>
    cass.<span style="color: #000000;">connect</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
  doAfter <span style="color: #F78811;">&#123;</span>
    cass.<span style="color: #000000;">close</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #6666FF;">&quot;Cassandra(<span style="color: #0000ff; font-weight: bold;">\&quot;</span>localhost<span style="color: #0000ff; font-weight: bold;">\&quot;</span>,9160)&quot;</span> should <span style="color: #F78811;">&#123;</span>
    setSequential<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #6666FF;">&quot;create a Cassandra instance&quot;</span> in <span style="color: #F78811;">&#123;</span>
      cass must<span style="color: #000080;">_!=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
    <span style="color: #F78811;">&#125;</span>
    <span style="color: #6666FF;">&quot;provide for creation of a Db&quot;</span> in <span style="color: #F78811;">&#123;</span>
      cass.<span style="color: #000000;">db</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;SocialGraph&quot;</span><span style="color: #F78811;">&#41;</span> must<span style="color: #000080;">_!=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
    <span style="color: #F78811;">&#125;</span>
    <span style="color: #6666FF;">&quot;allow insertion of values&quot;</span> in <span style="color: #F78811;">&#123;</span>
      cass.<span style="color: #000000;">insert</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;SocialGraph&quot;</span>,<span style="color: #6666FF;">&quot;mccv&quot;</span>,<span style="color: #6666FF;">&quot;Details:name&quot;</span>,<span style="color: #6666FF;">&quot;Mark McBride&quot;</span>.<span style="color: #000000;">getBytes</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;UTF-8&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #6666FF;">&quot;cass.Db(<span style="color: #0000ff; font-weight: bold;">\&quot;</span>SocialGraph<span style="color: #0000ff; font-weight: bold;">\&quot;</span>)&quot;</span> should <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=66</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Highlighting vs. Fixing Mistakes</title>
		<link>http://tardigra.de/?p=63</link>
		<comments>http://tardigra.de/?p=63#comments</comments>
		<pubDate>Sat, 18 Jul 2009 16:22:15 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[organization]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=63</guid>
		<description><![CDATA[This is an email I need to send myself every three months or so&#8230; every time I hit one of my jaded cycles and turn into a sarcastic cynic.
It&#8217;s not so hard to find fault in projects, people, processes, etc.  It&#8217;s not hard to point these things out publicly, and to make people feel [...]]]></description>
			<content:encoded><![CDATA[<p>This is an email I need to send myself every three months or so&#8230; every time I hit one of my jaded cycles and turn into a sarcastic cynic.</p>
<p>It&#8217;s not so hard to find fault in projects, people, processes, etc.  It&#8217;s not hard to point these things out publicly, and to make people feel small when they challenge your analysis.  What&#8217;s more challenging, and what separates the good leaders from the merely average is working <i>with</i> people to address the faults in a productive manner.</p>
<p>The uncivilized leader (me in my cynical phases) sees faults and looks for the least tedious solution.  It&#8217;s often tempting to discount people as incompetent and processes as useless.  The quickest solution seems to be to marginalize people and ignore processes.  However in the long run (at least in a large corporation) this leads to a team made up of mostly marginalized people, and chaotic processes that start crushing projects.</p>
<p>Instead of marginalizing a problem person, ask how they could make a productive contribution to the group.  Instead of bitching about a process, propose a new, better one.  Instead of tearing apart a technical design somebody has a deep emotional investment in, coach them on how to fix the current design and build a better one next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=63</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TalkingPuffin Twitter API Enhancement Samples</title>
		<link>http://tardigra.de/?p=58</link>
		<comments>http://tardigra.de/?p=58#comments</comments>
		<pubDate>Thu, 11 Jun 2009 04:17:30 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=58</guid>
		<description><![CDATA[Dave recently pushed some of my API changes to the main TalkingPuffin.  There are quite a few updates.  The API is more complete now, more resilient, supports optional REST arguments, and has a method to load all pages of various APIs.  I thought I&#8217;d show a few of the enhancements here.
This first [...]]]></description>
			<content:encoded><![CDATA[<p>Dave recently pushed some of my API changes to the main <a href="http://github.com/dcbriccetti/talking-puffin/tree/master">TalkingPuffin</a>.  There are quite a few updates.  The API is more complete now, more resilient, supports optional REST arguments, and has a method to load all pages of various APIs.  I thought I&#8217;d show a few of the enhancements here.</p>
<p>This first listing shows how to use the new TwitterArgs class.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> org.<span style="color: #000000;">talkingpuffin</span>.<span style="color: #000000;">twitter</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> ShowAPI <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// set up our credentials and session</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> user <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;foo&quot;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> password <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;bar&quot;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> sess <span style="color: #000080;">=</span> TwitterSession<span style="color: #F78811;">&#40;</span>user,password<span style="color: #F78811;">&#41;</span>
    <span style="color: #008000; font-style: italic;">// same method as before</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> friendsTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">getFriendsTimeline</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + friendsTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets from old style method&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #008000; font-style: italic;">// use per page count</span>
    friendsTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">getFriendsTimeline</span><span style="color: #F78811;">&#40;</span>TwitterArgs.<span style="color: #000000;">maxResults</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">200</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + friendsTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets using per page count&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #008000; font-style: italic;">// use chained twitter args</span>
    friendsTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">getFriendsTimeline</span><span style="color: #F78811;">&#40;</span>TwitterArgs.<span style="color: #000000;">maxResults</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">200</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">page</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + friendsTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets using chained twitter args&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>There are a variety of methods that support passing in a TwitterArgs instance.  These can be constructed by calling the various methods on the TwitterArgs object, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> args <span style="color: #000080;">=</span> TwitterArgs.<span style="color: #000000;">maxResults</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">200</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>If you want to pass in multiple optional arguments you can make calls on an existing args instance, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> args <span style="color: #000080;">=</span> oldArgs.<span style="color: #000000;">page</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>These get converted to a URL query segment, and are appended to the URLs called for twitter data.</p>
<p>The next listing shows some Scala functional programming neatness, and builds on my <a href="http://www.themcwongs.com/?p=50">previous post</a> about retry logic.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> org.<span style="color: #000000;">talkingpuffin</span>.<span style="color: #000000;">twitter</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> ShowAPI <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// set up our credentials and session</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> user <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> password <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> sess <span style="color: #000080;">=</span> TwitterSession<span style="color: #F78811;">&#40;</span>user,password<span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// demo load all... this just loads the first page</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> myTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">getUserTimeline</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mccv&quot;</span><span style="color: #F78811;">&#41;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + myTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets from my timeline&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #008000; font-style: italic;">// now we show load all.  loadAll just wants a function that takes an int as an arg,</span>
    <span style="color: #008000; font-style: italic;">// which is the page.  Scala's partially applied functions make this pretty easy</span>
    <span style="color: #008000; font-style: italic;">// to use in a general purpose way</span>
    myTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">loadAll</span><span style="color: #F78811;">&#40;</span>sess.<span style="color: #000000;">getUserTimeline</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mccv&quot;</span>,<span style="color: #000080;">_:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + myTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets using loadAll&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #008000; font-style: italic;">// this is even fancier.  Here I add retry logic to load all.</span>
    <span style="color: #008000; font-style: italic;">// note that retryPage is a function I defined here... but the session</span>
    <span style="color: #008000; font-style: italic;">// doesn't care.  It keeps iterating through pages, retrying and loading</span>
    <span style="color: #008000; font-style: italic;">// until it reaches the end.</span>
    myTweets <span style="color: #000080;">=</span> sess.<span style="color: #000000;">loadAll</span><span style="color: #F78811;">&#40;</span>retryPage<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_:</span>Int,sess.<span style="color: #000000;">getUserTimeline</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mccv&quot;</span>,<span style="color: #000080;">_:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;got &quot;</span> + myTweets.<span style="color: #000000;">size</span> + <span style="color: #6666FF;">&quot; tweets using loadAll and retries&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #00ff00; font-style: italic;">/**
    * this is a function that is sort of a thunk through to tryNTimes.
    */</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> retryPage<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>page<span style="color: #000080;">:</span>Int, func<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>T <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// here we define a privately scoped function</span>
        <span style="color: #008000; font-style: italic;">// that can be passed to tryNTimes</span>
        <span style="color: #0000ff; font-weight: bold;">def</span> tryPage<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
            func<span style="color: #F78811;">&#40;</span>page<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
        <span style="color: #008000; font-style: italic;">// and now we try N (5) times</span>
        tryNTimes<span style="color: #F78811;">&#40;</span>tryPage,<span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #00ff00; font-style: italic;">/**
    * from the last blog post, a retrier
    */</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> tryNTimes<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>func<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> T, runNumber<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>T <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">try</span><span style="color: #F78811;">&#123;</span>
          func<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #0000ff; font-weight: bold;">if</span> runNumber <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> tryNTimes<span style="color: #F78811;">&#40;</span>func,runNumber - <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">throw</span> e
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Hopefully this code is more or less self documenting.  The first session call just gets the first page of the user timeline.  This is usually sufficient for writing a Twitter client, but if you are doing data mining it isn&#8217;t so great.  The new API introduces a method called loadAll of type (f:(Int) => List[T]) => List[T].  This means that any method that takes a single int argument (a page number) and returns a list can be passed to loadAll.  It keeps executing the passed in function with increasing page numbers until an empty list is returned (note that this <i>must</i> be the behavior on page overruns as currently implemented.  If the overrun URI returns a 404 we&#8217;ll get an exception thrown.  Luckily Twitter currently just returns an empty list).</p>
<p>The second call shows this in action.  It&#8217;s using a slightly more complicated case, because getUserTimeline takes a String and an Int.  Scala&#8217;s partially applied functions make this a snap.  The line</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">sess.<span style="color: #000000;">getUserTimeline</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;mccv&quot;</span>,<span style="color: #000080;">_:</span>Int<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Takes the getUserTimeline call with one bound argument and one unbound.  It returns a function of type (Int) => List[TwitterStatus], which is exactly what loadAll wants. </p>
<p>The third call is even more <del datetime="2009-06-11T03:59:35+00:00">complicated</del> um, sophisticated.  Let&#8217;s say we want to retry operations five times, just in case we get dropped connections in the middle of a big load.  Well, all we need to do is get a function that takes an int and returns a list into loadAll.</p>
<p>In a previous blog post I wrote about implementing a retryable method.  You can see this more or less unchanged at the end of the file.  Unfortunately its signature isn&#8217;t quite what we want.  So we define retryPage, which acts as an adapter from loadAll to tryNTimes.  With this setup in place, we can set up our last call, which uses two partially applied functions.  The first converts getUserTimeline into the page-argument-only form, and the second converts retryPage into a page-argument-only form.</p>
<p>Running the two samples combined this gives us the following output</p>
<pre>
got 20 tweets from old style method
got 199 tweets using per page count
got 200 tweets using chained twitter args
got 20 tweets from my timeline
got 824 tweets using loadAll
trying to get page 1
trying to get page 2
trying to get page 3
trying to get page 4
trying to get page 5
trying to get page 6
trying to get page 7
trying to get page 8
trying to get page 9
trying to get page 10
trying to get page 11
trying to get page 12
trying to get page 13
trying to get page 14
trying to get page 15
trying to get page 16
trying to get page 17
trying to get page 18
trying to get page 19
trying to get page 20
trying to get page 21
trying to get page 22
trying to get page 23
trying to get page 24
trying to get page 25
trying to get page 26
trying to get page 27
trying to get page 28
trying to get page 29
trying to get page 30
trying to get page 31
trying to get page 32
trying to get page 33
trying to get page 34
trying to get page 35
trying to get page 36
trying to get page 37
trying to get page 38
trying to get page 39
trying to get page 40
trying to get page 41
trying to get page 42
trying to get page 43
got 824 tweets using loadAll and retries
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=58</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A slightly cleaner Java Retryable</title>
		<link>http://tardigra.de/?p=53</link>
		<comments>http://tardigra.de/?p=53#comments</comments>
		<pubDate>Thu, 28 May 2009 00:30:36 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=53</guid>
		<description><![CDATA[In my last post I walked through an implementation of a class that retries an operation N times before failing.  However after reading this post I realized that using Java&#8217;s Callable class cleans things up quite a bit.  This completely removes the need for a Retryable interface, and leaves you with Retrier implemented [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.themcwongs.com/?p=50">last post</a> I walked through an implementation of a class that retries an operation N times before failing.  However after reading <a href="http://biotext.org.uk/retriabletask-a-generic-wrapper-for-retrying-operations-in-java/">this post</a> I realized that using Java&#8217;s Callable class cleans things up quite a bit.  This completely removes the need for a Retryable interface, and leaves you with Retrier implemented as</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.concurrent.Callable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * The implementation of our retrying class
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Retryer<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * The retryable we will call
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> retryable<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Just assign our retryable field 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Retryer<span style="color: #009900;">&#40;</span>Callable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> retryable<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">retryable</span> <span style="color: #339933;">=</span> retryable<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Try to execute our retryable n times 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> T tryTimes<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// store the last thrown recoverable exception</span>
		RecoverableException lastException <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// try the specified number of times</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> times<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;running it with &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>times<span style="color: #339933;">-</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; tries remaining&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> retryable.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>RecoverableException re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				lastException <span style="color: #339933;">=</span> re<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> lastException<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And an implementation/usage demonstration as</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.concurrent.Callable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * An implementation of the Retryable interface
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RetryableImpl <span style="color: #000000; font-weight: bold;">implements</span> Callable<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Show usage of the retryer
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// create a new retrier</span>
		Retryer<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> retrier <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Retryer<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RetryableImpl<span style="color: #009900;">&#40;</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;">// run the retrier </span>
		<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result = &quot;</span> <span style="color: #339933;">+</span> retrier.<span style="color: #006633;">tryTimes</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failed after 3 tries&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * An intentionally flaky operation
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0.3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> RecoverableException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
	        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Scala version is still cleaner, but this Java version is a substantial improvement over the first version.  As Andrew said in the biotext post, getting to know (or in my case remembering) the more advanced Java concurrency classes is a good thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=53</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things that are easier in Scala vol 1</title>
		<link>http://tardigra.de/?p=50</link>
		<comments>http://tardigra.de/?p=50#comments</comments>
		<pubDate>Sat, 23 May 2009 04:04:00 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=50</guid>
		<description><![CDATA[Last week I ran into a cose where needed to add some logic to retry certain operations a number of times before failing.  These operations can throw a variety of exceptions, some of which are recoverable, some of which are not.  Due to a variety of issues the code in question must be [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I ran into a cose where needed to add some logic to retry certain operations a number of times before failing.  These operations can throw a variety of exceptions, some of which are recoverable, some of which are not.  Due to a variety of issues the code in question <b>must</b> be Java.  At least for the time being.  So I set out to do this.  The solution I came up with wasn&#8217;t bad as Java solutions go&#8230;</p>
<p>Step 1: identify recoverable exceptions.  Because I control the code that can throw the exceptions this isn&#8217;t so bad.  I create a simple RecoverableException class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A marker class for recoverable exceptions
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RecoverableException <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 2: create an interface that defines a retryable operation.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * An interface that defines an execute operation to retry
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Retryable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> T execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 3: implement a class that will retry a retryable n times before failing.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * The implementation of our retrying class
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Retryer<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * The retryable we will call
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> Retryable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> retryable<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Just assign our retryable field 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Retryer<span style="color: #009900;">&#40;</span>Retryable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> retryable<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">retryable</span> <span style="color: #339933;">=</span> retryable<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Try to execute our retryable n times 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> T tryTimes<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// store the last thrown recoverable exception</span>
		RecoverableException lastException <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// try the specified number of times</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> times<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;running it with &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>times<span style="color: #339933;">-</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; tries remaining&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> retryable.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>RecoverableException re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				lastException <span style="color: #339933;">=</span> re<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> lastException<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 4: implement the retryable interface with the operation we want retried</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.mccv</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * An implementation of the Retryable interface
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RetryableImpl <span style="color: #000000; font-weight: bold;">implements</span> Retryable<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Show usage of the retryer
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// create a new retrier</span>
		Retryer<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> retrier <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Retryer<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RetryableImpl<span style="color: #009900;">&#40;</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;">// run the retrier </span>
		<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result = &quot;</span> <span style="color: #339933;">+</span> retrier.<span style="color: #006633;">tryTimes</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failed after 3 tries&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * An intentionally flaky operation
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0.3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> RecoverableException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
	        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This works, and you can use anonymous inner classes to wrap stuff that doesn&#8217;t directly extend retryable.  But it&#8217;s ugly.  You have to make a lot of things final you wouldn&#8217;t otherwise (for instance let&#8217;s say you want your retryable to set a status code and response message.  This gets difficult due to having to mark things final).  It&#8217;s also a bit clumsy in that you have a Retryer class in play, plus an implementation of Retryable.  What I really wanted was a way to say</p>
<p>retry(myOperation,nTimes).</p>
<p>With Scala I got a lot closer.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> org.<span style="color: #000000;">mccv</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * A marker trait indicating an exception that is recoverable.
 * Note that we could make this a trait as well.
 */</span>
<span style="color: #0000ff; font-weight: bold;">class</span> RecoverableException <span style="color: #0000ff; font-weight: bold;">extends</span> Exception
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * Usage of our retryable class
 */</span>
<span style="color: #0000ff; font-weight: bold;">object</span> Retryer <span style="color: #F78811;">&#123;</span>
	<span style="color: #00ff00; font-style: italic;">/**
	* The workhorse.  Runs the operation, and if successful returns the result.
	* If unsuccessful calls itself recursively with a decremented run number.
	*/</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> tryNTimes<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>func<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> T, runNumber<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>T <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
	  println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;running it with &quot;</span> + runNumber + <span style="color: #6666FF;">&quot; tries remaining&quot;</span><span style="color: #F78811;">&#41;</span>
	  <span style="color: #0000ff; font-weight: bold;">try</span><span style="color: #F78811;">&#123;</span>
		  func<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
	  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
	    <span style="color: #0000ff; font-weight: bold;">case</span> e<span style="color: #000080;">:</span>RecoverableException <span style="color: #0000ff; font-weight: bold;">if</span> runNumber <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> tryNTimes<span style="color: #F78811;">&#40;</span>func,runNumber - <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
	    <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">throw</span> e
	  <span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Main <span style="color: #F78811;">&#123;</span>
   <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Unit <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> tries <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span><span style="color: #000080;">;</span>
    <span style="color: #0000ff; font-weight: bold;">try</span><span style="color: #F78811;">&#123;</span>
    	println<span style="color: #F78811;">&#40;</span>Retryer.<span style="color: #000000;">tryNTimes</span><span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>flakyMethod,tries<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
     <span style="color: #F78811;">&#125;</span><span style="color: #0000ff; font-weight: bold;">catch</span><span style="color: #F78811;">&#123;</span>
       <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;failed after &quot;</span> + tries + <span style="color: #6666FF;">&quot; tries&quot;</span><span style="color: #F78811;">&#41;</span>
     <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> flakyMethod<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>String <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>Math.<span style="color: #000000;">random</span> <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0.3</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">throw</span> <span style="color: #0000ff; font-weight: bold;">new</span> RecoverableException<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span><span style="color: #0000ff; font-weight: bold;">else</span><span style="color: #F78811;">&#123;</span>
      <span style="color: #6666FF;">&quot;foo&quot;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This is almost exactly what I&#8217;d like.  I define a simple object that has a tryNTimes method.  Because it&#8217;s an object it&#8217;s sort of like a Java static method&#8230; I can call Retryer.tryNTimes() directly.  This is a recursive call.  If a RecoverableException is caught, it just calls tryNTimes with the number of tries decremented.  The beauty of this is that you can pass in <i>any</i> zero argument function.  It doesn&#8217;t matter what class it&#8217;s defined in&#8230; it just has to know about recoverable exceptions.</p>
<p>I&#8217;ll be keeping an eye out for things that are easier in scala and keeping this series up to date.  I&#8217;m sure I&#8217;ll be running into more of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=50</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>You can&#8217;t clone superstars</title>
		<link>http://tardigra.de/?p=48</link>
		<comments>http://tardigra.de/?p=48#comments</comments>
		<pubDate>Sun, 17 May 2009 20:05:50 +0000</pubDate>
		<dc:creator>mccv</dc:creator>
				<category><![CDATA[organization]]></category>

		<guid isPermaLink="false">http://www.themcwongs.com/?p=48</guid>
		<description><![CDATA[Something that comes up fairly frequently in my organization is the question of how to replicate success.  When team A does really really well, there&#8217;s an urge to copy whatever magic it is they had.  Unfortunately the approach usually devolves into how to copy the successful team, not copy the success of that [...]]]></description>
			<content:encoded><![CDATA[<p>Something that comes up fairly frequently in my organization is the question of how to replicate success.  When team A does really really well, there&#8217;s an urge to copy whatever magic it is they had.  Unfortunately the approach usually devolves into how to copy the successful team, not copy the success of that team.</p>
<p>As an example, let&#8217;s say team A had a fantastic development lead.  Somebody who could keep an eye out for development progress, overall quality, and what the customer really wanted.  This development lead was granted fairly wide ranging influence over various parts of the project.  They managed quality assurance, development, and were the final authority on what features made it into the project.  And everything went great.</p>
<p>So now team B is spinning up.  They have a good, but not fantastic development lead.  However they do have a few quality assurance engineers who are rock stars, and a very good project manager.  Should you grant the same authority to the development lead you did for team A?  Of course not.</p>
<p>Replicating success is a matter of first getting good people.  In big companies this can be fairly tough, as you usually have to play the hand you&#8217;re dealt.  Teams often get paralyzed as they try to find the equivalent of the rock star development lead from team A.  It&#8217;s critical to identify whether you can alter the members of the team (or to what extent you can swap out the members of the team) before planning the structure of that team.  </p>
<p>After getting the people, the most critical factor is structuring the team so that team strengths are provided the freedom to succeed, and team weaknesses are appropriately supported.  This is <i>rarely</i> accomplished by just applying the template from another team.  The good leaders understand this, and are constantly looking for the factors that make a team successful.  They then carefully consider the makeup of the next team, and put a structure in place to allow those specific team members to succeed.</p>
]]></content:encoded>
			<wfw:commentRss>http://tardigra.de/?feed=rss2&amp;p=48</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
