<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Awkward Coder</title>
	
	<link>http://www.awkwardcoder.com</link>
	<description>Learning how to behave in public.</description>
	<lastBuildDate>Fri, 12 Aug 2011 16:36:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/theawkwardcoder" /><feedburner:info uri="theawkwardcoder" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Encrypted Code: How to Get Your Code Completely Re-Written</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/bfMTQdSTToA/</link>
		<comments>http://www.awkwardcoder.com/index.php/2011/08/12/encrypted-code-how-to-get-your-code-completely-re-written/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 16:08:42 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=364</guid>
		<description><![CDATA[In the past, I have coded with some very bright and talented developers. Software development is like art, and I&#8217;ve worked along-side some true code artists. Their code is clean, readable, and expressive. Because of the care that they put into the design of their code, other developers can open the code base and, with [...]]]></description>
			<content:encoded><![CDATA[<p>In the past, I have coded with some very bright and talented developers. Software development is like art, and I&#8217;ve worked along-side some true code artists. Their code is clean, readable, and expressive. Because of the care that they put into the design of their code, other developers can open the code base and, with a normal level of skill, modify or extend it with little or no prior knowledge. They are efficient and conscientious. I have a lot of respect for software developers like the guys with whom I&#8217;ve had the pleasure of working. I want to be just like them.</p>
<p>And then there&#8217;s the other type of developer. We&#8217;ll call him Einstein. Einstein is super smart. He thinks on a whole &#8216;nother level. In fact, he&#8217;s so smart that when he writes software, he writes it on a whole &#8216;nother level. On the UI level, the program runs and performs well. It does everything that it&#8217;s supposed to do. It might even do it REALLY well. Einstein has certainly outdone himself. He finished the project a few days early and the client is happy with the product.</p>
<p>Fast forward 6 months when the client needs to add a feature. Einstein is tied up with another project. The client has to hire another developer to modify the code base. We&#8217;ll call him Innocent Bystander. The client says to Innocent Bystander, &#8220;We&#8217;re really happy with Einstein&#8217;s work. You should have no trouble at all modifying this program.&#8221; So, Innocent Bystander cracks open the code and starts reading. </p>
<p>What he finds is a code base that only an author could love. It&#8217;s a tangled mess of spaghetti code and obscurely reused code blocks. Variables are named with acronyms that have long since been forgotten. There are no tests to show how code is expected to behave. Classes and methods cover pages and pages of scrolling. Nothing makes sense. It&#8217;s like trying to listen in on an encrypted conversation. The code base, for all intents and purposes, is encrypted.</p>
<p>So, Einstein was so &#8220;smart&#8221; that he wrote a code base that no one but he could understand. Innocent Bystander has accepted the task of adding a feature to that code base. Innocent Bystander has a couple choices&#8230; 1) Spend hours of the client&#8217;s time decrypting, modifying, and handling regression, or 2) Spend hours of the client&#8217;s time re-writing a section of the code, re-factoring the code that uses it, and  handle regression. Either way, the client pays more than he should. Either way, there is regression. At least with the second option, the code base is left a little better than how Innocent Bystander found it.</p>
<p>Friends, countrymen, fellow developers. PLEASE please pLeAsE hear me out. If you are an Einstein and you&#8217;re still reading this&#8230; welcome to your first step to recovery. Admit you have a problem and start thinking about others while you code. Think about your client. Think about the next guy who has to come behind you. You don&#8217;t want either to hate you, right? Learn how to code in maintainable ways. </p>
<p>If you want to make sure your code is removed or re-written, just make it complicated and convoluted. No one will be able to follow your code and, boom&#8230; mission accomplished. You&#8217;ve effectively wasted the money of your client. The world would have been better off if you had applied your vast, immeasurable intelligence to something that lends itself to tangled webs of scattered thought process.. not software development. </p>
<p>If you want your code to stick around and make a difference, write it so that the next developer can read and understand it. Name your variables and methods to express their intent. Adhere to engineering principles like Single Responsibility, Open-Closed, and Dependency Inversion. Code in standard ways. Don&#8217;t try to be clever by stuffing loads of functionality into one reusable function. Spend time learning new ways to make your code expressive and readable.</p>
<p>No one cares how smart you are. It matters not that you&#8217;re on a whole &#8216;nother level. People only really care about your contribution over the long-haul. Clients may love you when you deliver the product early like a rock star, but the will cease to love you later when they have to pay again for the same code. No one will write blog articles about your awesome &#8220;swiss-army-knife-function-that-does-everything&#8221;. No, sir&#8230; in fact, they&#8217;ll write blog articles like this one. </p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/bfMTQdSTToA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2011/08/12/encrypted-code-how-to-get-your-code-completely-re-written/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2011/08/12/encrypted-code-how-to-get-your-code-completely-re-written/</feedburner:origLink></item>
		<item>
		<title>Exceptional Code</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/dQZhGFgDueE/</link>
		<comments>http://www.awkwardcoder.com/index.php/2011/06/18/exceptional-code/#comments</comments>
		<pubDate>Sun, 19 Jun 2011 01:00:52 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=328</guid>
		<description><![CDATA[When do you throw an exception? I never asked myself that question until a few weeks ago when some guys on my team started wondering aloud. I suppose I never asked because I just threw exceptions whenever I felt like it. But the question on the minds of my collegues has got me thinking too. [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Permanent Link to Exceptional Code" rel="bookmark" href="http://www.awkwardcoder.com/index.php/2011/06/18/exceptional-code/"></a></p>
<p>When do you throw an exception? I never asked myself that question until a few weeks ago when some guys on my team started wondering aloud. I suppose I never asked because I just threw exceptions whenever I felt like it. But the question on the minds of my collegues has got me thinking too. When should I throw exceptions?</p>
<p>One team member says that exceptions should only be thrown when there is exceptional behavior. That sounds reasonable, right? But now, we have to define exceptional behavior. Google says that an exception is, “A person or thing that is excluded from a general statement or does not follow a rule.” So exceptional behavior is conduct that breaks rules, simply put.</p>
<p>Take the following code for example:</p>
<pre class="brush: csharp; title: ; notranslate">
public Monkey GetMonkeyById(int id)
{
    var monkey = _monkeyRepository.Get(id);
    return monkey;
}</pre>
<p>The purpose of the function is expressed in the function’s name: “Get a monkey by its ID.” So, what if there is no monkey found by the given ID? Is that exceptional behavior? If you were inclined to say, “no,” you could do a few things in response to your lack of monkey. You could return null, but that’s a cheap shortcut that will cause problematic code down the line. You could possibly change the method to return an object that contains a monkey, a success flag, and a message (for when there is no monkey found), like so:</p>
<pre class="brush: csharp; title: ; notranslate">
public class MonkeyResponse
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public Monkey Monkey { get; set; }
}</pre>
<p>So, then, your function can go something like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public MonkeyResponse GetMonkeyById(int id)
{
    var monkey = _monkeyRepository.Get(id);
    return new MonkeyResponse
        {
            Monkey = monkey,
            Success = true,
        };
}</pre>
<p>There is something wrong with that function that I REALLY don’t like. The MonkeyResponse class will ALWAYS return a null or some empty value. If there is a monkey found, it will return a null/empty message. If there is no monkey found, the monkey property will be null. Neither of those options are satisfactory.</p>
<p>The fact of the matter is, there is a rule being broken in this function when an invalid ID is passed in. The function&#8217;s purpose is to retrieve a monkey. If that is not possible, then the code&#8217;s course has to change drastically from it&#8217;s intended purpose. We need to call a spade a spade, address the broken rule appropriately, and throw an exception.</p>
<p>Here’s my refactored function including an exception:</p>
<pre class="brush: csharp; title: ; notranslate">
public Monkey GetMonkeyById(int id)
{
    var monkey = _monkeyRepository.Get(id);
    if(monkey==null) throw new Exception(&quot;Monkey not found by that ID.&quot;);
    return monkey;
}</pre>
<p>I got rid of the MonkeyResponse object because I don’t need it anymore. Now, my function returns a Monkey if one is found, and throws an exception when someone passes in an invalid ID. This feels much better. I know that the repo is returning a null when no monkey can be found, but that&#8217;s a refactor for another day. I would probably move the exception from this function into the repo function and let the exception bubble up.</p>
<p>This really isn’t rocket science, but I think it’s important to question your methods from time to time. I’ve been throwing exceptions (on purpose) without justification for years. Now, I have at least gone through the thought process of justifying my methods. In a nutshell, here’s what I’ve come up with:</p>
<blockquote><p>Throw an exception when a function’s behavior breaks the rules and causes the code to change course as a result.</p></blockquote>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/dQZhGFgDueE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2011/06/18/exceptional-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2011/06/18/exceptional-code/</feedburner:origLink></item>
		<item>
		<title>Getting the “Web Control” Warm Fuzzy in MVC</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/gwzo2NRt3qs/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/10/28/getting-the-web-control-warm-fuzzy-in-mvc/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 15:00:27 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=296</guid>
		<description><![CDATA[I recently spoke at a Codecamp in Pittsburgh (fine city by the way) about moving from ASP.NET Webforms to ASP.NET MVC. I even wrote an accompanying blog article if you&#8217;re interested. As I was presenting, I realized I didn&#8217;t leave enough time to talk about the &#8220;web control&#8221; story in MVC. I promised on the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spoke at a Codecamp in Pittsburgh (fine city by the way) about moving from ASP.NET Webforms to ASP.NET MVC. I even wrote an <a href="http://www.awkwardcoder.com/index.php/2010/10/18/take-back-cont…ebforms-to-mvc/">accompanying blog article</a> if you&#8217;re interested. As I was presenting, I realized I didn&#8217;t leave enough time to talk about the &#8220;web control&#8221; story in MVC. I promised on the spot to post an article about that very thing, and, so, here it is!</p>
<p>One reason a lot of Webforms developers might be hesitant to move to MVC is because the apparent lack of web controls. Web controls save time and make development of ASP.NET applications a breeze (sometimes). So, if developers want to branch out and get into MVC, they need to know that THERE ARE wonderful control-like things they can use with ease. Here are some examples to get you rolling:</p>
<h2>Data Sources:</h2>
<p>In WebForms, it&#8217;s typical to drag a data source like ObjectDataSource, XmlDataSource, or SqlDataSource onto your page in order to provide data to a control like a GridView or Repeater. We have no such construct in MVC. The closest equivalent can be found in the concept of a &#8220;view model&#8221;. A view model is an object that has been passed to the view in order to populate the it with data from the controller. With WebForms, you usually think of the page going out to get it&#8217;s data. In MVC, the view is given the data. It&#8217;s a subtle difference, but an important one. So, when you find yourself in MVC and you need to have dynamic data on your view, think &#8220;view model&#8221;.</p>
<h2>GridViews:</h2>
<p>The GridView is possibly one of the most important controls for a line-of-business application developer. It&#8217;s also one of the more involved controls to mimic since there&#8217;s so much going on (paging, sorting, grouping, searching, etc). However, there&#8217;s a company called Trirand who has put together an open source library that does what the GridView does plus a LOT more. The library is called JQuery Grid and is written completely in JavaScript using JQuery (of course). Check out a ton of demos and example code <a href="http://trirand.com/blog/jqgrid/jqgrid.html" target="_blank">here</a>. You can download JQuery Grid (jqGrid for short) from their <a href="http://www.trirand.com/blog/" target="_blank">team blog</a>. Also, <a href="http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx" target="_blank">here&#8217;s an article by Phil Haack</a> that explains one way of using jqGrid with MVC. Even though I mentioned view models above as the preferred way to get data to a view, you should also look into using a JsonResult (from the controller) to populate a JQuery Grid.</p>
<h2>Labels:</h2>
<p>I know this one might seem like a no brainer, but I remember using the Label control a lot when I was developing Webforms apps. When the Label renders down to HTML, you get a &lt;span&gt; tag surrounding your text. Also, you would set the value of the Label from code-behind (though, with a bunch of ViewState mess). Instead of a Label control adding tags for you, if you&#8217;re so inclined, you can easily add your own &lt;span&gt; tags. Otherwise, let your model do it&#8217;s thing. If the &#8220;Name&#8221; property on my view model has been set to &#8220;Byron&#8221;,  then &lt;%=Model.Name%&gt; will render down to &#8220;Byron&#8221; (without the quotes). Sorry if that&#8217;s too simplistic, but I promised some web control replacements, and I aim to please.</p>
<h2>TextBoxes:</h2>
<p>The TextBox is an incredibly important UI control. In Webforms, you just drag a TextBox control onto your design surface and change its ID to something reachable from code-behind. With MVC, its almost the same, minus the clicking and dragging. Just enter &lt;%=Html.TextBoxFor(x=&gt;x.Name)%&gt; into your view. This will render down to something like &lt;input id=&#8217;Name&#8217; name=&#8217;Name&#8217; value=&#8217;Byron&#8217; /&gt;. Pretty clean. Pretty easy.</p>
<h2>Calendars:</h2>
<p>I have to admit, the first time I used the old ASP.NET Calendar control was also the last. That thing was hard to use and it looked like death warmed over. Eventually, the AJAX Toolkit came out with a much better rendition, but I had already fallen in love with another open source calendar control. Nevertheless, those aren&#8217;t available to me in MVC. So, I just use a textbox and apply JQuery UI&#8217;s Datepicker plugin. Here&#8217;s the <a href="http://jqueryui.com/demos/datepicker/" target="_blank">documentation page</a> for the Datepicker plugin. You can download JQuery UI from the same site. If my model has a property on is like &#8220;Birthdate&#8221;, I could just enter &lt;%=Html.TextBoxFor(x=&gt;x.Birthdate, new { <a href="http://twitter.com/class" target="_blank">@class</a>=&#8221;datepicker&#8221; })%&gt; into my view. This would eventually get rendered down to something like &lt;input type=&#8217;text&#8217; id=&#8217;Birthdate&#8217; name=&#8217;Birthdate&#8217; value=&#8217;11/12/1978 17:35:00&#8242; class=&#8217;datepicker&#8217; /&gt;. Now, assuming you have a reference to JQuery UI in your view (or master) somewhere, you can equip your new textbox with a JQuery calendar by adding $(&#8216;.datepicker&#8217;).datepicker(); somewhere in your JavaScript section.</p>
<h2>Lists and Repeaters:</h2>
<p>When a GridView won&#8217;t cut it, many WebForms developers turn to ListViews or Repeaters. ListViews were my particular favorite once I got the hang of them. If you&#8217;re familiar with these great controls, you will be right at home with MVC. As mentioned above, when you want to get data to a view, you pass it an object called a view model. The view model might contain a list of books or people along with other data that the view can use. If you have such a list, just code the iteration in HTML using a bit of c# to help it along (gasp! mixing c# with HTML?? &#8212; yes, get over it). You could do something like this:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ul&gt;
&lt;%foreach(var book in Model.Books) {%&gt;
    &lt;li&gt;&lt;%=book.Name%&gt;&lt;/li&gt;
&lt;%}%&gt;
&lt;/ul&gt;
</pre>
<h2>I think you get the point.</h2>
<p>Try it out! Between JQuery, MVC&#8217;s HTML Helpers, and your own noggin&#8217;, you should be getting the web control warm fuzzies in no time. After the initial learning curve, you&#8217;ll be cranking out apps left and right.</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/gwzo2NRt3qs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/10/28/getting-the-web-control-warm-fuzzy-in-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/10/28/getting-the-web-control-warm-fuzzy-in-mvc/</feedburner:origLink></item>
		<item>
		<title>Take Back Control: Moving From Webforms to MVC</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/N1oRHNa_myk/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/10/18/take-back-control-moving-from-webforms-to-mvc/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 15:00:22 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=286</guid>
		<description><![CDATA[I made a major paradigm shift earlier this year in terms of programming methodologies. See, I&#8217;ve been programming own my own (for the most part) since I was 9 years old, but I&#8217;ve never paid attention to how other people coded. I got my start writing RPG&#8217;s with my childhood friend and went on to [...]]]></description>
			<content:encoded><![CDATA[<p>I made a major paradigm shift earlier this year in terms of programming methodologies. See, I&#8217;ve been programming own my own (for the most part) since I was 9 years old, but I&#8217;ve never paid attention to how other people coded. I got my start writing RPG&#8217;s with my childhood friend and went on to write line of business apps for small and eventually large companies. Because of my lack of knowledge of the outside world, I repeated myself constantly, re-invented the wheel countless times, and never dreamed of testing my code beyond what my friend <a href="http://twitter.com/plip" target="_blank">@plip</a> calls &#8220;Exception-Driven Development&#8221;.</p>
<p>I credit my enlightenment to three things: my newfound co-workers, my own desire to constantly improve, and ASP.NET MVC. The first two are self-explanatory. The last might seem suspect. How can a framework change the way a person thinks about programming web apps?</p>
<p>I started using ASP.NET (from here, known as &#8220;Webforms&#8221;) a few years ago and really dug in. Because I like to at least feel like I know what I&#8217;m doing, I got to know the framework top to bottom. I loved how creating a web app was almost as easy as creating a windows app. I didn&#8217;t love how far I was removed from the underlying HTML, CSS and Javascript. Oh, well&#8230; Just drag a control on the design surface and shut-up!</p>
<p>Enter the new hotness. MVC zooms me in a few levels of abstraction and reminds me of what is really going on behind the scenes of a web application. If http is the metal, MVC is closer to it. Instead of clicky-draggy controls, I have to use the keyboard A LOT more. As a result, I actually know what&#8217;s going on. On top of all that, IT&#8217;S EASIER!!</p>
<p>After using MVC for some time, developing apps via BDD, I now feel like I know MVC top to bottom. After getting to know MVC so well and some residual (albeit waining) knowledge of Webforms, I wanted to provide my developer friends entrenched in Webforms-land a way to make the big switch, if they so choose.</p>
<h1>Some Reusable Concepts in Webforms and MVC:</h1>
<h2>Page to view</h2>
<p>In Webforms, you have the concept of a &#8220;page&#8221;. The page contains the big three (HTML, CSS, and JavaScript) and some place holders for data. Very close to the same thing with MVC. In MVC, I have the concept of a &#8220;view&#8221;. A view contains the big three, and some place holders for data.</p>
<h2>Controls to Helpers and JQuery</h2>
<p>In Webforms, you have the concept of &#8220;controls&#8221; that you can drag onto your design surface. The controls will eventually get rendered down to the big three. In MVC, I have the concept of &#8220;Html Helpers&#8221;. No dragging. Instead, you type the name of the helper into your view. Same as a control, an HTML helper gets rendered down to the big three when the view is loaded. Also, to make your UI elements look and feel snazzy, you&#8217;ve got JQuery. Actually we all do. JQuery is just a Javascript framework/library, so it doesn&#8217;t care whether you&#8217;re using PHP, Rails, or ASP.NET (or even ASP Classic for that matter). Code your own HTML or use a Helper, then apply JQuery to it&#8230; Victory.</p>
<h2>Code behind to controller</h2>
<p>In Webforms, when you want to add logic or data to a page, you drill into its &#8220;code-behind&#8221; file and add code to the Page_Load method. In MVC, I have the concept of a &#8220;controller&#8221; that has &#8220;actions&#8221;, each responsible for combining my data with my views.</p>
<h2>Events to routes and methods</h2>
<p>In Webforms, when you want a button to do something, you attach an event to it which appears in the page&#8217;s code-behind. Any actual logic that happens, does so during the post-back (after the post, before the back) and the page is (typically) redisplayed after the event takes its course. In MVC, I have links that describe the controller and action I want to kick off. If my action needs to receive some data (as is typical in a post-back), then my form action should describe the correct controller and action, and my action needs to know what to receive.</p>
<h2>User controls to partial views</h2>
<p>In my last year using Webforms, I had gotten into user controls big time. They are a great way to re-use code, which is my favorite pass-time. In Webforms, much like a typical control, a user control is a markup/code-behind combo that encapsulates some reusable, displayable functionality. However, unlike typical controls, user controls are custom and don&#8217;t come in a DLL. In MVC, I have something similar in the concept of a &#8220;partial view&#8221;. A partial view is simply a section of an HTML page with place holders for data. Like views, partial views can be rendered by controller actions. They can also be referenced in views themselves with a single line of code.</p>
<h2>Master Pages</h2>
<p>Where would we be without master pages, right? Imagine templating an ASP.NET website without a master page! As it turns out, Webforms master pages are so great, that they&#8217;re exactly the same in MVC. How&#8217;s that for reusable knowledge?</p>
<h1>Things you&#8217;ll have to change:</h1>
<h2>Page lifecycle:</h2>
<p>In Webforms, you typically think of an aspx page loading and then its code-behind Page_Load method being called. That&#8217;s not really what&#8217;s going on, but from 30,000 feet, everything looks simpler than it really is. Who knows? Maybe you have memorized the Webforms page lifecycle like I did. Well, throw away your complicated charts and graphs. It all gets a LOT simpler. In MVC, I have a route, a controller, a method, a model, and a view. The user enters or clicks a URL. The URL gets parsed into a route. The route specifies the controller and the method. The method scrapes up a model and returns a view.</p>
<h2>Postbacks:</h2>
<p>The concept of an http post is intact in MVC. As expected, right? MVC is &#8220;closer to the metal&#8221; as they say, so your typical http protocol methods are large and in charge. But, in Webforms, you have the concept of a &#8220;post-back&#8221;. A post back is nothing more than a page posting back into itself. In MVC, you have to put in some extra effort to achieve a real post-back. Instead, you post into actions that may or may not return the same posting view.</p>
<h2>Models for Views:</h2>
<p>In Webforms, the way to put data on a page is to call into the control on the page and give it the desired data. In MVC, I have to think about things a little differently. You know what a model is. Now imagine a view that expects a model with certain properties that the view may or may not decide to use. In MVC, when a view is rendered, it is often combined with something called a &#8220;view model&#8221;. At that point, any place holders are replaced with model data and the view is delivered to the browser. The difference is subtle, but it&#8217;s important. Instead of telling controls what to do, you provide them with what they need and let them do it on their own.</p>
<h2>Viewstate:</h2>
<p>In Webforms, state (anything kept in memory) is simulated by flooding the user&#8217;s browser with seemingly endless strings of encoded data called &#8220;Viewstate&#8221;. Every time the form is posted, all that &#8220;state&#8221; is transmitted and re-populated when the page is rendered again. In MVC, there is no state, just like any other web technology. The only thing that is transmitted is what you specify. This is dramatically cleaner and more efficient. Instead of depending on Viewstate to hang on to label and text box values, you are responsible for populating and repopulating. It turns out it&#8217;s not that big of a deal with help from the model binder and view models.</p>
<h1>Give it a try!</h1>
<p>If you&#8217;re an serious Webforms developer, you&#8217;ll probably end up loving MVC after you get comfortable with the framework. I hope some of this information is helpful as you take back control and move to ASP.NET MVC.</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/N1oRHNa_myk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/10/18/take-back-control-moving-from-webforms-to-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/10/18/take-back-control-moving-from-webforms-to-mvc/</feedburner:origLink></item>
		<item>
		<title>Teaching a String Calculator How to Behave</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/JrI-Rr9Rsto/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/10/11/teaching-a-string-calculator-how-to-behave/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 15:00:17 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=254</guid>
		<description><![CDATA[I prefer to design my code by specifying expected behavior before any code is written. Machine.Specifications (mspec) provides a great way to accomplish this with less overall code and the bonus of normal-person-readable specifications. BDD keeps me focused on what the code is actually supposed to do, not on the classes and methods themselves. To exercise my [...]]]></description>
			<content:encoded><![CDATA[<p>I prefer to design my code by specifying expected behavior before any code is written. <a href="http://www.awkwardcoder.com/index.php/2010/04/13/how-to-mspec/">Machine.Specifications (mspec)</a> provides a great way to accomplish this with less overall code and the bonus of normal-person-readable specifications. BDD keeps me focused on what the code is actually supposed to do, not on the classes and methods themselves. To exercise my BDD muscles, I decided to work through the popular <a href="http://osherove.com/tdd-kata-1/" target="_blank">&#8220;String Calculator Kata&#8221; by Roy Osherove</a>. His instructions were from the traditional TDD approach, though, so I took a stab at converting them to a simple user story with acceptance criteria. Here&#8217;s what I came up with:</p>
<h3>&#8220;As a user, I want to add numbers together so that I can know the sum.&#8221;</h3>
<p>WHEN provided an empty string,<br />
THEN it should return 0</p>
<p>WHEN provided one number,<br />
THEN it should return the same number</p>
<p>WHEN provided two numbers separated by a comma,<br />
THEN it should return the sum of those two numbers</p>
<p>WHEN provided an unknown amount of numbers separated by commas,<br />
THEN it should return the sum of all numbers provided</p>
<p>WHEN provided numbers with a new line character,<br />
THEN it should return the sum of all numbers provided</p>
<p>WHEN provided numbers separated by a specified delimiter,<br />
THEN it should return the sum of all numbers provided</p>
<p>WHEN provided one negative number,<br />
THEN it should throw an exception (“Negative numbers are not allowed – [Display the negative number.”)</p>
<p>WHEN provided more than one negative number,<br />
THEN it should throw an exception (“Negative numbers are not allowed – [Display the negative numbers.”)</p>
<p>So, let's dive in and see where the story leads us! My job from here on out is to create a spec based on a single acceptance criterion, run the spec so that it fails, write JUST ENOUGH code in my new string calculator class to make the spec pass, and then move on to the next acceptance criterion.</p>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided an empty string,<br />
THEN it should return 0</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_an_empty_string {

	Establish a_string_calculator_context = () =&gt;
		_stringCalculator = new StringCalculator();

	Because of = () =&gt;
		_result = _stringCalculator.Add(string.Empty);

	It should_return_zero = () =&gt;
		_result.ShouldEqual(0);

	static StringCalculator _stringCalculator;
	static int _result;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		return 0; //simplest thing to make it pass, right?

	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided one number,<br />
THEN it should return the same number</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_one_number {

	Establish a_string_calculator_context = () =&gt;
		_stringCalculator = new StringCalculator();

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;5&quot;);

	It should_return_the_same_number = () =&gt;
		_result.ShouldEqual(5);

	static StringCalculator _stringCalculator;
	static int _result;
}
</pre>
<p>Now, I'm starting to see some repetition in my context. Before moving forward, I'm going to refactor my specs a bit to use a common context. For this, I will use an abstract class that starts with the word "given".</p>
<pre class="brush: csharp; title: ; notranslate">

public abstract class given_a_string_calculator {

	Establish a_string_calculator_context = () =&gt;
		_stringCalculator = new StringCalculator();

	protected static StringCalculator _stringCalculator;
}
</pre>
<p>So, first spec looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_an_empty_string :
	given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(string.Empty);

	It should_return_zero = () =&gt;
		_result.ShouldEqual(0);

	static int _result;
}
</pre>
<p>...and my second spec looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_one_number :
	given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;5&quot;);

	It should_return_the_same_number = () =&gt;
		_result.ShouldEqual(5);

	static int _result;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: int.Parse(stringToCalculate);
	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided two numbers separated by a comma,<br />
THEN it should return the sum of those two numbers</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_two_number_separated_by_a_comma :
	given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;2,5&quot;);

	It should_return_the_sum_of_those_two_numbers = () =&gt;
		_result.ShouldEqual(7);

	static int _result;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: stringToCalculate.Split(new char[] { &#8216;,&#8217; })
				.Sum(x=&gt; int.Parse(x));
	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided an unknown amount of numbers separated by commas,<br />
THEN it should return the sum of all numbers provided</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_multiple_numbers_separated_by_commas :
given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;3,4,5&quot;);

	It should_return_the_sum_of_all_numbers_provided = () =&gt;
		_result.ShouldEqual(12);

	static int _result;
}
</pre>
<p>UH OH! This spec passed without any modification to the calculator. I don't like when this happens, but I don't think I could have made the Add method much simpler when adding two numbers separated by a comma. Now, because of my use of Linq's Sum extension method, it passes with any amount of numbers. Moving on...</p>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided numbers with a new line character,<br />
THEN it should return the sum of all numbers provided</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_numbers_with_a_new_line_character :
	given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;3,4\n6&quot;);

	It should_return_the_sum_of_all_numbers_provided = () =&gt;
		_result.ShouldEqual(13);

	static int _result;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: stringToCalculate.Split(new char[] { ',', '\n' })
				.Sum(x=&gt; int.Parse(x));
	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided numbers separated by a specified delimiter,<br />
THEN it should return the sum of all numbers provided</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_numbers_separated_by_a_specified_delimeter :
	given_a_string_calculator {

	Because of = () =&gt;
		_result = _stringCalculator.Add(&quot;//;\n3;4;7&quot;);

	It should_return_the_sum_of_all_numbers_provided = () =&gt;
		_result.ShouldEqual(14);

	static int _result;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		var delimiters = &quot;,\n&quot;;

		if (stringToCalculate.StartsWith(&quot;//&quot;)) {
			delimiters += stringToCalculate[2];
			stringToCalculate = stringToCalculate
				.Substring(4, stringToCalculate.Length - 4);
		}

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: stringToCalculate
				.Split(delimiters.ToCharArray())
				.Sum(x=&gt; int.Parse(x));
	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided one negative number,<br />
THEN it should throw an exception (“Negative numbers are not allowed – [Display the negative number.”)</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_one_negative_number :
        given_a_string_calculator {

	Because of = () =&gt;
		_exception = Catch.Exception(()=&gt;
			_stringCalculator.Add(&quot;-1,2&quot;));

	It should_throw_an_exception = () =&gt;
		_exception.Message
			.ShouldEqual(&quot;Negative numbers are not allowed: -1&quot;);

	static Exception _exception;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		var delimiters = &quot;,\n&quot;;

		if (stringToCalculate.StartsWith(&quot;//&quot;)) {
			delimiters += stringToCalculate[2];
			stringToCalculate = stringToCalculate
			.Substring(4, stringToCalculate.Length - 4);
		}

		if(stringToCalculate.Contains(&quot;-&quot;)) {
			throw new Exception(&quot;Negative numbers are not allowed: -1&quot;);
		}

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: stringToCalculate
				.Split(delimiters.ToCharArray())
				.Sum(x=&gt; int.Parse(x));
	}
}
</pre>
<h3>Acceptance Criterion:</h3>
<p>WHEN provided more than one negative number,<br />
THEN it should throw an exception (“Negative numbers are not allowed – [Display the negative numbers.”)</p>
<h3>Specification with mspec:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class when_provided_more_than_one_negative_number :
	given_a_string_calculator {

	Because of = () =&gt;
		_exception = Catch.Exception(() =&gt;
			_stringCalculator.Add(&quot;-1,-2&quot;));

	It should_throw_an_exception = () =&gt;
		_exception.Message.ShouldEqual(
			&quot;Negative numbers are not allowed: -1,-2&quot;);

	static Exception _exception;
}
</pre>
<h3>Resulting code:</h3>
<pre class="brush: csharp; title: ; notranslate">
public class StringCalculator {

	public int Add(string stringToCalculate) {

		var delimiters = &quot;,\n&quot;;

		if (stringToCalculate.StartsWith(&quot;//&quot;)) {
			delimiters += stringToCalculate[2];
			stringToCalculate = stringToCalculate
				.Substring(4, stringToCalculate.Length - 4);
		}

		if(stringToCalculate.Contains(&quot;-&quot;)) {
			throw new Exception(
				string.Format(&quot;Negative numbers are not allowed: {0}&quot;,
					string.Join(&quot;,&quot;, stringToCalculate.Split(
						delimiters.ToCharArray()).Where(x=&gt;
							int.Parse(x) &lt; 1 ))));
		}

		return string.IsNullOrWhiteSpace(stringToCalculate)
			? 0
			: stringToCalculate
				.Split(delimiters.ToCharArray())
				.Sum(x=&gt; int.Parse(x));
	}
}
</pre>
<p>Here are the results from all eight specs:</p>
<pre class="brush: plain; title: ; notranslate">
when provided more than one negative number
» should throw an exception
when provided one negative number
» should throw an exception
when provided numbers separated by a specified delimiter
» should return the sum of all numbers provided
when provided numbers with a new line character
» should return the sum of all numbers provided
when provided multiple numbers separated by commas
» should return the sum of all numbers provided
when provided two number separated by a comma
» should return the sum of those two numbers
when provided one number
» should return the same number
when provided an empty string
» should return zero

8 passed, 0 failed, 0 skipped, took 1.05 seconds (Machine.Specifications 0.3.0).
</pre>
<p>In a simple kata like this, the end-result is pretty similar to what might come out of a traditional TDD process. Imagine doing a much more complicated code project, and you'll probably begin to see how powerful and valuable Behavior-Driven Development can be.</p>
<p><a href="http://www.awkwardcoder.com/wp-content/uploads/2010/10/MSpecStringCalculator.zip">Download Source Code</a></p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/JrI-Rr9Rsto" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/10/11/teaching-a-string-calculator-how-to-behave/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/10/11/teaching-a-string-calculator-how-to-behave/</feedburner:origLink></item>
		<item>
		<title>Dependency Injection for Moms</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/Snw3dMvqekg/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/09/20/dependency-inversion-for-moms/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 15:00:49 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=244</guid>
		<description><![CDATA[I was explaining my last blog article to my mom when I realized that I was going to have to explain the dependency inversion principle to her before she would see the significance (or insignificance) of my article. So I proceeded to explain the principle using my hands to represent classes and dependencies. When I [...]]]></description>
			<content:encoded><![CDATA[<p>I was explaining my last blog article to my mom when I realized that I was going to have to explain  the dependency inversion principle to her before she would see the significance (or insignificance) of my article. So I proceeded to explain the principle using my hands to represent classes and dependencies. When I was finished, my mom (her name is Cheryl) said, &#8220;Yeah! I get that.&#8221; Wait a sec&#8230; Did my dear mother just grok dependency inversion in less than a minute? That&#8217;s why I&#8217;m here, now, typing on my iPad. To get whatever goodness I just said to my mom down on &#8220;paper&#8221; so that I can use it again.</p>
<p>Forget lofty terms like coupling and inversion for a few minutes.</p>
<p>Let&#8217;s say you a have a computer program that checks movies in and out of a movie store. Employees can log in and help customers at the counter. Customers can pick movies off the shelf and bring them to the counter to rent or check out. This program stores customer and movie information so that it can be retrieved later. It is also able to fetch extra movie information from the internet. When movies are returned, employees can check them back in to the program and put them back on the shelf for others to rent.</p>
<p>If you were to make a list of all the things the program needed to be able to do, you could pretty easily mark all the items that depend on some external system. For the sake of a simple discussion, an external system is something outside of your program that you have limited or no control over. Just from my description of the program above, you could extract a few possible dependencies:</p>
<p>1. Log in/authentication system<br />
2. Database for storing inventory<br />
3. Web service for getting extra movie information</p>
<p>Now, the majority of programmers handle such dependencies by embedding them inside the program. But, what happens when something needs to change in one of the dependencies? The database gets too big and you have to upgrade to another type of database. The web service you were using closes shop and you have to add another one. Since these dependencies are embedded, the programmer has to change the core inner workings of the program. You already spent months fixing bugs after the first release. Well, get ready for another round of bug fixes and broken code.</p>
<p>A better way is to separate any external dependencies from your core code and refer to them very  loosely. Everything still works like it did before, but, now, your dependencies are modular and interchangeable. Swapping database vendors? No problem&#8230; You don&#8217;t even have to touch your core program. Want to write tests? No problem&#8230; Just swap out your external dependencies for fake ones and test away!</p>
<p>So, instead of your movie store app &#8220;having&#8221; a greenMovieStoreDatabase (just a name I came up with), it &#8220;uses&#8221; a genericMovieStoreDatabase and is told, &#8220;today, your genericMovieStoreDatabase will be a greenMovieStoreDatabase.&#8221; In this example, there&#8217;s a massive difference between &#8220;having&#8221; and &#8220;using&#8221;. If your program &#8220;has&#8221; a database, then it clings to it with iron claws daring you to mess with it&#8230; It&#8217;s embedded. If your program &#8220;uses&#8221; a generic database, it doesn&#8217;t necessarily care what color the database is as long as it acts like a database.</p>
<p>I would include some code samples, but my mom probably wouldn&#8217;t care about interfaces and implementations, IOC containers and bootstrappers. Who am I kidding? She probably didn&#8217;t care about dependency inversion either!</p>
<p>She&#8217;s a good lady for listening.</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/Snw3dMvqekg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/09/20/dependency-inversion-for-moms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/09/20/dependency-inversion-for-moms/</feedburner:origLink></item>
		<item>
		<title>End the Tyranny of System Time</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/eG1RrshXOuU/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/09/13/end-the-tyrany-of-system-time/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 02:04:00 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=222</guid>
		<description><![CDATA[The people, places and conditions mentioned in the following article are fictional. Any similarities to any of my past contracts is purely coincidental. I was consulting on a large project one day and discovered that it had a big problem. The company had been dealing with this problem year after year, having the same discussions, [...]]]></description>
			<content:encoded><![CDATA[<p>The people, places and conditions mentioned in the following article are fictional. Any similarities to any of my past contracts is purely coincidental.</p>
<p>I was consulting on a large project one day and discovered that it had a big problem. The company had been dealing with this problem year after year, having the same discussions, and coming up with the same broken solutions. One poor fellow even tried to help them escape the cycle, but got bogged down by the complexity. All this trouble over something I had never considered to be that big of a deal until then. So, it got me thinking&#8230;</p>
<p>System time is an external dependency! Maybe you&#8217;ve always known this, but the thought had JUST occurred to me. It seems harmless, right? Every language has some form of system time and the API for it probably hasn&#8217;t changed since the language&#8217;s first release. It sings a sweet lullaby and soothes into a false sense of security, but beware!  System time is a wolf in sheep&#8217;s clothing&#8230; an evil tyrant. You see, once you begin to write time-based logic into your code, you have upgraded system time to a full-blown dependency. If you&#8217;re familiar with the concepts of &#8220;<a href="http://en.wikipedia.org/wiki/Coupling_(computer_science)">coupling</a>&#8221; and the &#8220;<a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">dependency inversion principle</a>&#8220;, then a siren should be going off in your head right about now. Any external system that your code depends on to function should be injected into your class in some way, shape, or form. System time should be NO exception.</p>
<p>Back to the company I was working with. They had more than five independent systems that depend on system time in one way or another. Logging, validation rules, emails, you name it. For the most part, system were designed and written fairly well. The company was trying to follow Uncle Bob&#8217;s <a href="http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">SOLID principles</a> as best they could, though with little focus on automated testing. After all the systems were built, someone said, &#8220;Well, how can QA test the system for different days? On October 1st, the home page is supposed to turn blue. We need to be able to see that happen.&#8221; Even after creating such a loosely-coupled masterpiece, they didn&#8217;t think to consider system time to be the external dependency that it truly was. The entire code base, every tier and independent system, was SO tightly-coupled to system time (on multiple servers) that the final solution ended up being to set up a separate server cluster (including a domain controller) and change each server&#8217;s time manually. WOW.</p>
<p>Retrospect is a wonderful teacher. If each system had been built with automated testing as a priority, then we wouldn&#8217;t have ended up calling up the infrastructure team to set up a temporary network with which to do manual testing.</p>
<p>I&#8217;m sold. From here on out, I&#8217;m considering system time an external dependency. With that in mind, here&#8217;s a solution that I would propose to any team who is about to use system time in one of their projects.</p>
<p>First, a way to get a configured time out of some data source. I will use the  section of an app.config or web.config for this demo. You can write the implementation of ITimeConfiguration to pull from any data source (database, text file, registry, or nowhere at all).</p>
<pre class="brush: csharp; title: ; notranslate">
public interface ITimeConfiguration
{
    DateTime GetConfiguredTime();
}

public class TimeConfiguration : ITimeConfiguration
{
    public DateTime GetConfiguredTime()
    {
        var configuredTime = ConfigurationManager.AppSettings[&quot;ConfiguredTime&quot;];
    var outputDateTime = new DateTime();
    if( !string.IsNullOrWhiteSpace( configuredTime ) )
    {
        DateTime.TryParse(configuredTime, out outputDateTime);
    }
    return outputDateTime;
    }
}
</pre>
<p>Next, we need a time provider to consume our ITimeConfiguration:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface ITimeProvider
{
    DateTime Now();
}

public class TimeProvider : ITimeProvider
{
    readonly ITimeConfiguration _timeConfiguration;

    public TimeProvider(ITimeConfiguration timeConfiguration)
    {
        _timeConfiguration = timeConfiguration;
    }

    public DateTime Now()
    {
        var configuredTime = _timeConfiguration.GetConfiguredTime();
        return configuredTime == DateTime.MinValue
            ? DateTime.Now
            : configuredTime;
    }
}
</pre>
<p>Now, when we need to access the current (or configured) time, we can just pass ITimeProvider in to the class as a dependency. It&#8217;s mockable, testable, extensible, flexible, and it does laundry.</p>
<p>Well, maybe the laundry part is going too far. But I really do think this is better than consuming system time directly. I just implemented this scheme in a project and it&#8217;s working great. And I feel like a huge weight has been lifted off my shoulders. The tyranny of system time is finally over!</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/eG1RrshXOuU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/09/13/end-the-tyrany-of-system-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/09/13/end-the-tyrany-of-system-time/</feedburner:origLink></item>
		<item>
		<title>How to Thrive in Pairs</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/SxhH7mWb8ww/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/08/27/how-to-thrive-in-pairs/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 03:08:02 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Agile]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=200</guid>
		<description><![CDATA[One of my recent articles was a tongue-in-cheek tutorial entitled, “10 Ways to Kill Pair Programming”. I&#8217;ve always been more interested in focusing on solutions instead of problems, so I can&#8217;t just release an article like that and not follow up with some suggestions on how to make “Pair Programming” work. Admittedly, I&#8217;m not the [...]]]></description>
			<content:encoded><![CDATA[<p>One of my recent articles was a tongue-in-cheek tutorial entitled, “<a href="http://www.awkwardcoder.com/index.php/2010/08/27/10-ways-to-kill-pair-programming/">10 Ways to Kill Pair Programming</a>”. I&#8217;ve always been more interested in focusing on solutions instead of problems, so I can&#8217;t just release an article like that and not follow up with some suggestions on how to make “Pair Programming” work. Admittedly, I&#8217;m not the most seasoned blogger in the world of Agile practices, but “Pair Programming” is a human experience, and I am an experienced human. As with anything you read in the blog-o-sphere, please take my suggestions with a grain of salt (whatever that means).</p>
<p>I started to learn computer programming around 1986 with my good friend, <a href="http://www.linkedin.com/in/hamanng">Garret Hamann</a>. We didn&#8217;t know it at the time, but we were practicing a technique that would soon be known as “Pair Programming”. Garret learned from his dad who was an engineering student at Vanderbilt (probably via pairing). Garret transferred what he knew to me when we would pair for hours on the weekends. We would write RPG&#8217;s on my Commodore 64, taking turns on the keyboard, talking, arguing, learning. Then we would re-write those games on Garret&#8217;s Apple II. What a great way to learn to code!</p>
<p>“Pair Programming” is kind of a big deal these days. Adoption is widespread, especially amongst those who run in Agile circles (ever increasing, thankfully)&#8230; and with good reason. The benefits are huge. Here are just a few:</p>
<ul>
<li><strong>TRIBAL KNOWLEDGE:</strong> Even if 	you never switch pairing partners, your team feels the benefits or 	pairing simply because you&#8217;re not the only person on the team that 	knows how a section of code works. If you get hit by a bus or win 	the lottery, your team has another way to access your knowledge 	through your partner.</li>
<li><strong>NO NEED FOR CODE REVIEWS: </strong>Code 	is reviewed as it&#8217;s written. This provides a much more thorough and 	meaningful code review process and saves time.</li>
<li><strong>BETTER DESIGN MEANS LESS 	REFACTORING: </strong>Two heads are better than one, no matter how smart 	you think you are. When done right, pairing partners can help each 	other make better design decisions as the code unfolds. Because the 	code is designed better, you will naturally have fewer needs to go 	back and refactor code.</li>
<li><strong>CONSISTENT FOCUS: </strong>Ever feel 	like your focus is waning? It happens to everyone, but it doesn&#8217;t 	always happen at the same time for everyone. When pairing, when one 	partner&#8217;s focus begins to suffer, the other partner can help keep 	the ball rolling with a word or two. Better focus means more forward 	progress.</li>
</ul>
<p>Without realizing some of the 	benefits, it might be easy to look at “Pair Programming” as a 	waste of time or a productivity reducer. But, in fact, coding in 	pairs make the overall software development process much more 	efficient and enjoyable.</p>
<p>In my earlier article about “Pair Programming,” I proposed some ways to kill it off. Today, I&#8217;d like to suggest a few ways to help your team thrive in pairs.</p>
<ol>
<li><strong>BE WILLING TO LOSE: </strong>When 	you have an opinion and your pairing partner has an opposing 	opinion, you should talk it out. Maybe draw some circles and lines 	on the whiteboard. The goal is to arrive at the best decision, not 	for your opinion to be the winner. Sometimes, the best decision 	might be to stop discussing it and pick one. After all, you can 	always refactor later if it turns out you went the wrong direction. 	Being willing to lose is an essential part of working as member of a 	team and is even more essential for pairing.</li>
<li><strong>BE AN ENCOURAGER: </strong>You like 	to be praised for a job well done. Give the same consideration to 	your pairing partner. Take opportunities to build him/her up in 	front of the team and make it genuine. Talk about your partner more 	than yourself. Call attention to his/her best contributions.</li>
<li><strong>VOCALIZE CODE: </strong>Code 	certainly isn&#8217;t English, but we all have the ability to speak an English version when we&#8217;re explaining code to someone. Instead of 	coding silently, vocalize as you code. Keep a constant conversation 	going. If the only sound in the room is fingers on keys, you&#8217;re 	doing it wrong.</li>
<li><strong>EXPECT AND ACCEPT CORRECTION: </strong>Part of the huge benefit to pairing is the combining of two sets 	of experience. The only way to truly tap into that combined 	experience is to correct and discuss. Expect your pairing partner to 	question your decisions. When they do, put your pride in the trash 	can and discuss it like you care about the project more than your 	own opinion. If your partner isn&#8217;t starting discussions, start them 	yourself and invite him/her to interrupt at any moment.</li>
<li><strong>LEAVE SOME SPACE:</strong> Even 	though it&#8217;s a good practice to correct and discuss while pairing, 	it&#8217;s not so good if you go crazy with it. If your partner misses a 	semicolon or enters a typo, give them more than a half second before 	chiming in.</li>
<li><strong>ALLOW SPEED DIFFERENCES: </strong>Some 	programmers like to zip through code like it&#8217;s a race. Other 	programmers take their time and move at a much slower pace. Decide 	as a pair how you will handle the differences in speed and stick to 	it for an entire iteration. If you&#8217;re vocalizing your code, you will 	naturally slow down at some points and speed up at others. The main 	thing is to respect your partner&#8217;s pace.</li>
<li><strong>SWAP DRIVERS OFTEN: </strong>Being 	in the driver&#8217;s seat is admittedly more fun. With that in mind, swap 	seats every so often. If your partner doesn&#8217;t request it, you should 	offer it.</li>
</ol>
<p>For more information on &#8220;Pair Programming&#8221; and how to make it work for you or your team, check out these great articles:</p>
<ul>
<li>Pair Programming in Extreme Programming (XP) Rules &#8211; <a href="http://www.extremeprogramming.org/rules/pair.html">http://www.extremeprogramming.org/rules/pair.html</a></li>
<li>Wikipedia&#8217;s Take &#8211; <a href="http://en.wikipedia.org/wiki/Pair_programming">http://en.wikipedia.org/wiki/Pair_programming</a></li>
<li>Jeff Atwood&#8217;s comparison of Pair Programming and Code Reviews &#8211; <a href="http://www.codinghorror.com/blog/2007/11/pair-programming-vs-code-reviews.html">http://www.codinghorror.com/blog/2007/11/pair-programming-vs-code-reviews.html</a></li>
<li>Alan Shorkin&#8217;s views on effective and ineffective pair programming &#8211; <a href="http://www.skorks.com/2009/07/effective-vs-ineffective-pair-programming/">http://www.skorks.com/2009/07/effective-vs-ineffective-pair-programming/</a></li>
<li>Rod Hilton expresses his undying love for Pair Programming &#8211; <a href="http://www.nomachetejuggling.com/2009/02/21/i-love-pair-programming/">http://www.nomachetejuggling.com/2009/02/21/i-love-pair-programming/</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/SxhH7mWb8ww" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/08/27/how-to-thrive-in-pairs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/08/27/how-to-thrive-in-pairs/</feedburner:origLink></item>
		<item>
		<title>10 Ways to Kill Pair Programming</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/5_5vjzV2DoA/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/08/27/10-ways-to-kill-pair-programming/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 21:05:37 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Agile]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=199</guid>
		<description><![CDATA[If you have a hard time working with other humans or find that other humans have a hard time working with you, you may want to continue reading. If you value your own opinion over any other regardless of validity, then listen up. If you are a better coder than anyone on your team and [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a hard time working with other humans or find that other humans have a hard time working with you, you may want to continue reading. If you value your own opinion over any other regardless of validity, then listen up. If you are a better coder than anyone on your team and you want to keep it that way, then lean in closer. There is a software development technique that is gaining popularity amongst the “agile crazies” that threatens your quiet, peaceful, lonely existence. They call this technique, “Pair Programming.”</p>
<p>Wikipedia.com describes “Pair Programming” this way:</p>
<blockquote><p><a name="cite_ref-0"></a><strong>Pair programming</strong> is an <a href="http://en.wikipedia.org/wiki/Agile_software_development">agile software development</a> technique in which two <a href="http://en.wikipedia.org/wiki/Computer_programmer">programmers</a> work together at one work station. One types in <a href="http://en.wikipedia.org/wiki/Code_%28computer_programming%29">code</a> while the other <a href="http://en.wikipedia.org/wiki/Code_review">reviews</a> each line of code as it is typed in. The person typing is called the <strong>driver</strong>. The person reviewing the code is called the <strong>observer</strong> (or <strong>navigator</strong><a href="http://en.wikipedia.org/wiki/Pair_programming#cite_note-0">[1]</a>). The two programmers switch roles frequently.</p></blockquote>
<p>Sounds nice, right? Well, if I described you correctly in the paragraph above, then the answer is, “wrong!” Working so closely with another programmer is likely to end badly for you. However, “Pair Programming” has been building momentum over the last few years, especially since there has been an increased focus in software engineering practices, code quality, and ethical programming. But who needs any of those things? You make great software and you don&#8217;t feel like you should have to change the way you work&#8230; ever.</p>
<p>It is for those reasons and more that I decided to release this list of useful weapons in the fight against progress.</p>
<ol>
<li><strong>EXPRESS YOUR SUPERIORITY: </strong>You&#8217;re a 	better coder and you know it. Be sure your pairing partner knows it 	as well. Take every opportunity to hold your knowledge over your 	partner&#8217;s head. The best way to elevate yourself is to make your 	partner a little lower, even if only in your own mind.</li>
<li><strong>NEVER BACK DOWN: </strong>Your opinion is 	correct, or else it wouldn&#8217;t be yours. A correct opinion is worth 	fighting for to the bitter end. When one argument tactic begins to 	fail, switch to another, thereby flanking your opponent. Eventually, 	they will give up.</li>
<li><strong>GLOAT: </strong>Remember that discussion 	from 15 minutes ago where your opinion gained the victory? Of course 	you do. Well, in case your partner has already blocked it out of 	his/her mind, bring it back up when you see a place where it 	applies. Say things like, “See how much better it is since we did 	this my way?”</li>
<li><strong>WATCH SILENTLY:</strong> Nothing is more 	boring than watching someone code, right? Well, why make it better 	by communicating? Instead, sit silently and watch the code go across 	the screen. After a few minutes, take our your phone and update your 	status on Twitter or check your email. You may miss opportunities to 	discuss coding decisions, but you&#8217;ll likely be able to gloat about 	them later when refactoring becomes necessary (which is inevitable 	since you&#8217;re not in the driver&#8217;s seat).</li>
<li><strong>CODE SILENTLY: </strong>Why should you have 	to explain what you&#8217;re doing in your code. Your pairing partner can 	see it on the screen! Besides, they probably wouldn&#8217;t understand 	anyhow. Better to just code the way you always have because that&#8217;s 	when you&#8217;re at your best. Don&#8217;t talk through your code and certainly 	never explain any decisions you&#8217;re making.</li>
<li><strong>BETRAY TRUST: </strong>When you&#8217;re shooting 	the breeze with your co-workers or chatting on IRC, be sure to bring 	up funny or embarrassing stories from your “Pair Programming” 	experience. Of course, avoid subjects that reveal your own 	deficiencies, but be sure to highlight your pairing partner&#8217;s lack 	or prowess. Doing this behind his/her back is encouraged, since you 	will likely feel more free to embellish.</li>
<li><strong>DOMINATE THE KEYBOARD: </strong>Even though 	you are bent on killing “Pair Programming” in your team, you&#8217;re 	not a complete tool. As the better coder, you feel responsible for 	getting the project complete despite the handicap the “agile 	crazies” have placed on your team. To that end, and working within 	the confines or “Pair Programming”, you should make every effort 	to keep your position in the driver&#8217;s seat.</li>
<li><strong>CODE ALONE:</strong> Even though your team 	has decided to adopt “Pair Programming”, it doesn&#8217;t mean you 	can&#8217;t sneak in some private coding time (for old time&#8217;s sake). Ideal 	times to code privately are during coffee, lunch or restroom breaks. 	Be sure to start one or two sections of code while your partner is 	away from your workstation. As a result, you&#8217;ll get more time in the 	driver&#8217;s seat because you&#8217;re the only one who knows your intent and 	direction.</li>
<li><strong>WIN: </strong>Defeat is not an option for 	you. Stalemate is also not an option. It is imperative that you 	argue your point until your pairing partner sees your way of 	thinking or has laid down his guns and surrenders. One way to know 	you&#8217;ve won is when your partner ceases to express opinions and 	blindly accepts anything you say. One other benefit to this 	technique is that you will likely get to drive for the remainder of 	the project. Victory is sweet.</li>
<li><strong>COMPLAIN: </strong>Chances are, your 	organization or team has one or two “agile crazies” that sold 	the higher-ups on the concept of “Pair Programming.” That means 	the higher-ups are probably eager to hear about how it&#8217;s going. If 	your other attempts at sabotage are effective, then you should have 	quite a bit to complain about. Go around your team mates and fire 	off some emails to your manager or his/her superiors. Be sure to 	explain the obvious reasons why two programmers on one keyboard 	simply cannot work. Explain that morale across the entire team is low 	and that productivity would be back to “normal” if they would 	just put you in a position of influence. Be sure to taint the 	reputation of the “agile crazies”, maybe even calling them names 	like “agile crazies”.</li>
</ol>
<p>Above all, remember your greatness. You&#8217;re better looking, smarter, more experienced, faster, and certainly more humble than anyone else on your team. Let those truths guide you, and, even though you&#8217;ll be viewed by everyone around you as a jerk, you&#8217;ll have single-handedly defeated the “Pair Programming” dragon and that should be enough to keep you warm at night&#8230; alone&#8230; with your cats.</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/5_5vjzV2DoA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/08/27/10-ways-to-kill-pair-programming/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/08/27/10-ways-to-kill-pair-programming/</feedburner:origLink></item>
		<item>
		<title>DevLink Inspires New App: FlickTrap</title>
		<link>http://feedproxy.google.com/~r/theawkwardcoder/~3/2vBA-OolUs0/</link>
		<comments>http://www.awkwardcoder.com/index.php/2010/08/07/flicktrap-helps-you-remember-movie/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 21:58:07 +0000</pubDate>
		<dc:creator>Byron Sommardahl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.awkwardcoder.com/?p=187</guid>
		<description><![CDATA[I'm tired of saying to my friends, "I want to see that movie," and quickly forgetting. I might even say that I want to see it when it comes out on video, but I don't make a habit of going to the video store so I miss it that way as well. Then, five months later I'm the only guy at a party who hasn't seen it. So, my idea is to create an app that helps me remember movies I want to see and reminds me when they are available (and on what medium).]]></description>
			<content:encoded><![CDATA[<p>I just got finished with DevLink 2010. DevLink is a yearly conference for software developers in Nashville. This was my first year at DevLink and I was truly inspired by the experience. In fact, I&#8217;m so inspired that I decided to get serious about an app idea I&#8217;ve been batting around. I&#8217;m posting this here for accountability. I need your help to stay on task. If you like the idea, let me know you expect to see something from it. I&#8217;m pretty sure if I know people are counting on me, I will work hard to be sure I don&#8217;t let them down!</p>
<p>I&#8217;m tired of saying to my friends, &#8220;I want to see that movie,&#8221; and quickly forgetting. I might even say that I want to see it when it comes out on video, but I don&#8217;t make a habit of going to the video store so I miss it that way as well. Then, five months later I&#8217;m the only guy at a party who hasn&#8217;t seen it. So, my idea is to create an app that helps me remember movies I want to see and reminds me when they are available (and on what medium).</p>
<p>My plan today is to produce this app in stages. Here&#8217;s a break-down.</p>
<ol>
<li>Create a web app.</li>
<li>Add a mobile-friendly implementation of the same web app.</li>
<li>Re-use code from web app&#8217;s domain for iPhone app.</li>
<li>Re-use code from web app&#8217;s domain for Android app.</li>
<li>See where that takes me.</li>
</ol>
<p>I&#8217;m going to be developing this app using .NET 4.0 and C#. My reason for that, besides the fact that the .NET stack is my back-yard, is because I can create a great, well-designed, loosely-coupled, web app using ASP.NET MVC2 and then turn around and re-use my domain layer on nearly any other platform with Mono.</p>
<p>You are my customer. If you&#8217;re reading this, I&#8217;m talking to you. Let me know what you think. Shoot me an email, a DM <a href="http://twitter.com/sommardahl" target="_blank">@sommardahl</a>, and a comment. Expect to see something on this soon!</p>
<p>For sneak peeks, check out <a title="FlickTrap" href="http://flicktrap.com">http://flicktrap.com</a>.</p>
<p>Source can be found on GitHub at <a href="http://github.com/bsommardahl/FlickTrap">http://github.com/bsommardahl/FlickTrap</a>.</p>
<img src="http://feeds.feedburner.com/~r/theawkwardcoder/~4/2vBA-OolUs0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.awkwardcoder.com/index.php/2010/08/07/flicktrap-helps-you-remember-movie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.awkwardcoder.com/index.php/2010/08/07/flicktrap-helps-you-remember-movie/</feedburner:origLink></item>
	</channel>
</rss>

