<?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>blog of josh &#187; agent ralph</title>
	<atom:link href="http://landofjosh.com/tag/agent-ralph/feed/" rel="self" type="application/rss+xml" />
	<link>http://landofjosh.com</link>
	<description>software development under the big arch</description>
	<lastBuildDate>Thu, 22 Oct 2009 06:26:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Agent Ralph In Action</title>
		<link>http://landofjosh.com/2009/08/agent-ralph-in-action/</link>
		<comments>http://landofjosh.com/2009/08/agent-ralph-in-action/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 05:15:51 +0000</pubDate>
		<dc:creator>josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[agent ralph]]></category>
		<category><![CDATA[code-clones]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://landofjosh.com/?p=169</guid>
		<description><![CDATA[I&#8217;ve been yack yack yacking about clone detection and Agent Ralph.  It&#8217;s time to put up or shut up.  This post is some screen shots of Agent Ralph in action. Agent Ralph&#8216;s front end is a Resharper plug-in.  Any clones detected are passed up to the plug-in which presents them to the user as highlights and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been yack yack yacking about clone detection and Agent Ralph.  It&#8217;s time to put up or shut up.  This post is some screen shots of Agent Ralph in action.</p>
<p><a title="Agent Ralph Project" href="http://code.google.com/p/agentralphplugin/">Agent Ralph</a>&#8216;s front end is a <a title="JetBrain's Resharper" href="http://www.jetbrains.com/resharper/">Resharper</a> plug-in.  Any clones detected are passed up to the plug-in which presents them to the user as highlights and quick fixes.  This is how we achieve the automated repair that a modern clone tool needs.  The backend scans source files handed to it by the front end, using the techniques I&#8217;ve been <a href="http://landofjosh.com/2009/07/an-idea-for-robust-clone-detection-using-abstract-syntax-trees/">blogging about</a>.  Specifically, clones are identified by comparing abstract syntax trees of methods.  The ASTs may be modified by the application of safe refactorings (refactorings that do not change the inputs, outputs, or side effects).  If an AST can be safely coerced until it matches another then we can consider the originals functionally equivalent clones.  This technique will detect clones that would otherwise be overlooked by text based clone finders.</p>
<p>So, here&#8217;s the basic case.  Two identical methods:</p>
<p><img class="size-full wp-image-170 alignnone" title="identicalmethodshighlight-cropped" src="http://landofjosh.com/wp-content/uploads/2009/08/identicalmethodshighlight-cropped.png" alt="identicalmethodshighlight-cropped" width="436" height="195" /></p>
<p>Note the Resharper squigglies telling us something is up.  Passing the mouse over either method name brings up a tooltip identifying the method as a clone of the other.</p>
<p>Placing the cursor on the method name prompts you with a <a title="Resharper Quick Fixes" href="http://www.jetbrains.com/resharper/features/code_analysis.html#Quick-Fixes">quick fix</a>&#8230;</p>
<p><img class="alignnone size-full wp-image-173" title="identicalmethodsquickfix-cropped" src="http://landofjosh.com/wp-content/uploads/2009/08/identicalmethodsquickfix-cropped.png" alt="identicalmethodsquickfix-cropped" width="471" height="229" /></p>
<p>&#8230;and invoking it&#8230;</p>
<p><img class="alignnone size-full wp-image-172" title="identicalmethodsquickfixapplied-cropped" src="http://landofjosh.com/wp-content/uploads/2009/08/identicalmethodsquickfixapplied-cropped.png" alt="identicalmethodsquickfixapplied-cropped" width="455" height="198" /></p>
<p>&#8230;replaces the body of the clone with a call to the original.  That&#8217;s automated clone repair!  An inline method applied to Test1 will complete the removal.</p>
<p>The next methods are identical, but only if a rename local variable refactoring is applied.  And indeed you can see that it is, indicated by the highlighting and quickfix offering.</p>
<p><img class="size-full wp-image-175 alignnone" title="clonewithrenamelocal-cropped" src="http://landofjosh.com/wp-content/uploads/2009/08/clonewithrenamelocal-cropped.png" alt="clonewithrenamelocal-cropped" width="458" height="265" /></p>
<p>The last example is one I am particularly proud of.   Here we are detecting a clone that is a block within a larger method.  Methods EmbeddedClone1 and EmbeddedClone2 both contain clones of Test2.</p>
<p><img class="alignnone size-full wp-image-179" title="embeddedclonequickfix-cropped" src="http://landofjosh.com/wp-content/uploads/2009/08/embeddedclonequickfix-cropped.png" alt="embeddedclonequickfix-cropped" width="470" height="415" /></p>
<p>Thus far I&#8217;ve restricted myself to using methods as the only unit of comparison.  Doing so made it easier to reason and implement as I worked through ideas.  At some point I realized that I could use an extract method refactoring to create provisional methods from indiscriminate code blocks on the fly.  If the provisional method is a clone then it follows that the original code block is a clone.  In this way I can continue to think and code in terms of methods, yet rely on the extract method refactoring to apply my algorithms to sub-units of method (aka, arbitrary blocks and statements).</p>
]]></content:encoded>
			<wfw:commentRss>http://landofjosh.com/2009/08/agent-ralph-in-action/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unnecessary Complexity Case Study #1: Untyped Enum Comparisons</title>
		<link>http://landofjosh.com/2009/05/unnecessary-complexity-case-study-1-untyped-enum-comparisons/</link>
		<comments>http://landofjosh.com/2009/05/unnecessary-complexity-case-study-1-untyped-enum-comparisons/#comments</comments>
		<pubDate>Thu, 07 May 2009 13:27:27 +0000</pubDate>
		<dc:creator>josh</dc:creator>
				<category><![CDATA[unnecessary-complexity]]></category>
		<category><![CDATA[agent ralph]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://landofjosh.com/?p=12</guid>
		<description><![CDATA[This post is the first in a series of posts on specific examples of unnecessary complexity.   Consider this code: enum MyEnum  { First, Second } public void Match1(MyEnum e) {     if (e.ToString() == "First") { } } The problem lies in the condition of the if.  The developer is converting an enum instance [...]]]></description>
			<content:encoded><![CDATA[<p>This post is the first in a series of posts on specific examples of unnecessary complexity.  </p>
<p>Consider this code:</p>
<pre name="code" class="csharp">enum MyEnum  { First, Second }
public void Match1(MyEnum e)
{
    if (e.ToString() == "First") { }
}</pre>
<p>The problem lies in the condition of the if.  The developer is converting an enum instance to a string for the purposes of a comparison, circumventing type safety.  The primary problem here is that the statement can no longer safely participate in automated refactorings.  For example, if my MyEnum.First is renamed this code will break such that the condition is always false and the body will never execute.  A Find Usages executed on MyEnum.First would not identify this site.  </p>
<p>It also performs worse than the alternatives.  I hesitate to even mention that though because, like most micro performance optimizations, it is only going to matter in uncommon situations like a tight loop or heavy load.</p>
<p>The correct code is this:</p>
<pre name="code" class="csharp">if(e == MyEnum.First) { }</pre>
<p>The code is now typesafe, fast (it&#8217;s a comparison of integers), and refactoring friendly.  And it&#8217;s a simple enough fix.  The exact steps are:</p>
<ol>
<li>Scan the enum values list for a member whose name exactly matches the string literal.</li>
<li>If found, replace the string literal with a reference to the qualified enum value.</li>
</ol>
<p>There are some special cases to consider as well.</p>
<p>Here the string is being manipulated before comparing, by calling ToLower().</p>
<pre name="code" class="csharp">if(e.ToString().ToLower() == "first") { }</pre>
<p>When comparing the result of a ToLower() or ToUpper() call the string constant is single cased, likely with the intention of reducing typos.  I suspect the programmer considered this <a href="http://en.wikipedia.org/wiki/Defensive_programming">defensive programming</a>.</p>
<p>Unnecessary complexity begets bugs.  When your code base is littered with untyped enum comparisons, this mistake is inevitable:</p>
<pre name="code" class="csharp">if(e.ToString().ToLower() == "First") { }</pre>
<p>The string constant actually matches the declared enum casing, yet due to the ToLower() call the condition is always false, and the branch is never executed.  This permutation is particularly dangerous as the fix revives a branch that hasn&#8217;t been executing. You are eliminating a side effect, and a danger of eliminating side effects is breaking other parts of the system that could be dependent on them.</p>
<p>I have created a tool for automating the correction of untyped enum comparisons.  It&#8217;s a Resharper plug-in based <a href="http://www.jetbrains.com/resharper/features/code_analysis.html#Quick-Fixes">quick fix</a> and is available as part of my <a title="Agent Ralph" href="http://code.google.com/p/agentralphplugin/">Agent Ralph</a> project.</p>
<div id="attachment_18" class="wp-caption alignnone" style="width: 516px"><img class="size-full wp-image-18  " title="Make Enum Comparison Typesafe" src="http://landofjosh.com/wp-content/uploads/2009/05/makeenumcomparisontypesafe1.png" alt="Make Enum Comparison Typesafe" width="506" height="120" /><p class="wp-caption-text">Using Agent Ralph to automate a Make Enum Comparison Typesafe code correction.</p></div>
<p>Currently it only handles the simple case, pictured above.  I hope to correct that deficiency soon.</p>
<p><em>Thanks to <a href="http://www.fooberry.com">Mark</a> for the great suggestion on a WordPress friendly <a href="http://wordpress.org/extend/plugins/google-syntax-highlighter/">code syntax highlighter</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://landofjosh.com/2009/05/unnecessary-complexity-case-study-1-untyped-enum-comparisons/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
