<?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>Coding In Caledon</title>
	<atom:link href="http://codingincaledon.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingincaledon.wordpress.com</link>
	<description>Volume I</description>
	<lastBuildDate>Tue, 26 Oct 2010 21:01:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codingincaledon.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Coding In Caledon</title>
		<link>http://codingincaledon.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codingincaledon.wordpress.com/osd.xml" title="Coding In Caledon" />
	<atom:link rel='hub' href='http://codingincaledon.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Allowing Power Use</title>
		<link>http://codingincaledon.wordpress.com/2010/10/20/allowing-power-use/</link>
		<comments>http://codingincaledon.wordpress.com/2010/10/20/allowing-power-use/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 20:49:09 +0000</pubDate>
		<dc:creator>Cliff Mees</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[UX]]></category>
		<category><![CDATA[UI-design]]></category>

		<guid isPermaLink="false">http://codingincaledon.wordpress.com/?p=53</guid>
		<description><![CDATA[In a recent project, I was implementing a feature that allowed users to add items to an order.  My initial approach was to provide an input for the users which allowed them to specify the ID of the item (domain &#8230; <a href="http://codingincaledon.wordpress.com/2010/10/20/allowing-power-use/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=53&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a recent project, I was implementing a feature that allowed users to add items to an order.  My initial approach was to provide an input for the users which allowed them to specify the ID of the item (domain experts know item IDs), and to then fetch the required data to generate the fields required for the item.  Nothing really too special here.  If the user entered an item ID that did not exist, a prompt would allow them to create it inline; otherwise, if the item was de-listed, a corresponding message would be shown.</p>
<p>After discussing the first implementation with the users, the request was made to allow multiple IDs to be entered at once.  Initially, I was worried that the validation for such entries might prove fairly onerous.  The next implementation allowed for multiple entries separated by commas.  The following shows a screen shot of the relevant prompt with some requested items.  Note that one of the requested item IDs refers to an item that is de-listed, and another to one that does not exist (some &#8220;fat-fingered&#8221; commas are thrown in for good measure).  An already-existing item is shown for context.</p>
<div id="attachment_60" class="wp-caption aligncenter" style="width: 650px"><a href="http://codingincaledon.files.wordpress.com/2010/10/figure12.png"><img class="size-full wp-image-60" title="Figure1" src="http://codingincaledon.files.wordpress.com/2010/10/figure12.png?w=640&#038;h=140" alt="Figure 1" width="640" height="140" /></a><p class="wp-caption-text">Requesting multiple items at once -- some are not valid.</p></div>
<p>The results of the requested items are shown next:</p>
<div id="attachment_64" class="wp-caption aligncenter" style="width: 650px"><a href="http://codingincaledon.files.wordpress.com/2010/10/figure21.png"><img class="size-full wp-image-64" title="Figure2" src="http://codingincaledon.files.wordpress.com/2010/10/figure21.png?w=640&#038;h=324" alt="Figure 2" width="640" height="324" /></a><p class="wp-caption-text">Requested items listed after request for multiple IDs returns.</p></div>
<p>The two existing items are added, the item that does not exist is described as such with a prompt to allow creating it inline, and the invalid item is also specified as such.</p>
<p>The utility of this approach was quite dramatic.  A significant number of individual retrievals requiring separate inputting by the user along with separate calls to the server were collapsed into a single input (possibly a paste from an existing list elsewhere) and a single server request.  Typos would potentially show themselves as new item requests, and these could be safely ignored if required.  The single ID entry approach was not lost with this option either, thus providing trainees or infrequent users with a continued straight-forward process.</p>
<p>An alternate approach to this might have been to provide some form of type-ahead search (à la Google search).  Although possibly easing the requirement of &#8220;knowing the IDs&#8221;, this would have restricted the work-flow to retrieving a single item at a time (or, at a minimum required individual server calls to handle the type-ahead resolution).</p>
<p>Domain experts tend to spend a lot of time working in their applications, and get very good at using them.  Catering to their domain expertise in the features we provide helps to promote their power use.</p>
<p><span style="font-size:smaller;">Copyright © Cliff Mees 2010; all rights reserved.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingincaledon.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingincaledon.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingincaledon.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=53&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingincaledon.wordpress.com/2010/10/20/allowing-power-use/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31ded04f26fc8edba3052934455b78af?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cliffjmees</media:title>
		</media:content>

		<media:content url="http://codingincaledon.files.wordpress.com/2010/10/figure12.png" medium="image">
			<media:title type="html">Figure1</media:title>
		</media:content>

		<media:content url="http://codingincaledon.files.wordpress.com/2010/10/figure21.png" medium="image">
			<media:title type="html">Figure2</media:title>
		</media:content>
	</item>
		<item>
		<title>Milestones along my JavaScript path</title>
		<link>http://codingincaledon.wordpress.com/2010/09/30/milestones-along-my-javascript-path/</link>
		<comments>http://codingincaledon.wordpress.com/2010/09/30/milestones-along-my-javascript-path/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 20:29:56 +0000</pubDate>
		<dc:creator>Cliff Mees</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://codingincaledon.wordpress.com/?p=47</guid>
		<description><![CDATA[I&#8217;ve gotten around to start reading Douglas Crockford&#8216;s &#8220;JavaScript: The Good Parts&#8220;.  As I read, it occurs to me that my experience in using JavaScript over the years has been shaped by several key influences: 1. Microsoft Remote Scripting and &#8230; <a href="http://codingincaledon.wordpress.com/2010/09/30/milestones-along-my-javascript-path/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=47&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve gotten around to start reading <a href="http://www.crockford.com">Douglas Crockford</a>&#8216;s &#8220;<a href="http://www.amazon.ca/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=sr_1_1?s=gateway&amp;ie=UTF8&amp;qid=1285876095&amp;sr=8-1">JavaScript: The Good Parts</a>&#8220;.  As I read, it occurs to me that my experience in using JavaScript over the years has been shaped by several key influences:</p>
<p>1. <a href="http://msdn.microsoft.com/en-us/library/ms974566.aspx">Microsoft Remote Scripting</a> and <a href="http://www.ashleyit.com/rs/">JavaScript Remote Scripting</a>.  Introduced me to the remote procedure call approach; mostly to classic ASP JScript functions.  This transitioned naturally to Ajax later on.  Not really a JS language aspect, but important nonetheless&#8230;</p>
<p>2. Florian Gross <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117184">implements Ruby-esque features</a> in JavaScript.  I was investigating Ruby, and was delighted to be able to use these approaches in browser code!  It also led me to realize that there was more to JavaScript than I initially thought.</p>
<p>3. Daniel Brockman&#8217;s paper: <a href="http://brockman.se/2004/method-references/">Object-Oriented Event Listening through Partial Application in JavaScript</a>.  I came across this while initially looking at <a href="http://prototypejs.org">Prototype</a>.  The paper introduced closures in JavaScript to me and how they could be used.</p>
<p>4. <a href="http://prototypejs.org">Prototype</a> &#8211; the first of the modern cross-browser libraries I used with many features of 3 and 4 (above) applied.  I wrote a fair amount of JS that emulated its style.</p>
<p>Lately, I&#8217;m using JQuery and enjoying it.</p>
<p>Now, where will my next key influencing driver show up?</p>
<p><span style="font-size:smaller;">Copyright © Cliff Mees 2010; all rights reserved.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingincaledon.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingincaledon.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingincaledon.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=47&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingincaledon.wordpress.com/2010/09/30/milestones-along-my-javascript-path/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31ded04f26fc8edba3052934455b78af?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cliffjmees</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Koans</title>
		<link>http://codingincaledon.wordpress.com/2010/09/20/ruby-koans/</link>
		<comments>http://codingincaledon.wordpress.com/2010/09/20/ruby-koans/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 15:35:32 +0000</pubDate>
		<dc:creator>Cliff Mees</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://codingincaledon.wordpress.com/?p=38</guid>
		<description><![CDATA[A while ago, I stumbled across the &#8220;Learning Ruby&#8221; site at http://rubykoans.com. The authors Jim Weirich [@jimweirich] and Joe O&#8217;Brien [@objo] provide a set of programming exercises that serve to help teach (or refresh your knowledge of) programming in Ruby. &#8230; <a href="http://codingincaledon.wordpress.com/2010/09/20/ruby-koans/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=38&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while ago, I stumbled across the &#8220;Learning Ruby&#8221; site at <a href="http://rubykoans.com">http://rubykoans.com</a>.  The authors Jim Weirich [<a href="http://twitter.com/@jimweirich">@jimweirich</a>] and Joe O&#8217;Brien [<a href="http://twitter.com/objo">@objo</a>] provide a set of programming exercises that serve to help teach (or refresh your knowledge of) programming in Ruby.  The exercises are presented as koans; i.e. questions that you answer with Ruby code to &#8220;<em>&#8230;walk you along the path to enlightenment</em>&#8220;.</p>
<p>Each question is in fact a failing test that focuses on some aspect of Ruby.  In fact, the emphasis on testing is such that the first koan introduces asserting.</p>
<p>Occasionally, the koans are broken out into a slightly more substantial &#8220;project&#8221; exercise.  One of these: <em>about_scoring_project.rb</em> requires you to write a method to determine the score of a simplified version of the <a href="http://en.wikipedia.org/wiki/Greed_%28dice_game%29">Greed</a> dice game.  As with the other koans, a set of failing tests is provided.</p>
<p>For context, here is the comment section of <em>about_scoring_project.rb</em> that provides the scoring rules:</p>
<p><pre class="brush: ruby;">
# Greed is a dice game where you roll up to five dice to accumulate
# points.  The following &quot;score&quot; function will be used calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
#   number. (e.g. three fives is 500 points).
#
# * A one (that is not part of a set of three) is worth 100 points.
#
# * A five (that is not part of a set of three) is worth 50 points.
#
# * Everything else is worth 0 points.
#
#
# Examples:
#
# score([1,1,1,5,1]) =&gt; 1150 points
# score([2,3,4,6,2]) =&gt; 0 points
# score([3,4,5,3,3]) =&gt; 350 points
# score([1,5,1,2,4]) =&gt; 250 points
#
# More scoring examples are given in the tests below:
#
# Your goal is to write the score method.
</pre></p>
<p>My initial implementation used a hash to track the count of each die result.  It worked (i.e. passed all the tests), but seemed to be somewhat clunky.  Here it is:</p>
<p><pre class="brush: ruby;">
def score(dice)

  dice_count = {:&quot;1&quot; =&gt; 0, :&quot;2&quot; =&gt; 0, :&quot;3&quot; =&gt; 0, :&quot;4&quot; =&gt; 0, :&quot;5&quot; =&gt; 0, :&quot;6&quot; =&gt; 0}

  for die in dice
    dice_count[die.to_s.to_sym] += 1
  end

  score = 0

  # * A set of three ones is 1000 points
  if dice_count[:&quot;1&quot;].between?(3,5)
    score += 1000
  elsif dice_count[:&quot;1&quot;] == 6
    score += 2000
  end

  # * A set of three numbers (other than ones) is worth 100 times the
  #   number. (e.g. three fives is 500 points).
  (2..6).each do |non_one|
    if dice_count[non_one.to_s.to_sym].between?(3,5)
      score += (non_one * 100)
    elsif dice_count[non_one.to_s.to_sym] == 6
      score += 2 * (non_one * 100)
    end
  end

  number_not_in_set_of_three = lambda do |num|
    key = num.to_s.to_sym
    if [1,2].include? dice_count[key]
      return dice_count[key]
    elsif [4,5].include? dice_count[key]
      return dice_count[key]-3
    else
      return 0
    end
  end

  # * A one (that is not part of a set of three) is worth 100 points.
  score += (number_not_in_set_of_three.call(1) * 100)

  # * A five (that is not part of a set of three) is worth 50 points.
  score += (number_not_in_set_of_three.call(5) * 50)

end
</pre></p>
<p>I didn&#8217;t really like how checks for second triples, or numbers not included in a triple seemed to require near-duplicate code (i.e. all those <em>elsif </em>statements).</p>
<p>I ditched the hash of counts approach and came up with:</p>
<p><pre class="brush: ruby;">
def score(dice)

  score = 0
  triples = dice.sort.each_slice(3).map(&amp;:join)

  # * A set of three ones is 1000 points
  score = triples.grep(&quot;111&quot;).count * 1000

  # * A set of three numbers (other than ones) is worth 100 times the
  #   number. (e.g. three fives is 500 points).
  (2..6).each do |num|
    score += triples.grep([num,num,num].join).count * num * 100
  end

  # * A one (that is not part of a set of three) is worth 100 points.
  # * A five (that is not part of a set of three) is worth 50 points.
  {1=&gt;100,5=&gt;50}.each do |num,multiplier|
    score += dice.grep(num).count % 3 * multiplier
  end

  return score

end
</pre></p>
<p>That&#8217;s a lot more terse, and the near-duplications have been cleaned up.  I&#8217;m still not too pleased with the <em>[num,num,num].join</em> bit, but couldn&#8217;t think of a more elegant but short way to achieve the same thing.</p>
<p>The more terse things get, however, the more I find it is necessary to have comments as the intent of the code becomes more obscured.</p>
<p>RubyKoans was a lot of fun.  I encourage you to give them a try!</p>
<p><span style="font-size:smaller;">Copyright © Cliff Mees 2010; all rights reserved.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingincaledon.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingincaledon.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingincaledon.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=38&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingincaledon.wordpress.com/2010/09/20/ruby-koans/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31ded04f26fc8edba3052934455b78af?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cliffjmees</media:title>
		</media:content>
	</item>
		<item>
		<title>Clean Code Experience</title>
		<link>http://codingincaledon.wordpress.com/2010/09/16/clean-code-experience/</link>
		<comments>http://codingincaledon.wordpress.com/2010/09/16/clean-code-experience/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 18:00:17 +0000</pubDate>
		<dc:creator>Cliff Mees</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[clean-code]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://codingincaledon.wordpress.com/?p=8</guid>
		<description><![CDATA[A couple days ago, John MacIntyre [@JohnMacIntyre] posted an article about refactoring a small wage calculator function.  This is John&#8217;s second post in a series dealing with his reactions to Robert C. Martin&#8216;s book Clean Code.  First of all, thanks &#8230; <a href="http://codingincaledon.wordpress.com/2010/09/16/clean-code-experience/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=8&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A couple days ago, <a title="John MacIntyre" href="http://whileicompile.wordpress.com">John MacIntyre</a> [<a href="http://twitter.com/JohnMacIntyre">@JohnMacIntyre</a>] posted an <a title="What is to simple and small to refactor?" href="http://whileicompile.wordpress.com/2010/09/14/what-is-too-simple-and-small-to-refactor-as-clean-code">article</a> about refactoring a small wage calculator function.  This is John&#8217;s second post in a series dealing with his reactions to <a href="http://objectmentor.com/omTeam/martin_r.html">Robert C. Martin</a>&#8216;s book <a title="Clean Code" href="http://blog.objectmentor.com/articles/2008/04/08/clean-code-whew">Clean Code</a>.  First of all, thanks and kudos to John for sharing his experiences!</p>
<p>I thought I&#8217;d give it a whirl as well, and decided to actually blog about it too (this is my first meaningful post dear reader; please be gentle!  <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ).</p>
<p>I focused on the <a href="http://www.artima.com/intv/dry.html">DRY</a> issue that John noted in his analysis: that the base pay calculations were duplicated in  <em>WageCalculatorForEmployee::CalculateWithoutOvertime</em> and <em>WageCalculatorForContractor::Calculate</em>.  To resolve this, I decided to split up the methods to calculate regular and overtime hours as well as the methods to calculate the corresponding pays.</p>
<p>I retained the 3 classes that John established, but modified them as follows:</p>
<p><pre class="brush: csharp;">
    public abstract class WageCalculatorBase
    {
        public const Decimal MAXIMUM_BASE_WEEKLY_HOURS = 40;
        public const Decimal MAXIMUM_TOTAL_WEEKLY_HOURS = 80;

        public Decimal HoursWorked { get; protected set; }
        public Decimal HourlyRate { get; protected set; }

        public WageCalculatorBase(Decimal hours, Decimal hourlyRate)
        {
            if (hours  MAXIMUM_TOTAL_WEEKLY_HOURS)
                throw new ArgumentOutOfRangeException(
                    String.Format(&quot;Hours must be between 0 and {0}.&quot;,
                    MAXIMUM_TOTAL_WEEKLY_HOURS));

            HoursWorked = hours;
            HourlyRate = hourlyRate;
        }

        public Decimal Calculate()
        {
            return CalculateBasePay() + CalculateOverTimePay();
        }

        protected Decimal CalculateBasePay()
        {
            return HourlyRate * BaseHours;
        }

        protected abstract Decimal CalculateOverTimePay();

        protected Decimal BaseHours
        {
            get
            {
                return HoursWorked &lt;=  MAXIMUM_BASE_WEEKLY_HOURS ? HoursWorked - MAXIMUM_BASE_WEEKLY_HOURS : 0;
            }
        }
        protected Decimal OvertimeHours
        {
            get
            {
                return HoursWorked &gt; MAXIMUM_BASE_WEEKLY_HOURS ? HoursWorked - MAXIMUM_BASE_WEEKLY_HOURS : 0;
            }
        }
    }
</pre></p>
<p><pre class="brush: csharp;">
  public class WageCalculatorForEmployee : WageCalculatorBase
  {
      protected const Decimal OVERTIME_MULTIPLIER = 1.5m;

      public WageCalculatorForEmployee(Decimal hours, Decimal hourlyRate)
          :base(hours, hourlyRate)
      {
      }

      protected override Decimal CalculateOverTimePay()
      {
          return OvertimeHours * OVERTIME_MULTIPLIER * HourlyRate;
      }

  }
</pre></p>
<p><pre class="brush: csharp;">
    public class WageCalculatorForContractor : WageCalculatorBase
    {
        public WageCalculatorForContractor(Decimal hours, Decimal hourlyRate)
            :base(hours, hourlyRate)
        {
        }

        protected override Decimal CalculateOverTimePay()
        {
            return OvertimeHours * HourlyRate;
        }

    }
</pre></p>
<p>Shortly after John&#8217;s post (and after I had sent a copy of my refactoring to John), I saw the <a href="http://blog.syntaxc4.net/post/2010/09/14/Reply-What-is-too-simple-and-small-to-refactor.aspx">response</a> provided by Cory Fowler [<a href="http://www.twitter.com/SyntaxC4">@SyntaxC4</a>], as well as <a href="http://msmvps.com/blogs/jon_skeet/default.aspx">another</a> by Jon Skeet [<a href="http://twitter.com/jonskeet">@jonskeet</a>].  Both certainly are interesting and provided me with some new insights.  They also added to the original requirements by considering a person who earns a different rate for various amounts of overtime worked.  I decided to try to implement something like this as well, and was able to do so without having to alter my originally refactored design.  Given this, I figure the <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a> approach was a decent fit to the problem.</p>
<p><pre class="brush: csharp;">
    public class WageCalculatorForMultiRateContractor : WageCalculatorBase
    {
        protected const Decimal BASE_OVERTIME_HOURS_SPAN = 10.0m;
        protected const Decimal EXTENDED_OVERTIME_HOURS_SPAN = 20.0m;
        protected const Decimal MAXIMAL_OVERTIME_HOURS_SPAN = 10.0m;

        protected const Decimal BASE_OVERTIME_MULTIPLIER = 1.5m;
        protected const Decimal EXTENDED_OVERTIME_MULTIPLIER = 2.0m;
        protected const Decimal MAXIMAL_OVERTIME_MULTIPLIER = 2.5m;

        public Decimal BaseOvertimeHours
        {
            get
            {
                return Math.Min(OvertimeHours, BASE_OVERTIME_HOURS_SPAN);
            }
        }

        public Decimal ExtendedOvertimeHours
        {
            get
            {
                var moreThanBaseOvertimeHours =
                    Math.Max(OvertimeHours - BASE_OVERTIME_HOURS_SPAN, 0);
                return Math.Min(moreThanBaseOvertimeHours, EXTENDED_OVERTIME_HOURS_SPAN);
            }
        }

        public Decimal MaximalOvertimeHours
        {
            get
            {
                var moreThanExtendedOvertimeHours =
                    Math.Max(OvertimeHours - BASE_OVERTIME_HOURS_SPAN - EXTENDED_OVERTIME_HOURS_SPAN, 0);
                return Math.Min(moreThanExtendedOvertimeHours, MAXIMAL_OVERTIME_HOURS_SPAN);
            }
        }

        public WageCalculatorForMultiRateContractor(Decimal hours, Decimal hourlyRate)
            :base(hours, hourlyRate)
        {
        }

        protected override Decimal CalculateOverTimePay()
        {
            var baseOvertimePay = BaseOvertimeHours * BASE_OVERTIME_MULTIPLIER * HourlyRate;
            var extendedOvertimePay = ExtendedOvertimeHours * EXTENDED_OVERTIME_MULTIPLIER * HourlyRate;
            var maximalOvertimePay = MaximalOvertimeHours * MAXIMAL_OVERTIME_MULTIPLIER * HourlyRate;

            return baseOvertimePay + extendedOvertimePay + maximalOvertimePay;
        }

    }
</pre></p>
<p><span style="font-size:smaller;">Copyright &#169; Cliff Mees 2010; all rights reserved.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingincaledon.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingincaledon.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingincaledon.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=8&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingincaledon.wordpress.com/2010/09/16/clean-code-experience/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31ded04f26fc8edba3052934455b78af?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cliffjmees</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://codingincaledon.wordpress.com/2010/09/16/hello-world/</link>
		<comments>http://codingincaledon.wordpress.com/2010/09/16/hello-world/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 02:18:31 +0000</pubDate>
		<dc:creator>Cliff Mees</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codingincaledon.wordpress.com/?p=1</guid>
		<description><![CDATA[After years of reading other people&#8217;s blog, I&#8217;ve decided to finally take a crack at it myself.  Hopefully, I&#8217;ll have some interesting pieces to contribute! Now, to tinker with some settings&#8230;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=1&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After years of reading other people&#8217;s blog, I&#8217;ve decided to finally take a crack at it myself.  Hopefully, I&#8217;ll have some interesting pieces to contribute!</p>
<p>Now, to tinker with some settings&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingincaledon.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingincaledon.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingincaledon.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingincaledon.wordpress.com&amp;blog=15899338&amp;post=1&amp;subd=codingincaledon&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingincaledon.wordpress.com/2010/09/16/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31ded04f26fc8edba3052934455b78af?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cliffjmees</media:title>
		</media:content>
	</item>
	</channel>
</rss>
