<?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>.Net Smoothie &#187; Fibonacci Sequence</title>
	<atom:link href="http://www.richardbushnell.net/tag/fibonacci-sequence/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +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>Calculating the Fibonacci Sequence with C# 3.0</title>
		<link>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</link>
		<comments>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 09:36:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Fibonacci Sequence]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</guid>
		<description><![CDATA[Scott Hanselman just posted his latest article in his weekly source code series. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons: I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.hanselman.com/blog" target="_blank">Scott Hanselman</a> just posted his <a href="http://www.hanselman.com/blog/TheWeeklySourceCode13FibonacciEdition.aspx" target="_blank">latest article in his weekly source code series</a>. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons:</p>
<ol>
<li>I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it out. (Doh!) </li>
<li>It makes me question what I thought about coding. </li>
</ol>
<p>You see, I spend a lot of time refactoring. Sometimes too much. I have to ask myself why. It&#8217;s usually so that I can come back to the code later, and still understand what I was trying to do. <em>Most</em> of the time, <em>conciser is better. </em>But not always!</p>
<p>And that&#8217;s what I see in this post.</p>
<p><span id="more-38"></span></p>
<p>Suppose I had written this C# function (shamelessly stolen from Scott&#8217;s post):</p>
<pre class="code"><span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; fib = <span style="color: blue">null</span>;
fib = n =&gt; n &gt; 1 ? fib(n - 1) + fib(n - 2) : n;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Compare this to the C# 2.0 version:</p>
<pre class="code"><span style="color: blue">static int </span>Fibonacci(<span style="color: blue">int </span>x) {
  <span style="color: blue">if </span>(x &lt;= 1)
    <span style="color: blue">return </span>1;
  <span style="color: blue">return </span>Fibonacci(x - 1) + Fibonacci(x - 2);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Apart from the fact that these methods actually give different answers (sorry, Scott), if I were to try to understand these methods 6 months after writing them, which one of the code samples would I be most glad of seeing again?</p>
<p>You see, most code only gets read again for two reasons:</p>
<ol>
<li>to debug it, or </li>
<li>to hand it over to someone else </li>
</ol>
<p>In both cases, clever though it may seem, the first conciser version would probably cause more trouble. Now, don&#8217;t get me wrong, I&#8217;m all for good concise code, but XSLT is concise, and I hate debugging that.</p>
<p>Now, considering that in order to reuse the <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; function, I would have to pass the around, thus holding it in a static variable, or wrapping it in another class, it doesn&#8217;t actually turn out to be that practical after all. Concise, yes. Practical and readable, hmm.</p>
<p>So, while we have a very-much-by-Ruby-On-Rails-driven trend going to make code as beautiful as possible, don&#8217;t forget to not go too far. We have to be able to read code later on, remember.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

