<?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>Phil Leggetter - Real-Time Web Software and Technology Evangelist &#187; Agile</title>
	<atom:link href="http://www.leggetter.co.uk/tag/agile/feed" rel="self" type="application/rss+xml" />
	<link>http://www.leggetter.co.uk</link>
	<description>Real-Time Web, Real-Time Data and Social Media Software and Technology Evangelist and Consultant</description>
	<lastBuildDate>Sun, 29 Jan 2012 05:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How I approach problem solving in code?</title>
		<link>http://www.leggetter.co.uk/2009/10/23/how-i-approach-problem-solving-in-code.html</link>
		<comments>http://www.leggetter.co.uk/2009/10/23/how-i-approach-problem-solving-in-code.html#comments</comments>
		<pubDate>Fri, 23 Oct 2009 17:44:57 +0000</pubDate>
		<dc:creator>Phil Leggetter</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[problem solving]]></category>

		<guid isPermaLink="false">http://www.leggetter.co.uk/?p=382</guid>
		<description><![CDATA[<p>Recently I was posed the following question:</p> <p>Write a piece of code that prints all odd integer numbers between 1 and 99</p> <p>This really isn&#8217;t a difficult question but it still requires some thought. When I&#8217;m posed with any question I like to break things down into their constituent parts.</p> <p>Here&#8217;s the process I went [...]
Related posts:<ol>
<li><a href='http://www.leggetter.co.uk/2008/12/10/problem-solving-lessons-relearnt.html' rel='bookmark' title='Problem solving lessons relearnt'>Problem solving lessons relearnt</a></li>
<li><a href='http://www.leggetter.co.uk/2011/06/02/collecta-gets-dispensed-was-it-solving-a-hard-enough-problem.html' rel='bookmark' title='Collecta Gets Dispensed: Was It Solving a Hard Enough Problem?'>Collecta Gets Dispensed: Was It Solving a Hard Enough Problem?</a></li>
<li><a href='http://www.leggetter.co.uk/2011/06/28/recent-article-in-net-magazine-websockets-code-a-real-time-survey.html' rel='bookmark' title='Recent article in .net magazine: WebSockets &#8211; Code a real-time survey'>Recent article in .net magazine: WebSockets &#8211; Code a real-time survey</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Recently I was posed the following question:</p>
<blockquote><p>Write a piece of code that prints all odd integer numbers between 1 and 99</p></blockquote>
<p>This really isn&#8217;t a difficult question but it still requires some thought. When I&#8217;m posed with any question I like to break things down into their constituent parts.</p>
<p>Here&#8217;s the process I went through:</p>
<p>Okay, so I&#8217;ll define two variables for a start and end value and there&#8217;s going to have to be a loop.</p>
<pre class="brush: csharp; title: ; notranslate">int startValue = 1;
int endValue = 99;
for(int i = startValue;
     i &amp;lt;= endValue;
     i++)
{
   // work out if &quot;i&quot; is an odd number
}</pre>
<p>Now, for the odd number detection. And&#8230; after a few umms and errrs &#8230; I&#8217;m going to have to mod 2 (<code>%2</code>) the current value of <code>i</code> to work out if the value is odd. More &#8230; umms and errs. Okay, I&#8217;ve finally worked out that if something mod 2 is not equal to 0 it&#8217;s clearly an odd number. This took me longer than it should have but never mind. Once I&#8217;ve detected if <code>i</code> is an odd number I&#8217;ll then put the odd number into a list for use later.</p>
<pre class="brush: csharp; title: ; notranslate">
int startValue = 1;
int endValue = 99;
IList&lt;int&gt; oddValues = new List&amp;lt;int&amp;gt;();
for(int i = startValue;
     i &amp;lt;= endValue;
     i++)
{
   if(i%2 != 0)
   {
      oddValues.Add(i);
   }
}
</pre>
<p>Those of you that are good at these little puzzles, or just think this is way too easy, might already be screaming at me about one of the following:</p>
<ul>
<li>Why are you using a <code>IList<int></code>, why don&#8217;t you just print the value?</li>
<li>Odd numbers are always 2 apart so why aren&#8217;t you just increment <code>i</code> by 2 using <code>i+=2</code>?</li>
</ul>
<p><span id="more-382"></span><br />
I admit it,  I missed the second point and that is a bit silly of me. However, what I have starting doing is <a  title="separation of concerns" href="http://en.wikipedia.org/wiki/Separation_of_concerns">separating the concerns</a> of the piece of code. The code does two things; it detects the odd number and it prints the odd numbers. Those are two very distinct things. So, my code now looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">
int startValue = 1;
int endValue = 99;
IList&lt;int&gt; oddNumbers = new List&amp;lt;int&amp;gt;();
for(int i = startValue;
     i &amp;lt;= endValue;
     i++)
{
   if(i%2 != 0)
   {
      oddNumbers.Add(i);
   }
}

string oddNumbersList = string.Join(&quot;,&quot;, oddNumbers.ToArray());
Console.WriteLine(oddNumbersList );
</pre>
<p>I&#8217;m now going to refactor this further so I&#8217;ll put the two different pieces of functionality into different methods. I&#8217;ll rename the <code>i</code>, <code>startValue</code> and <code>endValue</code> variables to be something a bit more useful; say <code>numberToCheck</code>, <code>startNumber</code> and <code>endNumber</code>. I&#8217;ll also create another helper for the odd number checking named <code>IsOddNumber</code>:</p>
<pre class="brush: csharp; title: ; notranslate">
IList&lt;int&gt; GetOddNumbersBetween(int startNumber, int endNumber)
{
   IList oddValues = new List&amp;lt;int&amp;gt;();
   for(int numberToCheck = startNumber;
        numberToCheck &amp;lt;= endNumber;
        numberToCheck++)
   {
      if(IsOddNumber(numberToCheck) == true)
      {
         oddValues.Add(numberToChecki);
      }
   }
   return oddValues;
}

bool IsOddNumber(int number)
{
   return (number % 2 == 1);
}

void PrintOddNumbersBetween(int startNumber, int endNumber)
{
   IList&lt;int&gt; oddNumbers = GetOddNumbersBetween(startNumber, endNumber);
   string oddNumbersList = string.Join(&quot;,&quot;, oddNumbers.ToArray());
   Console.WriteLine(oddNumbersList);
}
</pre>
<p>Let&#8217;s say I then notice the second point you&#8217;ve been screaming at me about (Odd numbers are always 2 apart so why aren&#8217;t you just increment i by 2 using i+=2) that I mentioned above? In practice I should notice this sort of thing either when I give the code a complete review, or one of my peers spots it. When I see this problem I decide to update the <code>for</code> loop, as noted, and I then see that I possibly don&#8217;t need the <code>if(IsOddNumber(i) == true)</code> statement. Although it would pain me to do this, since it&#8217;s a lovely little method, I would need to consider deleting it. But then it strikes me, I&#8217;m no longer just solving the &#8220;odd numbers between 1 and 99 problem&#8221; so I can&#8217;t just assume that the <code>startNumber</code> is going to be an odd number. I need to make sure that it&#8217;s an odd number so I&#8217;ll create another small utility method for that called <code>EnsureOddNumber</code> which will check if the value passed is an odd number, and if not return the next odd number (I&#8217;d like to rethink the name of this method).</p>
<pre class="brush: csharp; title: ; notranslate">
IList&lt;int&gt; GetOddNumbersBetween(int startNumber, int endNumber)
{
   startNumber = EnsureOddNumber(startNumber);

   IList&lt;int&gt; oddValues = new List&amp;lt;int&amp;gt;();
   for(int numberToCheck = startNumber;
        numberToCheck &amp;lt;= endNumber;
        numberToCheck+=2)
   {
      oddValues.Add(numberToCheck);
   }
   return oddValues;
}

int EnsureOddNumber(int number)
{
   if( IsOddNumber(startNumber) == false )
   {
      startNumber++;
   }
   return startNumber;
}

bool IsOddNumber(int number)
{
   return (number % 2 == 1);
}

void PrintOddNumbersBetween(int startNumber, int endNumber)
{
   IList&lt;int&gt; oddNumbers = GetOddNumbersBetween(startNumber, endNumber);
   string oddNumbersList = string.Join(&quot;,&quot;, oddNumbers.ToArray());
   Console.WriteLine(oddNumbersList);
}
</pre>
<p>Now, when I look at this code I get a warm feeling because I feel that it solves the problem, it&#8217;s well engineered, the concerns are separated and the code is completely <a  href="http://en.wikipedia.org/wiki/Self-documenting">self documenting</a>.</p>
<p>There are a few comments that people may have here:</p>
<blockquote><p>The question asked specifically to print odd values between 1 and 99 and you&#8217;ve done more than was required.</p></blockquote>
<p>Although my answer does satisfy the original question have I over engineered things? The question does specifically ask us to print odd values between 1 and 99 so maybe I should have created a function that just satisfied that requirement.</p>
<pre class="brush: csharp; title: ; notranslate">
void PrintOddNumbersBetween1And99()
{
   for(int i = 1;
        i &amp;lt;= 99;
        i+=2)
   {
      Console.WriteLine(i + &quot; &quot;);
   }
}
</pre>
<p>And I&#8217;d have to admit that this very short piece of code exactly answers the question. But I&#8217;d also argue that there is very little chance of this code being reused. Don&#8217;t get me wrong, you definitely shouldn&#8217;t over engineer things but there should be some scope for <a  href="http://en.wikipedia.org/wiki/Code_reuse">code reuse</a>.</p>
<blockquote><p>For such a simple problem you&#8217;ve over engineered this.</p></blockquote>
<p>or</p>
<blockquote><p>There&#8217;s a better solutions that that&#8230; it&#8217;s not efficient</p></blockquote>
<p>Is creating four methods over engieering? Does my code require any comments to provide documentation? There may well be a more performant solution to this, but that&#8217;s not my point. My point is the way of approaching a question: the thought processes involved in understanding the problem and breaking it down to <a  href="http://en.wikipedia.org/wiki/Separation_of_concerns">separate concerns</a>, making it easy to read, <a  href="http://en.wikipedia.org/wiki/Code_reuse">reusable</a> and <a  href="http://en.wikipedia.org/wiki/Self-documenting">self documenting</a>. If the code that worked out the odd numbers is not efficient it could easily be changed in one place without impacting the interface, the other methods within the class, or the overall functionality.</p>
<p>Some people may jump to the simplest solution but I think the way i&#8217;ve described approaching and solving the problem demonstrates good practice. If I&#8217;m completely honest I would normally approach the development of something such as this by writing a test case first since I practice <a  href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> but that can wait for another blog post.</p>
<p>Related posts:<ol>
<li><a href='http://www.leggetter.co.uk/2008/12/10/problem-solving-lessons-relearnt.html' rel='bookmark' title='Problem solving lessons relearnt'>Problem solving lessons relearnt</a></li>
<li><a href='http://www.leggetter.co.uk/2011/06/02/collecta-gets-dispensed-was-it-solving-a-hard-enough-problem.html' rel='bookmark' title='Collecta Gets Dispensed: Was It Solving a Hard Enough Problem?'>Collecta Gets Dispensed: Was It Solving a Hard Enough Problem?</a></li>
<li><a href='http://www.leggetter.co.uk/2011/06/28/recent-article-in-net-magazine-websockets-code-a-real-time-survey.html' rel='bookmark' title='Recent article in .net magazine: WebSockets &#8211; Code a real-time survey'>Recent article in .net magazine: WebSockets &#8211; Code a real-time survey</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.leggetter.co.uk/2009/10/23/how-i-approach-problem-solving-in-code.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 24/42 queries in 0.041 seconds using disk: basic

Served from: www.leggetter.co.uk @ 2012-02-04 21:18:12 -->
