<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Not this... &#187; oops</title>
	<atom:link href="http://blog.timbunce.org/tag/oops/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.timbunce.org</link>
	<description>Listen. Reflect. Explore. Solve.</description>
	<lastBuildDate>Sat, 14 Jan 2012 21:59:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.timbunce.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Not this... &#187; oops</title>
		<link>http://blog.timbunce.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.timbunce.org/osd.xml" title="Not this..." />
	<atom:link rel='hub' href='http://blog.timbunce.org/?pushpress=hub'/>
		<item>
		<title>Code duplication, cheap but not free</title>
		<link>http://blog.timbunce.org/2008/03/25/code-duplication-cheap-but-not-free/</link>
		<comments>http://blog.timbunce.org/2008/03/25/code-duplication-cheap-but-not-free/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 15:50:39 +0000</pubDate>
		<dc:creator>TimBunce</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[oops]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://timbunce.wordpress.com/?p=27</guid>
		<description><![CDATA[I'm working on a large old codebase at the moment where Repeat Yourself seems to have been standard practice.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&amp;blog=2562816&amp;post=27&amp;subd=timbunce&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a large old codebase at the moment where Repeat Yourself seems to have been standard practice. Here&#8217;s a typical example:
<pre>    if (exists(Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{ $graph_id })
        &amp;&amp; Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{ $graph_id }-&gt;{ as_of_rev } &lt;= $current_revision) {
        $self-&gt;{cid} = Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{ $graph_id }-&gt;{ new_node_id };
    }
    elsif (exists(Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{''})
        &amp;&amp; Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{''}-&gt;{ as_of_rev } &lt;= $current_revision) {
        $self-&gt;{cid} = Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{ '' }-&gt;{ new_node_id };
    }</pre>
<p>The codebase has many, many, examples of this style written by a variety of developers.
<p>
What puzzles me is why this kind of code didn&#8217;t raise <a href="http://perl.plover.com/flagbook/">red flags</a> for the developers at the time. It&#8217;s harder to read, harder to maintain, and slower than a simpler approach. Perhaps the <code>elsif</code> part was a copy-n-paste job, but that still doesn&#8217;t explain the three instances of <code>Foo::StaticData::NodeRemap-&gt;get_data()-&gt;{ $args{cid} }-&gt;{ $graph_id }</code> in the first part. Perhaps even those were copy-n-pasted.</p>
<p>
I&#8217;d always have an urge to factor out the common expression into a temporary variable for efficiency and clarity. I wonder if my sensitivity to code duplication is partly due to needing to pay close attention to efficiency for most of my career. (I started Perl programming about 15 years ago, when cpu performance was measured in MHz.)</p>
<p>I also wonder how soon tools like Perl::Critic can help <a href="http://www.perlmonks.org/index.pl/jacques?node_id=667084">detect duplicate code</a> fragments. Common sub-expressions that may be candidates for elimination.</p>
<p>I&#8217;m working on optimizing the codebase at the moment. That chunk showed up as a performance issue so I rewrote it as</p>
<p>
<pre>
    my $NodeRemap = Foo::StaticData::NodeRemap-&gt;get_data();
    for my $id ( $graph_id, '' ) {
        my $x = $NodeRemap-&gt;{ $args{cid} }-&gt;{ $id };
        next unless $x and $x-&gt;{as_of_rev} &gt; $current_revision;
        $self-&gt;{cid} = $x-&gt;{new_node_id};
        last;
    }</pre>
</p>
<p>Spot my mistake?</p>
<p><font size="-2">Sidebar: This post is also an experiment in posting code to my blog. I&#8217;m trying out <a href="http://www.red-sweater.com/marsedit/">MarsEdit</a>. It&#8217;s good but I&#8217;d like to see a &#8220;Paste Preformatted&#8221; mechanism that would also html escape the contents of the paste buffer. It&#8217;s scriptable so I guess I could implement it myself in my copious spare time&#8230;</font></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/timbunce.wordpress.com/27/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/timbunce.wordpress.com/27/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timbunce.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timbunce.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timbunce.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&amp;blog=2562816&amp;post=27&amp;subd=timbunce&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.timbunce.org/2008/03/25/code-duplication-cheap-but-not-free/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cf82705f5ab43c73273ab5d690866b3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">TimBunce</media:title>
		</media:content>
	</item>
	</channel>
</rss>
