<?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>http://incrediblevehicle.com</link>
	<description>(It's a blog.)</description>
	<lastBuildDate>Sun, 19 Feb 2012 00:20:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>In search of productivity</title>
		<link>http://incrediblevehicle.com/2012/02/18/in-search-of-productivity/</link>
		<comments>http://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>http://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>http://incrediblevehicle.com/2011/08/31/python-python-python/</link>
		<comments>http://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>http://incrediblevehicle.com/2011/08/31/python-python-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &amp; Me</title>
		<link>http://incrediblevehicle.com/2011/07/25/java-me/</link>
		<comments>http://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>http://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! -->
