<?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>{ Code: Impossible } &#187; code</title>
	<atom:link href="http://codeimpossible.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeimpossible.com</link>
	<description>this = HowI.Roll();</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:58:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Comments stink!</title>
		<link>http://codeimpossible.com/2010/07/16/comments-stink/</link>
		<comments>http://codeimpossible.com/2010/07/16/comments-stink/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 05:28:26 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[opinionated]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=619</guid>
		<description><![CDATA[I&#8217;ve been thinking about the whole Comments-as-a-code-smell argument for a very long time. When I first heard this idea, I was in the comments are definitely needed side of the argument.
&#8220;Who could hate comments so much that they would label them as a code smell?&#8221; I thought.
Well, I had an epiphany the other day.
I&#8217;ve never [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been thinking about the whole <a href="http://memeagora.blogspot.com/2008/11/comments-code-smell.html" target="_blank">Comments-as-a-code-smell argument</a> for a very long time. When I first heard this idea, I was in the comments are definitely needed side of the argument.</p>
<p><em>&#8220;Who could hate comments so much that they would label them as a code smell?&#8221;</em> I thought.</p>
<p>Well, I had an epiphany the other day.</p>
<p><strong>I&#8217;ve never seen a block of code that actually needed a comment. Ever.</strong></p>
<p>If you have a section of code that feels too complex, instead of adding a comment do any/all of the following and you can, in almost every case, reduce the need for comments by 100%:</p>
<ol>
<li>add a sourcecontrol commit comment</li>
<li>do a simple refactor (<a href="http://www.industriallogic.com/xp/refactoring/composeMethod.html" target="_blank">Compose Method</a> ftw people),</li>
<li>use more descriptive variable/function/class/whatever names.</li>
</ol>
<p>If you do all of these things and the code you are working on still seems too complex, you may have bigger problems with your overall architecture/design and adding a comment isn&#8217;t going to help. In this case it&#8217;s time to go back to the drawing board.</p>
<p>Also, everytime you commit a changeset into sourcecontrol that contains a section of code commented out god kills a kitten. Please think of the kittens.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/07/16/comments-stink/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Migrating Asp.net MVC Projects from MVC 1.0 to 2.0</title>
		<link>http://codeimpossible.com/2010/04/06/migrating-asp-net-mvc-projects-from-mvc-1-0-to-2-0/</link>
		<comments>http://codeimpossible.com/2010/04/06/migrating-asp-net-mvc-projects-from-mvc-1-0-to-2-0/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 06:35:35 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=837</guid>
		<description><![CDATA[The Asp.net Mvc 2.0 RTM came out last month and a lot of people are converting their projects over. If you&#8217;re just starting to manually move your projects over then stop what you are doing, download and run the Mvc Converter. It will save you eons of time and frustration.
If you are like me, however, [...]]]></description>
			<content:encoded><![CDATA[<p>The<a href="http://haacked.com/archive/2010/03/11/aspnet-mvc2-released.aspx" target="_blank"> Asp.net Mvc 2.0 RTM came out last month</a> and a lot of people are converting their projects over. If you&#8217;re just starting to manually move your projects over then stop what you are doing, <a href="http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2-rtm.aspx" target="_blank">download and run the Mvc Converter</a>. It will save you eons of time and frustration.</p>
<p>If you are like me, however, and started porting your project over manually and are now knee deep in WTFBBQ sauce then follow the steps below and your project should be up and running in no time.</p>
<p>1. Back up your project. Just in case.</p>
<p>2. Open your project file(s) inside your favorite text editor (one with a decent find/replace system). Open the Find &amp; Replace dialog and find <code>"603c0e0b-db56-11dc-be95-000d561079b0"</code>, replacing it with <code>"F85E285D-A4E0-4152-9332-AB1D724D3325"</code>. My project turned up 1 result.</p>
<p>3. Open the Web.Config files in the root of the project and the root of the /Views folder. Open the Find &amp; Replace dialog again, this time searching for <code>"System.Web.Mvc, Version=1.0.0.0"</code> and replacing it with <code>"System.Web.Mvc, Version=2.0.0.0"</code>.</p>
<p>4. Add the following BindingRedirect to the bottom of the root Web.Config, just before the &lt;/Configuration&gt; node.</p>
<pre class="prettyprint"><code>
&lt;runtime&gt;
  &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
    &lt;dependentAssembly&gt;
      &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/&gt;
      &lt;bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/&gt;
    &lt;/dependentAssembly&gt;
  &lt;/assemblyBinding&gt;
&lt;/runtime&gt;
</code></pre>
<p>5. Open the solution in Visual Studio and replace the references to System.Web.Mvc 1.0 with the 2.0 assembly.<br />
6. Finally, and only if you really need them, open a new MVC 2.0 project and copy all the files in the /Scripts folder to your project. </p>
<p>Enjoy your freshly migrated project!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/04/06/migrating-asp-net-mvc-projects-from-mvc-1-0-to-2-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. #6 &#8211; enumerateOver()</title>
		<link>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/</link>
		<comments>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 04:17:52 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jsoq]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=478</guid>
		<description><![CDATA[
I&#8217;ve been focusing more and more on  my port of Ling-to-Objects, Jsoq the past few weeks. It&#8217;s still in really early stages and I&#8217;m not quite sure about it&#8217;s actual usefulness but I&#8217;m learning a lot about JavaScript and having a ton of fun along the way!
Jsoq deals with arrays a lot. About 95% [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://codeimpossible.com/wp-content/uploads/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p>I&#8217;ve been focusing more and more on <a href="http://bitbucket.org/codeimpossible/jsoq"> my port of Ling-to-Objects, Jsoq</a> the past few weeks. It&#8217;s still in really early stages and I&#8217;m not quite sure about it&#8217;s actual usefulness but I&#8217;m learning a lot about JavaScript and having a ton of fun along the way!</p>
<p>Jsoq deals with arrays a lot. About 95% of it&#8217;s use cases involve either looping through, altering, or creating arrays. Having a ton of <code>for</code> loops in my code just seems so&#8230; not right. For loops have always seemed dirty to me. They just aren&#8217;t elegant enough. </p>
<p>Here is your normal, average, everyday <code>for</code> loop in Javascript.</p>
<pre class="prettyprint"><code>
var array = "1,2,3,4,5,6,7,8,9,10".split(',');

for(var i = -1, l = array.length; ++i &lt; l;) {
    alert(array[i]);
}
</code></pre>
<p>This works. It&#8217;s reliable and gets the message across. But what if we needed to loop over an array and get all the items that matched a condition? Using a <code>for</code> loop the code could look something like:</p>
<pre class="prettyprint"><code>
var array = [ 1, 2, 3, 4, 5, 1, 2, 3, 4 ];
var results = [];
var condition = function(i) { return i%2 == 0; };

for(var itt = -1, len = array.length; ++itt &lt; len;) {
	if( condition(array[itt]) ) {
		results.push(array[itt]);
	}
}
</code></pre>
<p>This does work, I&#8217;ve written code like this many times before, and while <em>technically</em> there isn&#8217;t anything wrong with it I think there is still room for improvement.</p>
<p>Jsoq is going to be handling arrays all over the place so the solution to this problem <em>needs</em> to be simple.</p>
<p>Here&#8217;s what I need:</p>
<ul>
<li>to loop over an entire collection and perform an action on each item.</li>
<li>If that action produces a result, the item is to be pushed into an array and returned to after the loop is done</li>
<p>.</p>
<li>Also: it needs to be readable, someone else coming along should be able to determine what this thing is doing without too much difficulty. So I had my work cut out for me. </li>
</ul>
<p>A few hours later I had a decent function that I could use to replace a lot of the <code>for</code> loops I had. After some more refactoring I was able to wipe them all out and replace them with calls to <code>enumerateOver()</code>. Here is the latest version from source control:</p>
<pre class="prettyprint"><code>
function enumerateOver(collection, work) {
	var result = [], val = [];

	if (isArray(collection)) {
		try {
			for (var i = -1, l = collection.length; ++i < l;) {
				result = work(collection[i], i);
				if (typeof result !== "undefined" &#038;&#038; result != null) {
					val.push(result);
				}
			}
		}catch (e) {
			if (e != jsoq.$break) throw(e);
		}

		if (val.length > 0) {
			return val;
		}
	}else {
		try {
			val = work(collection, 0);
		}
		catch (e) {
			if (e != jsoq.$break) throw(e);
		}
		if (typeof val !== 'undefined') {
			return val;
		}
	}
	return result == null ? [] : result;
}
</code></pre>
<p>And here is the code to replace the <code>for</code> loops above, re-written to use <code>enumerateOver()</code></p>
<pre class="prettyprint"><code>
var results2 = enumerateOver(array, function(i, c) {
     return i%2 == 0;
});
</code></pre>
<p>So by implmenting this function I was able to come up with a more readable, testable and streamlined codebase. Is this suitable for everyone? Definitely not, but I did it for a few reasons:</p>
<ol>
<li>Like I mentioned before. I&#8217;ve never been at ease with <code>for</code> loops and being able to replace them all with calls to a single function was a huge win for me</li>
<li>The normal use-case didn&#8217;t fit right. I needed to not only iterate over arrays but also return the results of work performed on those items. Doing this the &#8220;regular&#8221; way just wouldn&#8217;t work (see previous reason)</li>
<li>I thought this was a fun problem to solve</li>
</ol>
<p>If you have any feedback, good, bad, or indifferent add a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Performance Re-match</title>
		<link>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/</link>
		<comments>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 06:07:37 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[google-chrome]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=703</guid>
		<description><![CDATA[Back in 2007 Jeff Atwood ran the top 4 web browsers through the SunSpider JavaScript Benchmark and posted his findings. It&#8217;s been close to 2 years since then and I was curious to see how FireFox 3.5, IE8, IE7 (in Compatibility Mode) and Google Chrome 4.0.249.11 would fair. 
So I did pretty much the same [...]]]></description>
			<content:encoded><![CDATA[<p>Back in 2007 <a href="http://codinghorror.com" title="Jeff Atwood's blog">Jeff Atwood</a> ran the top 4 web browsers through the <a href="http://www2.webkit.org/perf/sunspider-0.9/sunspider.html" title="SunSpider JavaScript Benchmark">SunSpider JavaScript Benchmark</a> and <a href="http://www.codinghorror.com/blog/archives/001023.html" title="The Great Browser JavaScript Showdown">posted his findings</a>. It&#8217;s been close to 2 years since then and I was curious to see how FireFox 3.5, IE8, IE7 (in Compatibility Mode) and Google Chrome 4.0.249.11 would fair. </p>
<p>So I did pretty much the same thing Jeff did and here is what I found:</p>
<p><img src="http://codeimpossible.com/wp-content/uploads/2009/12/JavaScript-Performance.png" alt="JavaScript Performance Graph" title="JavaScript Performance Graph" class="alignnone size-medium wp-image-704" /></p>
<p><small><em>* System specs: Windows 7 64-bit on a Dual-Core 2.53ghz CPU with 4gb of RAM with no browser extensions</em></small></p>
<ol>
<li>Chrome kicks some serious butt over everyone else with <strong>the entire test suite running to completion in under 1 second!!</strong></li>
<li>FireFox 3.5 has some serious improvements coming in at just over 1.5 seconds total, which is about 1/10 the time it took FireFox 2.0</li>
<li>Internet Explorer 8 and Internet Explorer 7 (compat mode) are still bringing up the rear but they had a better showing than the 20+ seconds IE7 took when Jeff ran the test</li>
</ol>
<p><strong>Surprises?</strong><br />
The only thing I found surprising about the results was that if you removed the string test from both IE runs then IE8 in compatibility mode beats IE8 running normally. That doesn&#8217;t seem right to me and I seriously hope JavaScript performance is something that gets addressed in IE9. And by &#8220;addressed&#8221; I mean &#8220;fixed by replacing Trident with WebKit&#8221;.</p>
<p>So, what do these results <em>actually</em> mean? Well I guess it depends. If you use your browser to read blogs and check your gmail then you shouldn&#8217;t really care about these numbers. However, if you&#8217;re a web developer you should be paying strong attention to these numbers and how far they&#8217;ve come in <em>just 2 years</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. Project Euler #3</title>
		<link>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/</link>
		<comments>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 06:25:57 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=666</guid>
		<description><![CDATA[
Here we are with another excellent installment in the &#8220;Works On My Machine&#8221; series where I post some code, some thoughts and hopefully show you something interesting/cool/new.
Today I&#8217;m going to talk briefly about Project Euler problem #3. Problem 3 asks you to find the largest prime factor of the number 600,851,475,143.
My solution is pretty simple [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://codeimpossible.com/wp-content/uploads/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p>Here we are with another excellent installment in the &#8220;Works On My Machine&#8221; series where I post some code, some thoughts and hopefully show you something interesting/cool/new.</p>
<p>Today I&#8217;m going to talk briefly about Project Euler problem #3. Problem 3 asks you to find the largest <a href="http://en.wikipedia.org/wiki/Prime_factor">prime factor</a> of the number 600,851,475,143.</p>
<p>My solution is pretty simple but it works:</p>
<pre class="prettyprint"><code>
var getLargestPrimeFactor = function(num)
{
    var factors = [];
    for ( var f = 2; f &lt; num; f++ )
    {
        if( num%f == 0 )
        {
            factors.push(f);
            num = num /f;
        }
    }
    factors.push(num);
    return factors[factors.length-1];
};

alert( getLargestPrimeFactor(600851475143));
</code></pre>
<p><a title="Try this code!" href="http://beta.jsvudo.com/76cc38" target="_blank">Try this code!</a></p>
<p>You may have noticed that there isn&#8217;t a check for a factors <del>optimus</del> primeness anywhere in there.</p>
<p>Thats because the <code>getLargestPrimeFactor()</code> function is dividing the number by it&#8217;s smallest factor, and the smallest factor for any number is always prime so when we run out of factors we&#8217;ve got the largest prime factor. Yeah, that&#8217;s pretth cool IMO.</p>
<p>I read the discussion threads for problem 3 after I solved it and I was surprised that hardly anyone removed the prime checks from their code.</p>
<p>So, problem 3 is in the bag and I feel pretty good about coming up with the prime check optimization (or lack thereof). A small bit of proof that I know what I&#8217;m doing. Sometimes :D</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. #5 Project Euler &#8211; Problem 2</title>
		<link>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/</link>
		<comments>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 05:49:51 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=639</guid>
		<description><![CDATA[
Project Euler is an awesome website that I found out about a while back via this question on StackOverflow. Basically, it&#8217;s a website that poses a ton of programmer related challenges and it keeps track of the problems that you solve.
The problems start out relatively easy (Add all the natural numbers below one thousand that [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://wpup.codeimpossible.com/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p><a href="http://projecteuler.net/">Project Euler</a> is an awesome website that I found out about a while back via <a href="http://stackoverflow.com/questions/4002/code-katas">this question on StackOverflow</a>. Basically, it&#8217;s a website that poses a ton of programmer related challenges and it keeps track of the problems that you solve.</p>
<p>The problems start out relatively easy (Add all the natural numbers below one thousand that are multiples of 3 or 5.) and progress all the way to insane (Caradano Triplets and Covex Holes).</p>
<p>I&#8217;m on the easy side, I just started working through them tonight and thought it would be cool to post how I build the solution to each problem as I complete them.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=2">Problem 2</a> is pretty easy:</p>
<p><i>&#8220;Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.&#8221;</i></p>
<p>So first things first, I&#8217;ll need to pick a language to use to solve this. I chose javascript simply because it was something I knew I could get running pretty quickly. </p>
<p>Next I&#8217;ll need some variables. (note: if you&#8217;re not familiar with Fibonacci numbers <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Wikipedia has an awesome article on them</a>.)</p>
<pre><code class="prettyprint">
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;
</code></pre>
<p>That should do. I&#8217;ve got my start variable (dropping the 0 in favor of more readable code), my end counter, and my &#8220;where-am-i&#8221; variables: current, previous, temp and my sum variable.</p>
<p>Obviously we&#8217;ll need some kind of loop. How about a while loop?</p>
<pre class="prettyprint"><code>
while( current <= end )
{

}
</code></pre>
<p>Cool. Now when the loop is running I'll need a way to keep track of the current value, the previous value and I need to add the current value to the sum. So....</p>
<pre class="prettyprint"><code>
while( current <= end )
{
    temp = current;
    current = current + previous;
    sum += current;
    previous = temp;
}
</code></pre>
<p>And there we hav... Oh Wait!</p>
<p>I overlooked a pretty important part of the problem description: <b><i>"sum of all the even-valued terms"</i></b>!!!</p>
<p>So given this new(ish) piece of info my solution end sup being:</p>
<pre class="prettyprint"><code>
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;

while( current <= end )
{
	temp = current;
	current = current + previous;

	if( current%2 == 0 )
		sum += current;

	previous = temp;
}

alert(sum);
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. #4 &#8211; Asp MVC Route Attributes</title>
		<link>http://codeimpossible.com/2009/06/22/w-o-m-m-4-asp-mvc-route-attributes/</link>
		<comments>http://codeimpossible.com/2009/06/22/w-o-m-m-4-asp-mvc-route-attributes/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 06:40:39 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=403</guid>
		<description><![CDATA[Download the source code mentioned in this blog post.

A few weeks ago on the StackOverflow podcast, something Jeff said got me thinking. Jeff was discussing how the stackoverflow team implemented their route mappings:

Those routes are&#8230; the way we implemented them are actually like decorators. Attributes on the methods. - Jeff Atwood (stackoverflow episode #54)

This instantly [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.box.net/shared/1q4bq5scuz" target="_blank">Download the source code mentioned in this blog post.</a></p>
<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://wpup.codeimpossible.com/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p>A few weeks ago <a href="http://itc.conversationsnetwork.org/audio/download/ITC.SO-Episode54-2009.05.20.mp3">on the StackOverflow podcast</a>, something Jeff said got me thinking. Jeff was discussing how the stackoverflow team implemented their route mappings:</p>
<p><em><br />
Those routes are&#8230; the way we implemented them are actually like decorators. Attributes on the methods. <cite>- Jeff Atwood (stackoverflow episode #54)</cite><br />
</em></p>
<p>This instantly piqued my interest and I completely zoned out for the rest of the podcast: caught up in working out the details of how I could do this for my own Asp.net MVC projects. </p>
<p>Coming up with the actual attribute code was easy; writing the code to set up all the Routes using only data defined in by the attribute was tricky.</p>
<p>Being new to attributes, and reflection in general, it took me a few hours until I had a very basic demo working. However, I was really starting to like where it was going.</p>
<p class="right side-note callout-green">
As a side note: There are lot of &#8220;helper&#8221; classes and objects in the route attribute project (it feels cluttered to me) and the reason I did this was to make the code in AssemblyExtensions.GetRoutes() easier to read.
</p>
<p>After a few nights of <a href="http://twitter.com/digitalBush/status/2121662803" target="_blank">Mtn Dew and convenience-store cherry-pie</a> I finished the rough code, tests and demo project (included in this blog post) and I was starting to realize that:</p>
<ol>
<li>Using the attributes is more declaritive and it feels cleaner</li>
<li>Having your route information right above your actions is incredibly useful</li>
<li>I had no more need to switch back and forth between your controller and the Global.asax.cs</li>
</ol>
<h2>How does it work?</h2>
<p>All of the real work for the RouteAttribute is done in the AssemblyExtensions class. This class uses extension methods to augment the System.Reflection.Assembly class with two methods: GetControllers() and GetRoutes().</p>
<p>GetRoutes is the only method that is used by other classes, I made GetControllers public for unit testing.</p>
<h3>GetRoutes()</h3>
<p>GetRoutes&#8217; first order of business is to make a list of data that it will need to build out all the routes for the assembly it was passed. After thats done GetRoutes will loop through the collected route data, build up each route and add it to the dictionary that will eventually be returned.</p>
<pre class="prettyprint"><code>
namespace CodeImpossible.Mvc.Routing
{
    public static class AssemblyExtensions
    {

        public static BindingFlags ActionFlags =
            BindingFlags.Instance |
            BindingFlags.Public |
            BindingFlags.DeclaredOnly;

        public static IList&lt;ControllerMetaData&gt; GetControllers(this Assembly assembly)
        {
            var controllers = assembly.GetTypes().ToList().FindAll(type =>
            {
                var isValidController = type.IsClass &#038;&#038;
                    type.IsPublic &#038;&#038;
                    type.IsSubclassOf&lt;Controller&gt;();

                var hasValidActions = type.GetMethods(ActionFlags).ToList().Any(m =>
                {
                    var valid = false;
                    if (m.ReturnParameter != null &#038;&#038; m.ReturnParameter.ParameterType == typeof(ActionResult))
                    {
                        valid = m.GetAttributesOfType&lt;RouteAttribute&gt;().Count > 0;
                    }

                    return valid;
                });

                return isValidController &#038;&#038; hasValidActions;
            }).Select&lt;Type, ControllerMetaData&gt;((t) => new ControllerMetaData(t)).ToList();

            return controllers;
        }

        public static IDictionary&lt;string, Route&gt; GetRoutes(this Assembly assembly)
        {
            var Routes      = new Dictionary&lt;string, Route&gt;();

            var data = (from c in assembly.GetControllers()
                        from a in c.GetActions()
                        from r in a.Data
                        select new
                        {
                            ControllerName = c.Name,
                            ActionName = a.Name,
                            RouteData = r,
                            RouteParams = a.Params
                        }).ToList();

            foreach (var r in data)
            {
                var route               = new Route(r.RouteData.RoutePath, new MvcRouteHandler());
                route.Constraints       = new RouteValueDictionary();
                route.Defaults          = new RouteValueDictionary(new {
                    controller = r.ControllerName,
                    action = r.ActionName
                });

                if (r.RouteData.RequireRouteParams &#038;&#038; r.RouteParams.Count() == 0)
                {
                    throw new MissingRouteParameterException("Unknown", r.RouteData.RoutePath);
                }

                var missingParams = new List&lt;ParameterMetaData&gt;();

                if (r.RouteData.RequireRouteParams)
                {
                    missingParams = (from p in r.RouteParams
                                     where r.RouteData.RoutePath.IndexOf("{" + p.Name + "}") == -1
                                     select p).ToList();
                }

                if (missingParams.Count > 0)
                {
                    var param = missingParams.First();
                    throw new MissingRouteParameterException(param.Name, r.RouteData.RoutePath);
                }

                foreach (var param in r.RouteParams)
                {
                    if (param.Data != null)
                    {
                        if (param.Data.DefaultValue != null)
                        {
                            route.Defaults.Add(param.Name, param.Data.DefaultValue);
                        }

                        if (param.Data.Constraint != null)
                        {
                            route.Constraints.Add(param.Name, param.Data.Constraint);
                        }
                    }
                }

                Routes.Add(r.RouteData.Name ?? r.RouteData.RoutePath, route);
            }

            return Routes;
        }
    }
}
</code></pre>
<h2>Getting the Routes into the RouteTable</h2>
<p>Slapping route attributes onto your classes and methods is all well and good but it doesn&#8217;t mean anything unless we can get those routes into the RouteTable object easliy. Originally I had the code to add the routes looking something like</p>
<pre class="prettyprint"><code>
var routes = Assembly.GetCurrentExecutingAssembly().GetRoutes();

routes.ForEach(r => RouteTable.Add(r));
</code></pre>
<p>This, although pretty easy, wasn&#8217;t as readible as I wanted. So I added some extension methods to the RouteTable class:</p>
<pre class="prettyprint"><code>
RouteTable.Routes.IncludeRoutesFromAssembly();
</code></pre>
<p>I think both of these are much clearer than doing:</p>
<pre class="prettyprint"><code>
RouteTable.Routes.MapRoute("Root",
    "",
    new { controller = "Test", action = "GetItem", id = 1 });

RouteTable.Routes.MapRoute("Search",
    "Search/{id}",
    new { controller = "Test", action = "Search", id = 1 });
//.. SNIP ...
</code></pre>
<h2>Using the RouteAttribute and RouteParamAttribute</h2>
<p>In the controller &#8220;TestController&#8221; below there are three actions: Index, FindByText, and GetItem. Using the RouteAttribute and RouteParamAttribute makes it pretty clear that the routes for FindByText and GetItem are the same but use different RouteContraints. </p>
<p>So a request for /Test/Search/Hello will go to FindByText while /Test/Search/1 will go to GetItem. Also notice how GetItem has a default value of 2 for the id argument.</p>
<pre class="prettyprint"><code>
public class TestController : Controller
{

    [Route(RoutePath = "Test")]
    public ActionResult Index()
    {

        return View();
    }

    [Route(RoutePath = "Test/Search/{query}")]
    public ActionResult FindByText(
        [RouteParam(Constraint="[a-zA-Z]{1,}")]
        string query)
    {

        return View();
    }

    [Route(RoutePath = "Test/Search/{id}")]
    public virtual ActionResult GetItem(
        [RouteParam(Constraint=@"\d{1,}", DefaultValue=2)]
        int id)
    {

        return View();
    }
}</code></pre>
<p>There is support for binding multiple routes to the same action; just add another Route attribute:</p>
<pre class="prettyprint"><code>
[Route("Products/Search/{id}")]
[Route("Products/{id}")]
public ActionResult GetProductById(int id)
{
    return View();
}
</code></pre>
<h2>Downsides or things I haven&#8217;t gotten to yet</h2>
<p>Just some gotchas that I think people might raise issue with.</p>
<p><strong>All of your controllers must inherit from the System.Web.Mvc.Controller class</strong><br />
This isn&#8217;t really a big deal because if you are using Asp.net MVC then you really should inherit from the Controller class, but for those of you using FubuMVC or another MVC framework this should be easy to change.</p>
<p><strong>Attributes can be ugly</strong><br />
I know a few people out there are against attributes but I think that this is a more than acceptable use because it made the code much easier to understand.</p>
<p><strong>Reflection can be slow</strong><br />
Honestly, when I first started working on this demo I was sort of turned off by the use of Reflection myself. After weighing the possible performance loss against the gains in both readability and maintenance I decided this was definitely worth it. I haven&#8217;t performance tested this code so, as always YMMV.</p>
<p>As always, if I screwed up or there is a better way to do this, please let me know in the comments.</p>
<p><a href="http://www.box.net/shared/1q4bq5scuz" target="_blank">Download the source code mentioned in this blog post.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/06/22/w-o-m-m-4-asp-mvc-route-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://itc.conversationsnetwork.org/audio/download/ITC.SO-Episode54-2009.05.20.mp3" length="33894437" type="audio/mpeg" />
		</item>
		<item>
		<title>W.O.M.M. weekly post #2</title>
		<link>http://codeimpossible.com/2009/04/10/womm-weekly-post-2/</link>
		<comments>http://codeimpossible.com/2009/04/10/womm-weekly-post-2/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 04:37:19 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=364</guid>
		<description><![CDATA[Due to me being insanely sick this week I&#8217;m going to be changing up the format for the Works On My Machine weekly project post for this week.
Instead of presenting a project that I&#8217;ve worked on I&#8217;d like to highlight some .Net OSS projects / source code that I&#8217;ve been working with / looking at [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://wpup.codeimpossible.com/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" />Due to me being insanely sick this week I&#8217;m going to be changing up the format for the Works On My Machine weekly project post for this week.</p>
<p>Instead of presenting a project that I&#8217;ve worked on I&#8217;d like to highlight some .Net OSS projects / source code that I&#8217;ve been working with / looking at lately.</p>
<p> </p>
<ul>
<li><a href="http://code.google.com/p/morelinq/" target="_blank">The More Linq project</a> &#8211; An extension library for LinqToObjects run by Jon Skeet.</li>
<li><a href="http://www.musikcube.com/" target="_blank">MusikCube</a> &#8211; Awesome music player that uses SqlLite to allow &#8220;smart playlists&#8221; (eg. show me all songs that were added this week that have fewer than 4 stars and have been played less than 10 times).</li>
<li><a href="http://code.google.com/p/moq/" target="_blank">Moq</a> &#8211; IMHO the best mocking framework for .net development.</li>
<li>Scott Hanselman posted some code on his website a while back about <a href="http://www.hanselman.com/blog/TheWeeklySourceCode35ZipCompressingASPNETSessionAndCacheState.aspx" target="_blank">how to Zip Compress your Session and Cache data</a> in Asp.net.</li>
</ul>
<p>Feel free to send me any code that you come across and I&#8217;ll feature it in a future post.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/04/10/womm-weekly-post-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
