<?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>Incredible Vehicle &#187; Coding</title>
	<atom:link href="http://incrediblevehicle.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>https://incrediblevehicle.com</link>
	<description>(It&#039;s a blog.)</description>
	<lastBuildDate>Fri, 27 Apr 2012 03:50:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Learn Me a Haskell</title>
		<link>https://incrediblevehicle.com/2012/04/26/learn-me-a-haskell/</link>
		<comments>https://incrediblevehicle.com/2012/04/26/learn-me-a-haskell/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 03:50:37 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=1135</guid>
		<description><![CDATA[I&#8217;ve been wanting for some time to delve into functional programming (FP). As I&#8217;m sure I&#8217;ve said eleventy-billion-and-one times, I got a taste of some FP patterns in Ruby and JavaScript, and Python to a lesser extent. Well, now I have: I&#8217;ve been reading Learn You a Haskell for Great Good!. Rationale The fewer lines [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been wanting for some time to delve into functional programming (FP). As I&#8217;m sure I&#8217;ve said eleventy-billion-and-one times, I got a taste of some FP patterns in Ruby and JavaScript, and Python to a lesser extent. Well, now I have: I&#8217;ve been reading <a href="http://learnyouahaskell.com/">Learn You a Haskell for Great Good!</a>.</p>

<p><span id="more-1135"></span></p>

<h3>Rationale</h3>

<p>The fewer lines of code there are, the fewer bugs you&#8217;re likely to have. Generally, if you can say a lot with fewer words, without loss of precision or intelligibility, it&#8217;s a good idea. It&#8217;s also a good idea to push complexity down by using language features or standard libraries to do the heavy lifting for you.</p>

<p>What I find attractive about functional programming patterns like higher order functions is that it allows you to push some of the complexity downward, into the language or standard libraries, without losing expressiveness. You can say more with fewer words. If we want to accumulate a list of even numbers between 1 and 20 in Java, it ends up looking something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>Foo<span style="color: #339933;">&gt;</span> fooList <span style="color: #339933;">=</span> getFooList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>Foo<span style="color: #339933;">&gt;</span> fooBarList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>Foo<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Foo f <span style="color: #339933;">:</span> fooList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">hasBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    fooBarList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>This example is a bit contrived, but not terribly so. Filtering a list of items is relatively common, and despite that, this has a lot of lines for such a simple, common operation.</p>

<p>Lest it seem like I&#8217;m picking on Java, I&#8217;ll show you a rough equivalent in Go:</p>


<div class="wp_syntax"><div class="code"><pre class="golang" style="font-family:monospace;">fooList := getFooList()
var fooBars []Foo
for _, f := range fooList {
  if (f.HasBar()) {
    fooBars = append(fooBars, f)
  }
}</pre></div></div>


<p>We have less boilerplate in terms of declaring variable types, but it&#8217;s still a lot of lines for filtering a list on a simple boolean test.</p>

<p>I pick on Python a lot, but this is extremely simple in Python:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">foos = GetFooList<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
foo_bars = <span style="color: black;">&#91;</span>f <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> foos <span style="color: #ff7700;font-weight:bold;">if</span> f.<span style="color: black;">HasBar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></div></div>


<p>This, I would say, is a nearly perfect ratio of lines-to-operations. You could even replace <code>foo_list</code> with <code>GetFooList()</code>, but let&#8217;s not get carried away. Ruby takes this even further and provides a number of similar patterns for the <a href="http://www.ruby-doc.org/core-1.9.3/Array.html">Array</a> class.</p>

<p>I know this is crazy talk, but I think this is extremely powerful. You can express your intent very clearly in fewer lines. Your readers (yes, readers) are free to focus on <strong>what</strong> you are doing instead of <strong>how</strong>.</p>

<p>If you read about FP, you&#8217;ll also hear a lot about state. The notion is that if you&#8217;ve got methods attached to a class, the state of that class is implicit input to any given method. Coming from a testing perspective, I believe this makes a class harder to unit test. Sometimes you must store state, obviously, but in general a method which has explicit inputs and outputs is easier to test. (I suppose this is also applicable to trying to reason about what a method does mathematically.)</p>

<p>What was sort of revelatory to me is that, actually, a lack of mutable state isn&#8217;t so crazy (h/t <a href="http://www.youtube.com/watch?v=b9FagOVqxmI">Haskell Amuse-Bouche</a>). If you&#8217;ve used *nix for getting work done, you&#8217;ve probably done something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'foo.*bar'</span> ~<span style="color: #000000; font-weight: bold;">/</span>baz <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> <span style="color: #660033;">-c</span></pre></div></div>


<p>None of those are actually changing any state. You&#8217;re just filtering input. If you take the above — avoid state, give functions explicit input/output — to a reasonable conclusion, what you end up with is a glorified pipeline like what you see above. And this is one of the ideas behind Haskell.</p>

<h3>A simple example and then done</h3>

<p>In Haskell, you can compose a series of functions pretty easily, like this:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">negate</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">abs</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,-</span><span style="color: red;">19</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">24</span><span style="color: green;">&#93;</span></pre></div></div>


<p>It&#8217;s a slightly goofy <a href="http://learnyouahaskell.com/higher-order-functions#composition">math example</a> ripped off from Learn You a Haskell, but you get the idea. We chain together functions <code>negate</code> and <code>abs</code> as one function, and then map that onto the following list.</p>

<p>The concision here is impressive, although I find &#8220;real&#8221; Haskell considerably more difficult to read so far. Haskell programmers are very fond of one-letter variables and the syntax itself is so expressive in part because it is so concise. If you can decode it, it starts to become beautiful, but it&#8217;s that decoding part I still struggle with.</p>

<p>As I delve more into Haskell, I&#8217;ll probably blog about it. This is enough for now, though.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2012/04/26/learn-me-a-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Productivity</title>
		<link>https://incrediblevehicle.com/2012/04/25/productivity/</link>
		<comments>https://incrediblevehicle.com/2012/04/25/productivity/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 05:19:57 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=1082</guid>
		<description><![CDATA[So what languages do I think offer the best prospects for high-productivity coding buttressed by strong typing? Maybe it says something about what my opinion is worth that I&#8217;m not so sure about one of the two languages I was putting forward. Still, this is as good as time as any to talk about the [...]]]></description>
			<content:encoded><![CDATA[<p>So what languages do I think offer the best prospects for high-productivity coding buttressed by strong typing? Maybe it says something about what my opinion is worth that I&#8217;m not so sure about one of the two languages I was putting forward.</p>

<p>Still, this is as good as time as any to talk about the first one. Lately I&#8217;ve spent some time with <a href="http://www.golang.org/">the Go programming language</a>.</p>

<h3>Go</h3>

<p>If I had to sum up Go in my own words, I&#8217;d call it the spiritual successor to C, with modern sensibilities. It has concise and lightweight syntax. It&#8217;s memory safe (read: garbage-collected) and strongly typed. It has powerful concurrency primitives at the language level. It has a unique approach to interfaces where they are actually orthogonal to data types, not unlike Haskell&#8217;s typeclasses. It has first class functions and lexical closures. And it has a rich standard library.</p>

<p>Although Go doesn&#8217;t ship with a REPL or interpreter, there are some <a href="https://bitbucket.org/binet/igo">projects</a> <a href="https://github.com/vito/go-repl">out</a> <a href="https://github.com/mattn/yet-another-go-repl">there</a>. However, there&#8217;s also <a href="">http://play.golang.org/</a>. As far as I&#8217;m concerned, this is close enough to a REPL that I&#8217;ve found it extremely useful for prototyping, testing out a package from the Go standard library, and I&#8217;ve even written code I&#8217;ve ended up keeping later.</p>

<p>Go threads the needle between providing a strongly typed language which is more productive than C++ and orders of magnitude less verbose and clunky than Java. It&#8217;s more performant than your average scripting language, yet the standard library is just as rich and easy to use. I would probably not go back to Python except for ~100 line helpers, or glue code which is just above shell scripts in terms of complexity.</p>

<p>It&#8217;s tough to pick good examples for some rough comparison that aren&#8217;t already covered in the Go docs. But here&#8217;s one of my hobby-horses, something which is easy in some scripting languages.</p>

<h3>Processing command output</h3>

<p>This is trivially easy in shell scripts, and shell scripts were made for processing text. Perl makes it approximately as easy, and as Ruby borrows so much from Perl, it&#8217;s similarly easy.</p>

<p>In Python 2.x, <a href="http://incrediblevehicle.com/2011/08/31/python-python-python/">it&#8217;s terrible</a>. I&#8217;d rather not write this in C++ or Java.</p>

<p>Here&#8217;s how you run a simple command in Go and capture its output:</p>

<pre><code>// exec lives under the os/exec package
out, err := exec.Command("foo", "--bar", "baz").Output()
</code></pre>

<p><code>out</code> is the output, an array of bytes. You&#8217;ll want to split it in lines and iterate over it, treating it as (say) an id and a username along with it.</p>

<pre><code>lines := bytes.Split(out, '\n')
for _, line := range lines {
  // Split by spaces.
  fields := bytes.Fields(line)
  fmt.Println("id is %v. username is %v.", fields[0], fields[1])
}
</code></pre>

<p>Notice the lack of type declarations. Most of the time you use <code>:=</code> to declare and initialize at once, which ends up feeling a lot like Python or Ruby. The difference is that the Go compiler infers types based on return values and the like, granting the benefit of type checking.</p>

<h3>Further reading</h3>

<p>I could come up with more examples. I&#8217;m trying not to! That&#8217;s because you&#8217;re better off playing with something like <a href="http://tour.golang.org/">the Go tour</a>. Trust me: you might think it a bit silly at first, but there are exercises and the app will test your solution. It&#8217;s very good.</p>

<p>The Go site is rather good, as well. There&#8217;s [a Go tutorial] wherein you write a wiki which is worth your time. You should read <a href="http://golang.org/doc/effective_go.html">Effective Go</a> if you&#8217;ve decided you&#8217;re actually serious about it.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2012/04/25/productivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sublime Text 2</title>
		<link>https://incrediblevehicle.com/2012/04/21/sublime-text-2/</link>
		<comments>https://incrediblevehicle.com/2012/04/21/sublime-text-2/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 17:04:52 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[st2]]></category>
		<category><![CDATA[sublimetext2]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=1112</guid>
		<description><![CDATA[Today I took a relatively big step in terms of my coding workflow. I purchased a license for Sublime Text 2 and immediately began using it as my full-time code editor. Advantages Vim keybindings for 70% &#8211; 90% of the commands I use (e.g. ci(, marks, :w) Find in files Cmd-R to jump to a [...]]]></description>
			<content:encoded><![CDATA[<p>Today I took a relatively big step in terms of my coding workflow. I purchased a license for <a href="http://www.sublimetext.com/">Sublime Text 2</a> and immediately began using it as my full-time code editor.</p>

<p><span id="more-1112"></span></p>

<h3>Advantages</h3>

<ul>
<li>Vim keybindings for 70% &#8211; 90% of the commands I use (e.g. <code>ci(</code>, marks, <code>:w</code>)</li>
<li>Find in files</li>
<li>Cmd-R to jump to a symbol</li>
<li>Command-T-style fuzzy completion of filenames via Cmd-P</li>
<li>Projects (i.e. a list of files spread across disparate dirs accessible via Cmd-P)</li>
<li>Menu commands executable through Cmd-Shift-P, complete with fuzzy matching</li>
<li>A sensible configuration &amp; plugin language</li>
<li>Fast. Buttery smooth.</li>
<li>Linux AND Mac compatibility</li>
<li>One license, all of your machines</li>
</ul>

<p>It&#8217;s not that Vim can&#8217;t do most of this stuff (modulo the sensible configuration language). It&#8217;s that all of the above <em>works out of the box</em>. There&#8217;s a litany of things that I&#8217;ve just never quite gotten to work right. For instance, CommandT worked for a while and then broke. I don&#8217;t mean to pick on anybody. But this was not uncommon.</p>

<p>That was perhaps the single biggest reason that I switched.</p>

<p>At another point, I was trying to fix bugs in a dozen or so Python scripts, themselves scattered across three or more different subdirectories at various depths. In ST2, I added all three dirs to my project, and I bounced between them with Cmd-P quickly and easily. It just worked. And it&#8217;s <em>fast</em>. It breaks my heart to say it but Vim felt positively medieval in comparison.</p>

<p>I also look forward to learning how to write plugins. I&#8217;ve never been able to convince myself to learn Vim&#8217;s scripting language.</p>

<p>There are some disadvantages worth mentioning.</p>

<h3>Disadvantages</h3>

<ol>
<li>Split-pane isn&#8217;t as good as Vim</li>
<li>Inaccessible via the Terminal</li>
<li>Not libre or gratis</li>
<li>I may run into cognitive dissonance wrt Vim bindings</li>
</ol>

<p>With ST2, you have to switch between a canned set of layouts rather than being able to ad-hoc split pane. With Vim, I have anywhere from 3 &#8211; 5 panes open, and with <code>^W</code> and some handy <code>\ew</code> aliases (edit file in same working dir as this file), it&#8217;s mostly muscle memory.</p>

<p>I can&#8217;t use ST2 in the Terminal, which means I&#8217;ll have to fallback to Vim. That&#8217;s not the end of the world, but it is inconvenient.</p>

<p>I hesitated for weeks, having taken it for a test drive for some of the toying around I&#8217;ve done with <a href="http://golang.org">Go</a>. But it wasn&#8217;t about FOSS. It was about the price. Why pay for a text editor when there are N free ones available?</p>

<p>That said, I am willing to pay a little bit of money to get the job done. (Shit, if time is money, it&#8217;s already saved me a ton of time.) And I know that I&#8217;m supporting a small and independent developer. I want to live in a world where people can make money off of developing kick-ass software.</p>

<p>Vintage (the plugin for ST2 which adds Vim keybindings) does have some limitations. <code></code> &#8220;, double grave, doesn&#8217;t work right. <code>g;</code> and <code>g,</code> don&#8217;t work. I&#8217;m sure I&#8217;ll run across more.</p>

<h3>Conclusion</h3>

<p>It&#8217;s been some weeks since I switched, and I haven&#8217;t really looked back. If you&#8217;ve got the dough and you&#8217;re looking for an editor that&#8217;ll help you step your game up, I recommend you check out Sublime Text 2. As I said, it&#8217;s a free trial.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2012/04/21/sublime-text-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In search of productivity</title>
		<link>https://incrediblevehicle.com/2012/02/18/in-search-of-productivity/</link>
		<comments>https://incrediblevehicle.com/2012/02/18/in-search-of-productivity/#comments</comments>
		<pubDate>Sat, 18 Feb 2012 23:20:11 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=1055</guid>
		<description><![CDATA[Once upon a time, I started to learn programming in C++. It turns out that it&#8217;s hard (for some value of &#8220;hard&#8221;) to do a lot of basic things in C++. As a budding programmer, I spent more time struggling to make myself understood to the compiler than writing code. When I was introduced to [...]]]></description>
			<content:encoded><![CDATA[<p>Once upon a time, I started to learn programming in C++. It turns out that it&#8217;s hard (for some value of &#8220;hard&#8221;) to do a lot of basic things in C++. As a budding programmer, I spent more time struggling to make myself understood to the compiler than writing code.</p>

<p>When I was introduced to Python, suddenly programming was fun again. It was hard to imagine going back to anything like C++ when I could become so productive so quickly with Python.</p>

<p>I held to this view for a very, very long time. I appreciated how languages like C++ or Java would be useful for writing &#8220;serious&#8221; applications, but I didn&#8217;t write that stuff. That was for other people.</p>

<p>In the last year or so, I&#8217;ve gradually come to believe that a strong type-checking system AND a high-productivity language ought not and are not mutually exclusive. The corollary is that I will consider carefully before I use a scripting language for anything more than a thousand or so lines.</p>

<p><span id="more-1055"></span></p>

<h3>The first sign of trouble</h3>

<p>The first time I ran into serious trouble with a scripting language was a few years ago (yikes). I wrote a script in Python which would monitor some servers for updated software and kick off a job when they were updated. It was less than 1000 lines, but despite knowing better, I did a poor job keeping it clean. And because it was such a mess, of course I had no tests.</p>

<p>Without unit tests, I had to test it manually. The cycle was something like: write a bunch of code, and run the whole dang script. This was time-consuming and error-prone. I&#8217;d miss things. Basic things. The script itself wasn&#8217;t mission critical but I found these bugs and the script itself personally embarrassing; I knew better, and I knew I knew better.</p>

<p>Still, I was otherwise quite productive in Python. Around the same time, using the Django framework, I wrote a server which slurped XML-formatted test results and allowed the user to view results grouped by various properties. It was very fast to write, and I could hardly imagine doing it in C++ or Java.</p>

<p>I had three experiences in the last year or two which rearranged my prejudices.</p>

<h3>JavaScript and the Closure Compiler</h3>

<p>The first was working in JavaScript land, with the <a href="http://code.google.com/closure/compiler/">Closure Compiler</a>. JavaScript was far less restrictive and, in my opinion, more expressive than Java. The compiler caught some of my dumb mistakes and I grudgingly came to value it. However, it didn&#8217;t catch all of my mistakes and the ones that it did not catch were sometimes difficult to track down.</p>

<p>The lesson I learned was that even the slightest type-checking can really help save you from yourself. It also makes refactoring (and thus maintenance) easier; adding or removing a parameter, or passing in the wrong type can be difficult to debug in a language like JavaScript.</p>

<h3>Java</h3>

<p>The second was working in Java land. I wrote a framework which would test a remote API. I had to do a lot of fighting with the language, with a lot of boilerplate and book-keeping. I missed closures and first class functions dearly; contrary to what I thought at first, Java does have a very limited form of closures, in the form of anonymous inner classes. The lengths I had to go to get certain pieces working was comical; at one point, I wrote a <a href="http://incrediblevehicle.com/2011/07/25/java-me/">FactoryFactory</a>. I&#8217;m not kidding! And I&#8217;m not proud of it, but the language and API I was using drove me to it.</p>

<p>However, up to this point in my career, I don&#8217;t think I&#8217;d spent as much time with a decent IDE on an extended project. Several aspects were revelatory. I&#8217;ll mention two.</p>

<p>1) The type system gave me substantially more confidence that I was doing the right thing most of the time. I spent more time fixing incorrect assumptions about the API, adding error-recovery, or simply writing code than chasing down basic mistakes. I do that in any language, but the difference here was that the IDE and language eliminated an entire class of mistakes <em>as I introduced them</em>. By the time I ran my program, I had substantially more confidence about its correctness than I ever did in Python.</p>

<p>2) If you haven&#8217;t yet, read Martin Fowler&#8217;s <a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_ss_tl?ie=UTF8&amp;tag=incredvehicl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201485672">Refactoring</a>. The short version is that there are a set of highly mechanical techniques you can use to facilitate refactoring. Furthermore, many Java IDEs support many of these operations. Even something as basic as renaming method calls or variables within a specific context is massively more helpful than dumb S&amp;R.</p>

<h3>Lest I seem insane</h3>

<p>Despite all that, I still wouldn&#8217;t program in Java again by choice. I want closures. I want first class functions. I want built-in higher order functions, for that matter. I want syntax and/or a syntactic paradigm (think variables or methods like fooByBarWithBazAsQuux) that&#8217;s substantially less verbose.</p>

<p>It&#8217;s the difference between</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">numbers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>
evens = numbers.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">2</span> == <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>


<p>and</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// I'm sure this is still invalid for many reasons.</span>
ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> intList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> evenIntsList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> i <span style="color: #339933;">:</span> intList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    evenIntsList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>That? That up there? It hurts me inside. The compiler makes it harder for you to screw it up, but you still have to do the whole song and dance every. Single. Time. You spend ~6 lines on an operation which, in the grand scheme of your program, ought to be small and trivial.</p>

<h3>Python</h3>

<p>Speaking of Python, I started relatively recently on refactoring a set of Python scripts responsible for kicking off tests. Nobody owned these scripts and consequently they languished as multiple people had patched or hacked on them to fix bugs or add features. The scripts were a mess.</p>

<p>Furthermore, there are few thousand lines of these scripts, plus thousands of lines more in the form of libraries which the scripts use. There were and are tests, but obviously nobody ran them because they were all broken.</p>

<p>After having spent so much time in the Land of Type-checking, reality came rushing back. It scary to make changes without having any confidence that they were correct. I wrote tests to find basic mistakes which would not manifest until runtime.</p>

<p>I would say that this was the biggest shock and what did the most to change my views. A scripting language is very good for getting something running quickly, but in terms of refactoring and maintenance, it took two or three times as long as I thought it ought to.</p>

<h3>A word about tests</h3>

<p>You could make a strong case that the scenario above— writing tests to the point of boredom— is Working As Intended. I am strongly sympathetic to this idea.</p>

<p>You ought to write unit tests with good coverage no matter the language. Tools like <a href="https://github.com/gfxmonk/autonose">autonose</a> allow you to get feedback from those tests rather quickly. This workflow arguably gives you faster, more comprehensive feedback than one with just a compiler and manually run unit tests.</p>

<p>One hitch is that getting a good error message from a unit test requires an amount of diligence multiplied by the number of unit tests. A compiler does only catch a small subset of the problems a unit test would, but any decent one ought to catch them more reliably and precisely.</p>

<p>Anyway, I haven&#8217;t delved too much into larger-scale projects which are based on scripting languages, but my guess is that their approach is similar to this. Write testable code. Use frameworks which make it easy to write tests (with a nice DSL, flexible mocking, or what have you). Use frameworks which make it easy to run tests (such as speed and parallelism). Minimize the inherent friction and you&#8217;ll get something richer than a compiler alone would offer you.</p>

<h3>The answer?</h3>

<p>I&#8217;m still writing tests in Java. I&#8217;m still refactoring Python scripts. For obvious reasons it would be silly to introduce a new technology into an existing stack chiefly or even partially for my own personal satisfaction. Rewriting is a dubious proposition anyway, one that must be considered very carefully. So for practical reasons, the situation won&#8217;t change.</p>

<p>Even so, I still want to have some go-to language. What could I use for a new project, personal or otherwise which would have good odds of making me happy?</p>

<p>C++ is not memory safe. C is simpler than C++ but is also memory unsafe and has more trade-offs. In both C and C++, it can be very difficult to write basic functionality.</p>

<p>Java is memory safe and has a rich set of libraries, but as I&#8217;ve mentioned, I&#8217;m not interested.</p>

<p>So what&#8217;s the answer? What language or technology do I feel is most promising, offering a combination of strong typing and high productivity? I have one prospect, or even two. But I&#8217;ll save those for the next post.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2012/02/18/in-search-of-productivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python, Python, Python</title>
		<link>https://incrediblevehicle.com/2011/08/31/python-python-python/</link>
		<comments>https://incrediblevehicle.com/2011/08/31/python-python-python/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 02:46:34 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=978</guid>
		<description><![CDATA[Oh, god. I still have a bunch of rant left in me. So here we go, Internet: yet another angry rant to add to the pile. Sometimes, in the course of one&#8217;s life, one is left with a one-off task. In this case, I needed to call a binary a whole lot of times, and [...]]]></description>
			<content:encoded><![CDATA[<p>Oh, god. I still have a bunch of rant left in me. So here we go, Internet: yet another angry rant to add to the pile.</p>

<p>Sometimes, in the course of one&#8217;s life, one is left with a one-off task. In this case, I needed to call a binary a whole lot of times, and do something with the output each time. The details aren&#8217;t important; I just needed to write a wrapper script for this binary, do a modest amount of processing on it, and then output the result to a file. This is a pretty common task, one to which the command line in general and *nix in particular is well-suited.</p>

<p>Now, for various reasons, I prefer not to do this via shell scripts. I don&#8217;t have a hard and fast rule for when or why. Unless you routinely write shell scripts (I don&#8217;t), you&#8217;ll inevitably spend a bunch of time looking stuff up that you looked up, oh, six months ago. Or at least I do. I don&#8217;t enjoy it, so if I can&#8217;t do it with a few commands, maybe one loop or so, then I prefer to use a scripting language. I have the advantage of better/clearer failure modes and simpler syntax and I feel like I&#8217;m learning something more powerful and more widely applicable in the process.</p>

<p><span id="more-978"></span></p>

<p>One of my &#8220;favorite&#8221; things to forget and look up again is the difference between <code>[[ ]]</code> and <code>[ ]</code> in tests. Another one is how you loop over a file or output from a command (<code>while read line; do bar --baz $line; done &lt;foo</code> and <code>foo | while read line; do bar $line; done</code>, respectively).</p>

<p>If you have no trouble remembering this stuff or you write scripts often enough, I suppose this is moot. There was a time when I was scripting enough that I didn&#8217;t have to look all this stuff up every time, but that time is past. YMMV and all that.</p>

<p>Anyway, the point is that I probably could&#8217;ve done this with something like this (which is simplified somewhat):</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">ARGS</span>=<span style="color: #ff0000;">&quot;--bar=baz&quot;</span>
<span style="color: #007800;">CMD</span>=<span style="color: #ff0000;">&quot;~/bin/foo&quot;</span>
<span style="color: #007800;">KEY</span>=<span style="color: #ff0000;">&quot;quux&quot;</span>
<span style="color: #007800;">MIN</span>=<span style="color: #000000;">0</span>
<span style="color: #007800;">MAX</span>=<span style="color: #000000;">10000</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">0</span> <span style="color: #000000;">1000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> num <span style="color: #000000; font-weight: bold;">do</span>;
  <span style="color: #007800;">$CMD</span> <span style="color: #007800;">$num</span> <span style="color: #007800;">$ARGS</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-q</span> <span style="color: #007800;">$KEY</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;&lt;</span><span style="color: #007800;">$line</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #007800;">value</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'[1-9]+$'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$num</span>,<span style="color: #007800;">$value</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>


<p>I&#8217;m sure I have a bajillion bugs in there. For example, I am not at all confident about the outer <code>for</code> loop syntax. And while it was easy to write, I&#8217;m not doing anything with command line args, doing any error handling, or anything like that. (On the other other hand, I uh underestimated how easy this was. That&#8217;s partially why I&#8217;m convinced there are bugs in there.)</p>

<p>I still don&#8217;t know how to write idiomatic (typo: idiotmatic) Ruby, so this is going to be very rough. Still, it feels natural to me, minus one or two hitches:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ARGS = <span style="color:#996600;">&quot;--bar=baz&quot;</span>
CMD = <span style="color:#996600;">&quot;~/bin/foo&quot;</span>
KEY = <span style="color:#006600; font-weight:bold;">/</span>quux<span style="color:#006600; font-weight:bold;">/</span>
MIN = <span style="color:#006666;">0</span>
MAX = <span style="color:#006666;">10000</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> main
  <span style="color:#006600; font-weight:bold;">&#40;</span>MIN..<span style="color:#9900CC;">MAX</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    out = <span style="color:#006600; font-weight:bold;">%</span>x<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#008000; font-style:italic;">#{CMD} #{i} #{ARGS} ]</span>
    out.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'<span style="color:#000099;">\n</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
      KEY.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span>
        value = line.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{i},#{value}<span style="color:#000099;">\n</span>&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>I&#8217;m not confident about the outer loop once again, and I&#8217;m not sure the call to match() will do what I want, let alone whether it&#8217;s elegant. Still, I feel pretty good about it. I love Ruby&#8217;s pattern of passing in blocks.</p>

<p>Fortunately or unfortunately, I had to use Python. And don&#8217;t get me wrong: I love Python. It is through Python that I learned to love scripting languages. Processing a file line by line was, I think, the real epiphany. And list comprehensions are wonderfully expressive.</p>

<p>But man alive is it <strong>awful</strong> for this sort of thing. I&#8217;m not even going to bother writing it out in Python. I&#8217;ll just excerpt, from the <a href="http://docs.python.org/library/subprocess.html#replacing-bin-sh-shell-backquote">2.7 subprocess docs</a>. Here&#8217;s what they say should replace backticks <code>output = \</code>mycmd myarg&#96;`:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">output = Popen<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;mycmd&quot;</span>, <span style="color: #483d8b;">&quot;myarg&quot;</span><span style="color: black;">&#93;</span>, stdout=PIPE<span style="color: black;">&#41;</span>.<span style="color: black;">communicate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>


<p>Mind you, this is if you did <code>from subprocess import *</code>. Generally I don&#8217;t, which means it ends up looking like this:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">output = \
  <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;mycmd&quot;</span>, <span style="color: #483d8b;">&quot;myarg&quot;</span><span style="color: black;">&#93;</span>, <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">stdout</span>=PIPE<span style="color: black;">&#41;</span>.\
  communicate<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>


<p>Yes, once you have it written, sequestered in its own function that you never touch again, it&#8217;s not so bad. However, this is firmly in the category of something I will not be able to (and have not been able to) remember months later.</p>

<p>It is also not discoverable in the sense that it&#8217;s highly particular&#8212; <code>stdout=PIPE</code>? Really? Compare and contrast opening a file (<code>for line in open('somefile'): print foo(line)</code> or <code>[foo(line) for line in open('somefile')</code>) with this monster. Even my Ruby example could have used backticks if I hadn&#8217;t remembered the <code>%x</code> syntax. The best I can say about <code>subprocess</code> is that it&#8217;s a) better than <code>Popen</code> in Python 2.4 (or whatever), and b) it&#8217;s easier to search the web for than <code>%x</code>.</p>

<p>The kicker of course is that rest of the Python script was very easy to write! However, since calling the binary was changing some external state, though, I had to make sure it was doing the right thing. In the end building and testing the call to <code>subprocess.Popen()</code> took longer than the rest of the script. In an otherwise elegant, no-bullshit, batteries included language, the <code>subprocess</code> module is a terrible blemish. It doesn&#8217;t look any better in Python 3.0, either, unfortunately.</p>

<p>Even more unfortunately, this is ultimately why it would be my preference to use either Ruby or shell. Ruby seems to work well for a variety of tasks, from writing a full-fledged webapp to some grungy text manipulation. You don&#8217;t have to compromise because it&#8217;s quite easy to use it for Python-y things as well as Perl-y things. It&#8217;s just a shame that Python seems to treat this case with a bizarre kind of fussiness incongruent with the rest of the language and standard library.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2011/08/31/python-python-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java &amp; Me</title>
		<link>https://incrediblevehicle.com/2011/07/25/java-me/</link>
		<comments>https://incrediblevehicle.com/2011/07/25/java-me/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 06:15:42 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[FP]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=926</guid>
		<description><![CDATA[I&#8217;ve been coding lately, and fortunately or unfortunately, it&#8217;s Java. I hadn&#8217;t done much Java before I started working on this current project; my experience before this was with C++ in college, a bunch of Python, a splash of Ruby, a bunch of JavaScript, and a very small pinch of Objective-C/CocoaTouch. It&#8217;s JavaScript and Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding lately, and fortunately or unfortunately, it&#8217;s Java. I hadn&#8217;t done much Java before I started working on this current project; my experience before this was with C++ in college, a bunch of Python, a splash of Ruby, a bunch of JavaScript, and a very small pinch of Objective-C/CocoaTouch.</p>

<p>It&#8217;s JavaScript and Ruby that&#8217;ve influenced me most lately. Functional patterns in particular have attracted my attention. I really dig stuff like this in Ruby:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'somefile'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">readlines</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">print</span> x
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>And then I spent a whole boatload of time in JavaScript, where you get to do stuff like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> someArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
goog.<span style="color: #660066;">array</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Here's a number: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>What I <em>really</em> dig about this pattern is passing in a function where I can do whatever I like. The factory pattern becomes much simpler. We can pass this around to whoever:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> reqFactory <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  req.<span style="color: #660066;">setUrl</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  req.<span style="color: #660066;">addHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;quux&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> req<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>


<p>The current conundrum involves implementing some kind of retry logic. In one case, I need to build a new request each time the request fails. In another case, I just want to retry for a certain subset of exceptions. In yet another case, if I make the request and it doesn&#8217;t have what I want, I want to re-issue it.</p>

<p>In Java, it&#8217;s— it&#8217;s messy. I don&#8217;t know what the Java-esque way of doing it is, quite frankly. I find myself wanting to create a Retrier interface which defines an onSuccess, onFailure, and a doRetry method.</p>

<p>I&#8217;m not sure what each method should return; what if I want to be able to return whatever it is that we got from the request? Do I create a wrapper class w/generics? I think this is how it works in the <a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html">Kingdom of Nouns</a>. The nouns just keep proliferating.</p>

<p>The one thing I think Java has going for it is anonymous inner classes. I feel like the way files and classes proliferate in Java is nuts, and being able to keep the implementation of such as a Retrier close to where it&#8217;s used seems like a win. It&#8217;s a one-off, right? A whole class &amp; implementation &amp; file devoted to it feels like it&#8217;s belaboring the point.</p>

<p>I&#8217;m sure at some point I&#8217;ll figure it out and/or get used to it. In the meantime, every time I want to add a layer of abstraction, it feels like massive overkill. I almost wrote up code samples for the above but it turned out to be too much effort. I can&#8217;t be as glib with it, at least not yet. Oh well.</p>
]]></content:encoded>
			<wfw:commentRss>https://incrediblevehicle.com/2011/07/25/java-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
