<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[dave^2 = -1]]></title>
  <link href="http://davesquared.net/atom.xml" rel="self"/>
  <link href="http://davesquared.net/"/>
  <updated>2012-01-31T10:56:15+11:00</updated>
  <id>http://davesquared.net/</id>
  <author>
    <name><![CDATA[David Tchepak]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Goodbye Blogger]]></title>
    <link href="http://davesquared.net/2012/01/goodbye-blogger.html"/>
    <updated>2012-01-29T21:28:00+11:00</updated>
    <id>http://davesquared.net/2012/01/goodbye-blogger</id>
    <content type="html"><![CDATA[<p>After blogging for five years on <a href="http://www.blogger.com">Blogger</a>, I&#8217;ve finally decided to take the plunge and run my own blog. I&#8217;ve actually really enjoyed hosting with Blogger; I think it&#8217;s a pretty under-rated platform. It&#8217;s easy to get started, possible to do all sorts of crazy customisations, and they&#8217;ve recently started adding some cool stuff like mobile templates and <a href="http://buzz.blogger.com/2011/03/fresh-new-perspectives-for-your-blog.html">different dynamic views</a>. So why on earth am I leaving again?</p>

<!--more-->


<p>Well, a couple of reasons. First, I like to write posts in <a href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> using Vim. For the last year or so I&#8217;ve been converting posts to HTML, piping the output to clipboard, and pasting it into Blogger&#8217;s web UI. Switching to a platform that supports Markdown makes that a little simpler. I&#8217;ve also never really like the Blogger/Picasa Web image integration thing, so running my own blog means I can dump images wherever I want to and embed away. I also wanted to experiment a bit more with different layouts, but rather than relying on Blogger&#8217;s templating system, I wanted to work with straight HTML and CSS (well, via <a href="http://en.wikipedia.org/wiki/Sass_(stylesheet_language">SASS</a>). This would also buy me separation between layout, content, and platform.</p>

<p>If all this sounds a bit tenuous to you then you are probably right. I think all this is justification for me just wanting a change after half a decade on the same platform.</p>

<p>And so you are now reading a static site generated by <a href="http://octopress.org/">Octopress</a>, hosted on <a href="http://www.heroku.com/">Heroku</a>. I&#8217;ve imported everything over from Blogger and for the most part have kept the same links to avoid breaking stuff. If you find anything amiss or have any problems reading on your browser / feed / device please let me know and I&#8217;ll do my best to fix it up (yes, I know the font is large :)).</p>

<p>Thanks to <a href="https://twitter.com/aeoth">Paul</a> for good naturedly goading me into finally flicking over to Octopress and for helping me out with some questions; <a href="http://twitter.com/troyhunt">Troy</a>, <a href="http://twitter.com/thecolonial">OJ</a> and <a href="https://twitter.com/tarnacious">Tarn</a> for their advice; and <a href="https://twitter.com/thomasjo">Thomas</a> for putting up with all sorts of inane questions I bombarded him with over email and IM.</p>

<p>And finally, thanks to Blogger for an enjoyable 5 years! So long, and thanks for all the posts and fish and stuff. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What do you enjoy about programming?]]></title>
    <link href="http://davesquared.net/2011/12/what-do-you-enjoy-about-programming.html"/>
    <updated>2011-12-16T20:17:00+11:00</updated>
    <id>http://davesquared.net/2011/12/what-do-you-enjoy-about-programming</id>
    <content type="html"><![CDATA[<p>Over the last couple of weeks I think I&#8217;ve identified one of the main things I enjoy about programming. For me, it seems to be the puzzle-like aspect of it; finding ways to slot small bits and pieces together to create/achieve something bigger. When I&#8217;m working on a problem I can approach in this way I find it much easier to get into a good <a href="http://en.wikipedia.org/wiki/Flow_(psychology)">flow</a> where I&#8217;m productive, creative and happy.</p>

<p>This idea started to dawn on me while I was in <a href="https://twitter.com/joshuakerievsky">Joshua Kerievsky&#8217;s</a> Refactoring workshop at <a href="http://www.yowconference.com.au/">YOW 2011</a>. We were going through refactoring exercises, but trying to take extremely small steps to keep the tests green. The final goal could be achieved fairly easily by moving a few chunks of code around while keeping the tests broken for a while, but I really enjoyed the challenge of finding small steps that would keep the tests passing all the way to the final state.</p>

<p>This was further reinforced at <a href="https://twitter.com/dibblego">Tony&#8217;s</a> excellent Functional Programming workshop, where we spent quite a bit of time trying to fit together various functions (and partial applications of them) according to their type signatures. I thoroughly enjoyed this process, despite really struggling to get the solutions much of the time. I also realised I get an extra kick out of these puzzles when there is an element of elegance or simplicity to the way these pieces fit together. For example, implementing <code>map</code> with folds rather than recursion and pattern matching.</p>

<p>I think identifying what you enjoy about programming (or any activity really) can be an important part of maintaining motivation, and improving productivity and creativity as a result. I&#8217;m going to put this idea to the test by trying to break down problems in ways that I can use approaches I enjoy, and see if that helps me get better results, both in terms of solution and my happiness and enthusiasm.</p>

<p>This knowledge also helps me better understand some of my weaknesses. I have spent a lot of time over the years trying to piece together various patterns and practices to try and find a process I can continually apply to create great software. In other words, trying to solve the puzzle of how to develop software. My propensity to approach things as puzzles has probably led me astray here, as the unlikelihood of a silver bullet for software development has been demonstrated time and time again. Recognising this, I&#8217;ll try be more careful to make sure I&#8217;m solving the right problems, and not just inventing problems to puzzle-solve (although that can be educational as well, provided it&#8217;s deliberate).</p>

<p>So, what is it about programming you enjoy? Can you use that knowledge to improve the way you work?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dave's not so excellent typing adventure]]></title>
    <link href="http://davesquared.net/2011/12/daves-not-so-excellent-typing-adventure.html"/>
    <updated>2011-12-13T17:40:00+11:00</updated>
    <id>http://davesquared.net/2011/12/daves-not-so-excellent-typing-adventure</id>
    <content type="html"><![CDATA[<p>A few weeks ago I decided to depart from my usual QWERTY use and try an alternate keyboard layout. Rather than go the moderately rare <a href="http://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard">Dvorak</a> layout, I decided to go full-hipster and try <a href="http://en.wikipedia.org/wiki/Colemak#Colemak">Colemak</a>. This post describes the ensuing hilarity and (mis)adventure.</p>

<h2>Why not QWERTY?</h2>

<p>I type reasonably well with QWERTY: normally between 90 - 110 wpm. I started doing some practice to try and improve my accuracy and technique and noticed a few things. First, my right hand tended to drift all over the keyboard (away from the home position), which would result in me sometimes getting lost and making a mistake. A mistake at 110 wpm means typing around 10 characters in the second it takes you to realise it. Backing out those characters, correcting the mistake, correcting your positioning, and continuing gives your typing speed a pretty big hit and is quite frustrating.</p>

<p>Second, my hands had to do all sorts of gymnastics while typing. Common English character combinations like &#8216;st&#8217;, &#8216;ion&#8217;, &#8216;ce&#8217;, &#8216;ed&#8217; require some big shifts that take you away from the home position (slow and error prone), or require using the same finger for consecutive characters (slow and encourages shortcuts which shift your position).</p>

<p>Third, QWERTY is left-hand biased (at least for English). This can become quite fatiguing. There are also words like &#8216;minimum&#8217; and the frequently-typed &#8216;sweaterdresses&#8217; (pointed out on the <a href="http://colemak.com/wiki/index.php?title=Ergonomic">Colemak site</a>) that rely on a single hand; again, quite fatiguing. </p>

<p>I started to wonder if improving my QWERTY was worthwhile. Maybe I should try an alternate keyboard layout like Dvorak, which was meant to address many of these comfort and accuracy issues (my main goal), and also improve overall speed (a distant second goal).</p>

<h2>Why Colemak?</h2>

<p>It was around this time I saw <a href="http://twitter.com/thecolonial">@TheColonial</a> mention something about the Colemak layout. I&#8217;d never heard of it, but in trying to research what it was I found some promising signs. Colemak was designed to be efficient and ergonomic by keeping commonly used keys on the home row and on the strongest fingers (check out the <a href="http://colemak.com/forum/viewtopic.php?id=128">heatmaps</a> for Colemak et al.), as well as being easy to learn by not deviating too far from QWERTY (the entire bottom left hand is unchanged from QWERTY, which means common shortcuts like Ctrl+Z/X/C/V are unchanged).</p>

<p>If you&#8217;ve ever experienced <a href="http://www.hanselman.com/blog/TheProgrammersHands.aspx">programmer hands</a>, you&#8217;ll appreciate why it is important to look after yourself and avoid RSI. I happened across <a href="http://patorjk.com/keyboard-layout-analyzer/">PAT or JK&#8217;s keyboard layout analyser</a>, plugged in a few pieces of code and blog posts, and found that, had I typed these using Colemak instead of QWERTY, my fingers would have moved in the order of 50% less to type the same text. Figures like 192m for QWERTY, 97m for Colemak were not uncommon. It also tended to beat out Dvorak. How could anyone interested in efficiency <em>not</em> try that out?</p>

<h2>Three weeks with Colemak</h2>

<p>I started out doing Colemak exercises with <a href="http://web.me.com/typetrainer4mac/aTypeTrainer4Mac/home.html">aTypeTrainerForMac</a>. Largely due to its commonalities with QWERTY, the layout itself was quite easy to learn. I had the key positions memorised by the end of the first day, but using it effectively was another matter entirely. </p>

<p>No matter how well I knew the individual keys, I found my brain wanted to think about typing a whole word or pattern at a time. I could start typing the first few characters in Colemak, get to a familiar pattern like &#8216;ion&#8217;, and my brain would leap ahead and get me to complete the word (incorrectly) in QWERTY. Getting my reason to fight these reflexes was very uncomfortable, but at the same time a fascinating insight into how the brain works.</p>

<p>After 1 week of fairly intense practice, I could type steadily at 20 wpm with Colemak, but my QWERTY had dropped a bit to 80.</p>

<p>Shortly after that (with the help of autocomplete) I was able to use Colemak pretty effectively. After three weeks of almost exclusive use I could type between 40-50 wpm and it felt very comfortable. My technique and accuracy were improving. </p>

<p>It was then I found my QWERTY was down to 15 wpm. That is not a typo; a measly 15 wpm. I was completely unable to use a standard keyboard.</p>

<h2>Return of the QWERTY</h2>

<p>I was a few days away from attending a Code Retreat in Brisbane, and was really concerned about being able to pair with people when I couldn&#8217;t type properly. I decided to jump back into QWERTY full time. It took a solid day, but after being completely useless for an hour or so the reflexes started to come back. By the end of the day I was back to 90 wpm (provided I didn&#8217;t think too hard about it :)). Surprisingly I could still type ~30 wpm on Colemak.</p>

<p>One thing that really surprised me is how clumsy QWERTY felt to me. I could feel the added strain on my fingers, wrists and shoulders due to the increased movement. Accuracy seemed to come much easier with Colemak, although that could have been a product of how I had trained myself up on all the characters, rather than on real text. I also really started to notice the bad touch typing habits I had formed with QWERTY, like leaving the home row and using wrong fingers to hit keys due to the key positions of many words.</p>

<h2>Current state</h2>

<p>I&#8217;m really undecided on how to proceed from here. One one hand Colemak really seems a lot more comfortable, and I&#8217;m pretty confident I could get up to comparable levels of speed with it.</p>

<p>On the other hand, almost <em>every other keyboard in the English-speaking world uses QWERTY</em>. It&#8217;s obviously going to take me a bit of work to maintain proficiency with both QWERTY and Colemak. The alternative, being unable to use most other computers effectively, is very unappealing. If I was only ever going to use my own computer I&#8217;d almost definitely stick with Colemak, but as I do a lot of pair programming it really comes down to exactly how much effort it&#8217;s going to take to be bi-typal. </p>

<p>If we end up getting <a href="https://sermoa.wordpress.com/2011/11/27/qido-for-colemak-please/">QIDO for Colemak</a> it may help for pair programming, but I&#8217;m still concerned about being stuck at some random computer and being unable to use it.</p>

<p>My other concern is that my perceived increase in comfort with Colemak is at half the speed of my QWERTY typing. I&#8217;m not sure whether Colemak would still be comfortable once I hit 100 wpm, or whether I would get the same discomfort as I do with QWERTY.</p>

<p>So there you have it; a bit of a non-conclusion. Colemak seems to have a lot of things going for it, but it&#8217;s hard to recommend it in the face of the ubiquity of QWERTY and with only anecdotal evidence of it&#8217;s ergonomic benefits (and there&#8217;s some <a href="http://ergonomicinfo.com/articles/colemak-dvorak/">counter-anecdotes</a> too). It&#8217;s definitely an interesting experiment to try, just for some insight in to how your brain works with typing, but unless you only use your own hardware I&#8217;d suggest keeping up speed on QWERTY as well.</p>

<p>As for me, I think I&#8217;ll try and develop both QWERTY and Colemak in parallel for a while and see what happens.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Documentation via automation]]></title>
    <link href="http://davesquared.net/2011/11/documentation-via-automation.html"/>
    <updated>2011-11-26T22:01:00+11:00</updated>
    <id>http://davesquared.net/2011/11/documentation-via-automation</id>
    <content type="html"><![CDATA[<p>There are lots of benefits to automating common, recurring tasks like builds and deployments. We gain reliable, repeatable results and remove the cost of duplicated effort. However I&#8217;ve just come across a related benefit that makes it worthwhile considering automation for less common tasks, and that&#8217;s as a form of reliable documentation.</p>

<p>I&#8217;m currently battling some driver signing issues. We have some incomplete documentation from a previous occasion we&#8217;ve been through a related problem, but no one remembers the exact steps used. Because this previous occasion was reasonably considered a one-off, it was never automated. If it had been, it would now provide reliable, repeatable, unambiguously documented steps for getting me out of my current jam. Even if not an exact match for my current requirements, having a working example I could use as a basis to work from would be invaluable.</p>

<p>This has made me think that it is probably worth automating a whole bunch of tasks I hadn&#8217;t previously considered due to their infrequent nature. What better documentation of a procedure than working, executable steps? Sure, there may be a need for accompanying rationale, but given a blind choice between a Word/wiki document and something that actually runs and works, I&#8217;m picking the latter.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Some mocking opinions]]></title>
    <link href="http://davesquared.net/2011/11/some-mocking-opinions.html"/>
    <updated>2011-11-22T22:25:00+11:00</updated>
    <id>http://davesquared.net/2011/11/some-mocking-opinions</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been thinking through how I use test doubles (mocks, stubs, test spies, etc) recently, and thought I&#8217;d write down a snapshot of my current opinions on the subject.</p>

<h2>Don&#8217;t mock types you don&#8217;t own</h2>

<p>I&#8217;ve <a href="http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html">written about this before</a>, and I still think it is good advice. Test down to your lowest level of abstraction, then integration / contract / acceptance test over the boundary. By mocking a type you don&#8217;t own that dependency starts bleeding in to your code and pushing your own abstractions in potentially unhelpful ways. You&#8217;re also not really testing much; unless you also have good contract tests then checking you&#8217;ve called a specific external method is not going to tell you much about whether your code does what it needs to.</p>

<p>Contrived example: say I was faking out <code>Math.Round()</code> (let&#8217;s pretend it&#8217;s an instance method or you are using a framework that can mock statics via the profiler API):</p>

<pre><code>[Test]
public void Calculate_with_rounding() {
    math.Round(2.5).Returns(3); //Fake math.Round
    var subject = new Calculation(math);

    Assert.AreEqual(subject.AddAndRound(1, 1.5), 3);
}
</code></pre>

<p>Perfectly reasonable? Well, except for the fact .NET uses banker&#8217;s rounding and rounds <a href="http://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-instead-of-3-are-you-kidding-me">2.5 to 2</a> (and 1.5 to 2 for that matter). If you were using Python (which rounds away from 0), you would have been spot on. If you care about the rounding method, you&#8217;ve now got a bug.</p>

<p>If something as simple as rounding can trip us up, imagine what we could do if we start mocking ORM or ADO.NET calls.</p>

<h2>Try to avoid mocks in acceptance tests</h2>

<p>In my experience this tends to result in too much behaviour being pushed into the mocks. My first preference is to use real pieces, second is to hand-code fakes that have enough logic to work as required, and convenience methods to help tests configure them appropriately. As per &quot;don&#8217;t mock types you don&#8217;t own&quot;, it is also a good idea to test your fakes match the real behaviour.</p>

<h2>Learn mocking before a mocking framework</h2>

<p>I&#8217;ve often heard developers new to automated testing say things like &quot;I really need to learn (Rhino Mocks | Moq | Mocking Framework X)&quot;. I think this is the wrong emphasis; before learning a framework for creating test doubles it&#8217;s important to understand how test doubles work and how to use them.</p>

<p>For me, a great way to learn was to hand-roll all my fake objects for my tests to act as required. Manually stubbing out values and/or recording calls gave me a good understanding of the different types of test doubles (mocks, spies, stubs etc.) and how they work. Once this got old (very quickly) it was fairly simple to take the behaviour I knew how to hand-code and translate that into the syntax required by a mocking framework. It just became a matter of automating what I was already doing. (It also helped me understand common difficulties like trying to mock non-virtual members.)</p>

<h2>Don&#8217;t explictly test intermediate steps or inconsequential details</h2>

<p>If we assert on details of an implementation we tend to get tight coupling and brittle tests. An example I have seen fairly frequently is:</p>

<pre><code>[Test]
public void Should_get_the_widget_from_the_factory() {
    var factory = MockRepository.GenerateMock&lt;IWidgetFactory&gt;();
    var subject = new Foo(factory);
    subject.DoStuff();
    factory.AssertWasCalled(x =&gt; x.GetWidget());
}

[Test]
public void Should_turn_the_widget() {
    var widget = MockRepository.GenerateMock&lt;IWidget&gt;();
    var factory = MockRepository.GenerateStub&lt;IWidgetFactory&gt;();
    factory.Stub(x =&gt; x.GetWidget()).Return(widget);
    var subject = new Foo(factory);
    subject.DoStuff();
    widget.AssertWasCalled(x =&gt; x.Turn());
}
</code></pre>

<p>Here the first test is a completely redundant. The second test covers that entire code path (how else could the widget from the factory get turned, if the subject did not call the factory?). Now you could argue that you prefer the extra, explicit specification the first test provides, to which I&#8217;d respond that I don&#8217;t think it&#8217;s worth the pain from the additional friction it causes when you want to change this implementation detail. </p>

<p>Besides, what do we really care about for our subject? That it uses a factory? Or that it turns the widget? Focus on how you want the object to behave, not how it implements that behaviour.</p>

<p>This approach can help lead us to better abstractions, as we start identifying roles and responsibilities separately from implementation details. And it will definitely make your code easier to change without the friction of over-specified tests.</p>

<h2>Mock interaction with the contract, not the specific implementation</h2>

<p>On a highly related point, the aim of abstraction is decoupling from the implementation. If we are configuring our test doubles with lots of behaviour that our unit tests are relying on then our object is coupled to that particular implementation, not to a contract of behaviour or a role. For an abstraction to be effective we should be able to drop in a completely new implementation that fulfils the required role. This is not the case if we need to set up a test double&#8217;s method to call another dependency and return some rearrangement of the result. If we&#8217;re relying on that in our test then our abstraction has failed.</p>

<p>Sometimes you just get stuck with having to perform a callback from a stub, but in general if you are pushing behaviour into your mock, re-think the design or consider using a hand-coded fake before you go contorting your mocking framework.</p>

<h2>Beware over-abstraction</h2>

<p>It is quite easy to churn out layers of useless abstractions when using mock frameworks. Abstractions have a cost. Feedback from tests is great, but pay close attention to SOLID and the rules of simple design and call out to meaningful abstractions, rather than putting in a dependency just for testing. I wrote some <a href="http://davesquared.net/2011/06/abstraction-and-oo.html">guidelines on abstractions</a> a while back that I don&#8217;t entirely disagree with yet.</p>

<h2>Some unanswered questions</h2>

<h3>Mocks vs. stubs, tell vs. ask.</h3>

<p>I&#8217;ve tended to prefer stubs over mocks (stubbing out the results of calls rather than checking they were received), as per the widget factory example above. This flies in the face of the &quot;Tell, don&#8217;t ask&quot; principle, which recommends we don&#8217;t ask a collaborator for some state and act on the result, but instead give the collaborator the state it needs from us and tell it what to do with it. This seems to suggest I should be using mocks (checking received calls) a whole lot more than I stub them out.</p>

<h3>Avoiding &quot;Yet Another Factory&quot;</h3>

<p>If an object news up something, our unit test will typically have almost no ability to affect that object. If we want to check our subject news up a view model and calls <code>Activate()</code> on it, we have no way of asserting this was done without exposing <code>IsActivated</code> and relying on that implementation detail. Leaky abstraction. Bad.</p>

<p>One solution is injecting a factory into our object. We can stub out what this returns, make it return another test double, and then check it received the <code>Activate()</code> call. Just introduce a factory. Yet another factory. Searching through files matching <code>*Factory</code> becomes an exercise akin to reading War and Peace. And they&#8217;re generally not even <a href="http://en.wikipedia.org/wiki/Factory_pattern">real factories</a>! They don&#8217;t choose a particular implementation, they are just a glorified wrapper over a single constructor.</p>

<p>Sure, I sometimes try to ease my conscience by injecting a factory method as a <code>Func&lt;T&gt;</code> which my IoC container helps me with. But deep down I know its still YAF, and a small part of me dies.</p>

<p>I&#8217;m hoping choosing &quot;better&quot; abstractions will help me with this, but I&#8217;ve had limited success to date.</p>

<h3>Interface explosion</h3>

<p>C# seems to make it difficult to do testing without using interfaces. And so I end up pulling out yet another interface that will never see another implementation. I&#8217;ve heard <a href="https://twitter.com/shannoncornish">Shannon</a> refer to them as &quot;the new header files&quot;. Every class has its interface documented in its header/interface file. It&#8217;s easy to say just choose better abstractions where the interface can be reused, but this is still something I struggle with.</p>

<h3>Mocking in dynamic languages</h3>

<p>This post has been written primarily from the perspective of static languages; I not sure how much (if any) applies to dynamic languages. From my limited experience testing and mocking seem to be done quite differently in languages like Ruby and Python. I&#8217;m keen to learn more about how mocking is done in these languages and see how much can help me improve how I test.</p>

<h2>End of transmission</h2>

<p>This has been a brain dump of my current opinions about mocking. If you agree, disagree, and/or can help me with some of my unanswered questions, please leave a comment.</p>

<p>Now if you&#8217;ll excuse me, I&#8217;m off to code up yet another factory&#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Odd problem with Git, Windows and virus-checkers]]></title>
    <link href="http://davesquared.net/2011/10/odd-problem-with-git-windows-and-virus.html"/>
    <updated>2011-10-22T22:19:00+11:00</updated>
    <id>http://davesquared.net/2011/10/odd-problem-with-git-windows-and-virus</id>
    <content type="html"><![CDATA[<p>Had a really odd git problem this week, with an even odder solution, so am posting in the hope of helping the next poor dev who has to try and track this down via Google.</p>

<p>We had a branch checked out with 2 new commits on it, and we wanted to squash it into a single commit using <code>git rebase -i (basecommit)</code>. This would start the normal interactive rebase, then get into a loop of printing the following error to console:</p>

<pre><code>mv: cannot move '.git/rebase-merge/git-rebase-todo.new' to '.git/rebase-merge/git-rebase.todo'
</code></pre>

<p>Looking at the <code>.git/rebase-merge</code> folder, I could see the <code>git-rebase-todo.new</code> file getting repeatedly created, then deleted. This was happening on two different machines.</p>

<p>Some googling lead me to <a href="http://osdir.com/ml/msysgit/2010-01/msg00007.html">this post</a> which suggested a virus checker may be locking the file.</p>

<p>Sure enough, turning off Microsoft Security Essentials&#8217; Real-time protection, doing the rebase, then turning it back on again, resolved the problem. I&#8217;ve never had this problem before so must have just gotten &#8220;lucky&#8221; with this particular file matching some property the virus checker was looking for.</p>

<p>I guess this is probably worth trying whenever getting strange file IO errors from any software ported to Windows.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Unhelpful flaming is unhelpful]]></title>
    <link href="http://davesquared.net/2011/09/unhelpful-flaming-is-unhelpful.html"/>
    <updated>2011-09-20T23:47:00+10:00</updated>
    <id>http://davesquared.net/2011/09/unhelpful-flaming-is-unhelpful</id>
    <content type="html"><![CDATA[<p>I recently read an excellent post on <a href="http://ecomwriter.com/2011/09/08/patience-and-respect/">patience and respect</a> from Andrew Sharp, suggesting the radical idea of trying to empathise with the much-maligned <i>whoever-wrote-this-steaming-pile-of-code-that-I-now-have-the-misfortune-of-having-to-work-with</i> instead of the usual cursing of the previous author&#8217;s name, professionalism, family, and very existence in a bout of barely contained nerd-rage.</p>

<p>This is often quite embarrassing when you realise the previous author was you, but this isn&#8217;t the only reason to stop the practice of flaming the authors of rotten code.</p>

<p>Yes, this post is basically a rehash of <a href="http://ecomwriter.com/2011/09/08/patience-and-respect/">Andrew&#8217;s post</a>, but as it resonated with me I felt compelled to write down my perspective on the topic. His is concise and beautifully written, but mine briefly teases SharePoint, so take your pick. :D</p>

<h2>The situation</h2>

<p>You open the code. It&#8217;s worse than you thought. Your face falls as you flick through the files you need to change, and see one or more of the following:</p>

<ul>
<li>Classes with thousands of lines.</li>
<li>Methods with thousands of lines.</li>
<li>Nested <code>if</code>s so deep you need to scroll horizontally to find the end of the arrow.</li>
<li>Constructors with 17 arguments.</li>
<li><a href="http://www.richard-banks.org/2011/02/anti-region-campaign.html">Regions</a>.</li>
<li>No trace of automated testing. Or of even having been manually tested.</li>
<li>Code that only works due to some obscure side-effects.</li>
<li>Code that only works on Tuesdays.</li>
<li>Liberal sprinklings of <code>Thread.Sleep</code>.</li>
<li>Object Oriented code where all object members are static.</li>
<li>No.respect_for.the_law().of.demeter()</li>
<li>SharePoint involved somewhere.</li>
<li>Incredibly clever code featuring an abstract factory pattern to produce strategies which are injected into commands applied with double dispatch using the visitor pattern, proxying through to WCF calls batched into units of work and persisted using an EF entity data model. You can&#8217;t help but think this is overkill to print FizzBuzz.</li>
</ul>


<h2>The initial reaction</h2>

<ul>
<li>WTF!?!?</li>
<li>No seriously; WTF?!!?!?</li>
<li>What were they thinking?</li>
<li>What an unprofessional jerk!</li>
<li>How do they sleep at night?!?!</li>
<li>I&#8217;ve got to post this to twitter. Everyone will get a good laugh at this.</li>
<li>Oh, and I&#8217;ll blog it! There&#8217;s another couple of hundred people to pour scorn and ridicule on this deserving schmuck!</li>
<li>Even better, I&#8217;m posting this to thedailywtf.com!</li>
</ul>

<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.osnews.com/story/19266/WTFs_m" imageanchor="1"><img border="0" height="377" width="400" src="http://davesquared.net/images/fromblogger/s400-wtfm.jpg" /></a></div>

<h2>Stop! Hammertime!</h2>

<p>What exactly are we doing here? We&#8217;re all puffed up with self-righteous indignation, but what is this vehement response going to accomplish? Shame an incompetent person out of programming, thereby ridding the glorious profession of software development of one of those rare dullards that somehow slip into the ranks of the brilliant, intellectual master-craftsmen and women that dominate the industry? (In case it isn&#8217;t clear, that sentence carries a &lt;sarcasm/&gt; tag :P).</p>

<p>Probably not. All we&#8217;re doing is stroking our own egos, delighting in someone else screwing up for a change, and further fostering the culture of competitiveness, elitism and macho-attitudes that seem to keep <a href="http://www.jamesthigpen.com/blog/2009/02/28/altnet-seattle-2009-why-so-mean/">popping up in our industry</a>.</p>

<p>Sure, short-term you may feel better for venting, but adding to the bile on the internet is a bit like peeing in the dam that supplies your own drinking water.</p>

<p>So what could we do instead?</p>

<h2>Consider the context</h2>

<p>Let&#8217;s take a deep breath, then start thinking about why this code stinks. It is important to realise that people are generally trying to do good work. There are probably valid reasons the code you are currently staring at seems wrong, and most of them probably have nothing to do with trying to spite you.</p>

<ul>
<li>The author is inexperienced.</li>
<li>The author does not have experience with that particular problem.</li>
<li>The author is trying a new approach in an attempt to learn.</li>
<li>The author doesn&#8217;t know about a language feature, pattern, or project convention.</li>
<li>The author was working to an insane deadline that would cost the company millions if the feature wasn&#8217;t out the door in 33 minutes.</li>
<li>The author was actually confronted with worse code, and did substantial tidy up of a whole bunch of classes and extracted the most ugly bit to a place where it could be refactored later (i.e. where you currently need to modify ;)). The reason you are not currently fighting another 3 fires is because of the work they did.</li>
<li>The author knew the code wasn&#8217;t perfect, but it wasn&#8217;t obvious at the time how else to factor it.</li>
<li>The author just seperated from their long term partner, they have a parent in hospital, their car was smashed by an uninsured driver, their bank just increase its mortgage rate and their pet just died in a tragic canoeing accident. They had trouble concentrating on the code they wrote that week.</li>
<li>The author implemented a bunch of awesome classes, and this one.</li>
<li>The author wrote the code in a way that was perfect at the time, it&#8217;s just that times have changed.</li>
<li>The code is actually really good. We just don&#8217;t see the beauty of it.</li>
<li>The author made a mistake. You may remember making one or two of these yourself in your younger days.</li>
<li>The author was forced at gunpoint to write the code by a rogue PM who was also holding the author&#8217;s family hostage, dangling them precariously over a vat of potent hydrochloric acid while screaming &#8220;add another responsibility to the class or they all get it! Add more regions and superfluous comments! And CLOSE THAT CLASS FOR EXTENSION!!! DO IT!!!1!&#8221;</li>
</ul>

<p>None of this means the author is a bad person, and yet us programmers seem to take perverse delight in opening some code and sniggering &#8220;WTF?!!? What were they thinking?&#8221; while firing up our favourite blame tool. Maybe it just makes us feel better about all the cruddy code we&#8217;ve written?</p>

<h2>Opportunities</h2>

<p>Instead of dwelling on the negatives, let&#8217;s look at the opportunities the code has given us to do some more constructive things than suggested by our initial reactions.</p>

<ul>
<li>If this is public code, we could contact the author, thank them for being brave enough to share the code in the first place, and <a href="http://ecomwriter.com/2011/09/08/patience-and-respect/">constructively suggest alternate approaches</a>, explaining the rationale for doing so and being careful to keep the person and the code separate (e.g. favour &#8220;this class could use a factory to create this instance&#8221; or &#8220;we could use a factory&#8230;&#8221; over &#8220;you shouldn&#8217;t create this here, use a factory&#8221;. This helps get past people&#8217;s natural tendency to become defensive, and prevents you from slipping into flame mode.)</li>
<li>If you work with the person, maybe consider how <a href="http://davesquared.net/2010/08/there-is-no-u-in-collective-ownership.html">collective ownership</a> applies, and think about how the team can improve things in future.</li>
<li>If you can see how the code ended up in its present state and what to do about it, take the general concepts (not the personal attack) and blog that, post it to a programming mailing list, conduct a brownbag session at work, present the general principles at your local usergroup, talk at a conference, get on a podcast, start a podcast. Teach it. Educate. Play a small but valuable part in lifting the community and advancing the practice of programming.</li>
<li>Think about how APIs or patterns could change or be used to prevent the problem. Release an open source project that makes getting it right easy and obvious (as well as an explanation of why this approach works).</li>
<li>Practice giving criticism in a constructive, respectful way. This is a skill worth developing.</li>
<li>Recognise that <a href="http://davesquared.net/2011/08/technical-debt.html">there is no perfect, tech debt-free solution</a>, and think up of lots of ways to fix the problems you see in the code. This will help you to improve both your coding and your perspective. Combine this with aforementioned blogging, presenting, teaching etc.</li>
<li>Encourage the author. Programming is hard; praising anything good about the code and encouraging them to try improvements can give them more confidence to tackle future problems more creatively. It&#8217;s amazing what being freed from the fear of failure can do.</li>
<li>We could, hmm, I dunno, maybe FIX THE DARN CODE AND MOVE ON WITH OUR LIVES!!! ;)</li>
</ul>

<div class="note"><b>Tip:</b> While explaining a techinical concept, avoid condescending words or words that diminish the task like &#8220;simply&#8221;, &#8220;basic&#8221;, &#8220;trivial&#8221;, and &#8220;obvious&#8221;. It may be to you, or it may be in hindsight. Recognise that if it were any of these things to the author, then they would have done it that way in the first place.</div>

<h2>Why bother?</h2>

<p>Because programming culture seems filled with large egos, worthless competitiveness, negativity, and cults around celebrities which discourage both creative thinking and challenges to the status quo. And yet we also have many examples of selflessness and generosity: people pouring hundreds of hours into OSS projects, donating their time to usergroups and mailing lists, and blogging stuff they learn so that others may benefit. Let&#8217;s spend more time on these positives within the dev community. By focusing on the opportunities finding &#8220;bad&#8221; code gives us, I believe we can get a community where:</p>

<ul>
<li>People can ask honest questions without being told to &#8220;RTFM n00b&#8221;.</li>
<li>People spend more time discussing different approaches than defending previous ones.</li>
<li>People can post sample code or OSS projects without being ripped apart and mocked, but instead given constructive advice and congratulations for having the guts to risk failing in public.</li>
<li>All people can contribute; not just the loudest, most extroverted, most confident personalities. A more diverse group of people will provide more diverse ideas and innovations.</li>
<li>Failure starts to be considered a wonderful thing, as even a single failure is an opportunity for many people to learn and improve as developers.</li>
</ul>


<p>Please leave your flames and ad hominem attacks in the comments. ;)</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Functional programming newbie and something something monad something]]></title>
    <link href="http://davesquared.net/2011/08/functional-programming-newbie-and.html"/>
    <updated>2011-08-16T01:30:00+10:00</updated>
    <id>http://davesquared.net/2011/08/functional-programming-newbie-and</id>
    <content type="html"><![CDATA[<p>Let me get one thing straight: I know absolutely nothing about monads. I have never <a href="http://devhawk.net/2008/07/30/monadic-philosophy-part-2-the-linq-monad/">intentionally used something I&#8217;ve recognised as a monad</a>. I am dangerously unqualified to enhance your understanding of monads in any way. In fact <em><b>reading this may damage you and prevent you from ever learning what a monad actually is!!!</b></em></p>

<p>The first reason I&#8217;m posting anything about monads at all is that I watched one of <a href="http://www.ndc2011.no/index.aspx?cat=1070&amp;id=1300">Robert &#8220;Uncle Bob&#8221; Martin&#8217;s entertaining NDC 2011 talks</a> titled &#8220;WTF is a monad&#8221; (<a href="http://www.ndc2011.no/index.aspx?id=361621&amp;cat=1069">video available from the NDC site</a>). I&#8217;m unsure how approximated or mathematically correct he was intending the presentation to be, but I found it really interesting and was able to implement something I can only hope was vaguely monadic based on my interpretation of the information he presented. So I thought I&#8217;d share it with you in case you could correct me (it should go without saying, but any mistakes here are mine and have nothing to do with Bob or his presentation). Worst case is it gets you interested enough to look into the topic and find out all the stuff I got wrong. (<a href="https://twitter.com/#!/TheColonial/status/100725544697593859">Was that alright OJ?</a> ;))</p>

<p>The second reason is that I like writing words like monad, monadic, and monoid because for a brief, shining moment it makes me feel like a real computer scientist. This moment generally comes crashing down as soon as I realise I have no idea what any of these terms mean, but it is a good couple of milliseconds. :)</p>

<p>Did I mention I don&#8217;t know what I&#8217;m talking about? For this post especially I mean. Yes? Good, you should be safe to read on then&#8230;</p>

<h2>Something something monad something</h2>

<p>As far as I can gather, a <a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)">monad</a> is a structure that will let you use functions that take arguments of a certain type, and apply it to values from an another type (I&#8217;ll call this the <em>monadic type</em>, but I could be misusing the term). We need to be able to map back and forth between these types. Bob roughly approximates a monad to an <em>adapter</em>; a monad is a way of adapting one type to another.</p>

<p>It is the form of this adapter that makes it a monad. A monad can be expressed as two functions: the <em>unit function</em>, normally called <code>return</code> or <code>result</code>, that takes an argument of the original type and returns the monadic type; and a <code>bind</code> function that takes the monadic type and a function that works on original types. (Technically monads should also obey the <a href="http://www.haskell.org/haskellwiki/Monad_Laws">monad laws</a>. I&#8217;m sure I&#8217;ve missed other important points about them too, but let&#8217;s run with this for now.)</p>

<p>This structure has a few useful properties, mainly to do with being able to chain a sequence of functions that take arguments of the original type, then apply arguments of the monadic type to that chain. I think.</p>

<h2>A dot monad?</h2>

<p>Uncle Bob&#8217;s first example was using a monad to manipulate a dots type using functions that normally work with integers. The dots type is simply a representation of an integer using &#8216;.&#8217; characters, so 5 maps to &#8216;&#8230;..&#8217; and back again. We&#8217;d like to be able to be able to use dots with standard integer operations like <code>add</code>, so that &#8216;..&#8217; + &#8216;&#8230;&#8217; gives &#8216;&#8230;..&#8217;.</p>

<p>Let&#8217;s look at an example in Python:</p>

<pre class="brush:python">
class DotMonad:
    def result(self, i):
        return '.' * i
    def bind(self, dots, f):
        return f(len(dots))
</pre>

<div class="note"><b>Aside:</b> If you haven&#8217;t used Python before, the <code>self</code> arguments to the functions is required due to how instance methods work in Python. You can safely ignore them for this post, but if you know C# or Java <code>self</code> basically becomes like <code>this</code> in the context of an instance method.</div>

<p>Here our <code>result</code> function just translates integers (<code>i</code>) into dots. The <code>bind</code> function takes some dots and a function <code>f</code> that takes an integer. First it converts <code>dots</code> to integers (using the length of the string of <code>dots</code>) then calls <code>f</code> using the result.</p>

<p>This means that if we have an <code>add</code> function which takes integers, we can use our monad to adapt that function to take dots.</p>

<pre class="brush:python">
# Integer add function
def add(a, b):
    return a+b

# Monadic add function for dots
def addM(dotsA, dotsB):
    m = DotMonad()
    return m.bind(dotsA, 
        lambda a: m.bind(dotsB, 
        lambda b: m.result(a+b)
        )
    )
</pre>


<p>I&#8217;ve used <code>a</code> and <code>b</code> as the plain integer types, and <code>dotsA</code> and <code>dotsB</code> to represent our monadic dots type. We can now call <code>addM('..', '...')</code> and get <code>'.....'</code>.</p>

<p>So how&#8217;s this work? Well remember that <code>bind</code> takes a dot for a first argument, and calls the function provided as a second argument after converting the dot to an <code>int</code>. The function we provide will be called with <code>dotsA</code> converted to integer <code>a</code>, then recursively call <code>bind</code> to convert <code>dotsB</code> in the same way. The last function in the chain is to the monad&#8217;s <code>result</code> method which will convert the result of <code>a+b</code> back to dots.</p>

<p>Let&#8217;s expand out and trace through the <code>addM('..', '...')</code> example to make sure we&#8217;ve got a handle on this:</p>

<pre class="brush:python">
return m.bind(dotsA,    # dotsA is '..', which is converted to int and passed to fn in 2nd arg
    lambda a:           # bind calls function with a = len('..'), which is 2 
        m.bind(dotsB,   # dotsB is '...', which is converted to int and passed to fn in 2nd arg
    lambda b:           # 2nd bind calls function with b = len('...'), which is 3 
        m.result(a+b)   # a+b is 2+3=5. m.result converts this back to '.....'
)
</pre>

<p>I think this is called <a href="http://www.haskell.org/haskellwiki/Lifting">lifting</a> the add (<code>+</code>) function to work with our monad.</p>

<h2>Lifting functions using monads</h2>

<p>So we&#8217;ve now got a version of the basic integer <code>add</code> function that can work with our monadic dots type. But we&#8217;d like to be able to apply all integer functions to work with dots. In fact, we can generalise our <code>addM</code> function from before to lift any function which takes two arguments using a monad that can bind to that function&#8217;s argument type .</p>

<div class="note"><b>Aside:</b> We could also generalise to support functions with any number of arguments, but I&#8217;m struggling to keep up as it is. :\ :)</div>


<p></p>

<pre class="brush:python">
def liftm(m, op):
    return lambda a,b: m.bind(a,
            lambda ax: m.bind(b,
            lambda bx: m.result(op(ax, bx))
            )
    )
</pre>


<p>This is pretty much identical to our <code>addM</code> function, but we can now do some neat stuff. Let&#8217;s import some standard Python operators and dot-erise them:</p>

<pre class="brush:python">
import operator

addM = liftm(DotMonad(), operator.add)
subM = liftm(DotMonad(), operator.sub)
divM = liftm(DotMonad(), operator.div)
mulM = liftm(DotMonad(), operator.mul)

#Interactive python session
&gt;&gt;&gt; addM('..', '.')
'...'
&gt;&gt;&gt; subM('....', '...')
'.'
&gt;&gt;&gt; divM(mulM('..', '...'), subM('...', '.'))
'...'
</pre>


<h2>Should we try again? Maybe&#8230;</h2>

<p>Let&#8217;s try another monad (again, from one Bob showed in his talk). This time we&#8217;re going to try and represent a type that can either have or be missing a value as a monadic type. So something very similar to .NET&#8217;s <em>nullable types</em>, <code>Nullable&lt;T&gt;</code>. The difference with the monadic form is that, because of the way we chain sequences of <code>bind</code> operations, we can actually perform operations involving missing values without throwing null reference exceptions everywhere.</p>

<pre class="brush:python">
class MaybeMonad:
    def result(self, x):
        return x
    def bind(self, maybe, f):
        if (maybe is None):
            return None
        else:
            return f(maybe)
</pre>


<p>Here our <code>result</code> function just returns whatever value it is given. If it has a value it will return that value; otherwise it will return <code>None</code> (Python&#8217;s <code>null</code> or <code>nil</code> value).</p>

<p>Now we can lift our standard operators to work with our <code>Maybe</code> type:</p>

<pre class="brush:python">
&gt;&gt;&gt; addm = liftm(MaybeMonad(), operator.add)
&gt;&gt;&gt; mulm = liftm(MaybeMonad(), operator.mul)
&gt;&gt;&gt; addm(2, 3)
5
&gt;&gt;&gt; addm(4, None)
&gt;&gt;&gt; mulm(6, 7)
42
&gt;&gt;&gt; mulm(None, None)
</pre>


<p>Or we can lift null-safe versions of other functions:</p>

<pre class="brush:python">
def string_lens(a, b):
    return len(a) + len(b)

#Interactive python session
&gt;&gt;&gt; string_lens("Hello", "World")
10
&gt;&gt;&gt; string_lens("Hello", None)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 2, in string_lens
TypeError: object of type 'NoneType' has no len()

&gt;&gt;&gt; safe = liftm(MaybeMonad(), string_lens)
&gt;&gt;&gt; safe("Hello", "World")
10
&gt;&gt;&gt; safe("Hello", None)
</pre>


<p>Here <code>string_lens</code> throws when we pass in <code>None</code>, but our <code>safe</code> lifted version takes them in its stride.</p>

<h2>Real-world monads</h2>

<p>Monads can actually be spotted out in the wild. They particularly enjoy frolicking with pure functional languages, where they can be used for (among other things) getting around the pesky limitation of not allowing side-effects in functions. Mutable state can be simulated by passing a State monad between functions. The I/O monad is used to encapsulate the side-effects of reading and writing from input and output.</p>

<p>Reading through the <a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#Examples">examples in the Wikipedia entry</a> shows some collections can even be regarded as monads (for example, <code>result</code> can return a list from a single item, <code>bind</code> can map a function to each element in a list). In some instances <a href="http://tomasp.net/blog/idioms-in-linq.aspx">LINQ statements can also be used as monads</a>. I&#8217;ve even seen <a href="http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/">JQuery accused on monadishness</a> (yes, I just made up a word).</p>

<p>So where&#8217;s this leave us? If you&#8217;re like me: dazed, confused, craving a cup of tea, and also quite eager to resume working through the excellent <a href="http://learnyouahaskell.com/">Learn you a Haskell</a> tutorial. :)</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Technical debt]]></title>
    <link href="http://davesquared.net/2011/08/technical-debt.html"/>
    <updated>2011-08-12T23:20:00+10:00</updated>
    <id>http://davesquared.net/2011/08/technical-debt</id>
    <content type="html"><![CDATA[<div class="note"><b>tl;dr</b>: Every design decision creates technical debt. Trying to come up with the purest, best design can in some cases lead to more debt than seemingly less-ideal, lower impact changes. It can help to keep this in mind while making design decisions; today&#8217;s optimal abstraction may cause much more pain long term than the quicker, less intrusive fix.
</div>

<p>I&#8217;ve started listening to the excellent <a href="http://rubyrogues.com/">Ruby Rogues</a> podcast recently, and the recent <a href="http://rubyrogues.com/technical-debt/">episode on technical debt</a> really got me thinking about the topic.</p>

<h2>The technical debt metaphor</h2>

<p>The <a href="http://www.martinfowler.com/bliki/TechnicalDebt.html">technical debt metaphor</a> has traditionally been used to explain the impact of using hacky approaches in software development. We can go into debt by trading some quality (be it design purity, code cleanliness, testing, documentation etc.) for a temporary boost in speed.</p>

<p>The metaphor points out that, like financial debt, technical debt also accrues interest. By implementing something in a sub-optimal way we are not simply deferring a fixed time period it would have taken to do the work properly in the first place; that time will increase the longer we leave the hack in and become more dependent on it, or have to work around it in the rest of the code. This means if we accrue too much debt then our project becomes bankrupt &#8211; the quality comes so low we can no longer work effectively with the codebase, and the project grinds to a stand still.</p>

<p>The conclusion of the metaphor is that we can take on small amounts of technical debt as required to meet deadlines or other constraints, but we need to keep the debt at a manageable level by regularly paying it back with refactoring and rework to keep the project progressing well longer term.</p>

<h2>Inadvertent debt</h2>

<p>Martin Fowler points out that technical debt can be accrued both <a href="http://www.martinfowler.com/bliki/TechnicalDebtQuadrant.html">deliberately and inadvertently</a>. Most of the time we just don&#8217;t know the right approach in advance, and so end up using sub-optimal solutions that accrue interest and slow us down.</p>

<blockquote><p>&#8220;The moment you realize what the design should have been, you also realize that you have an inadvertent debt&#8230; My view is this kind of debt is inevitable and thus should be expected. Even the best teams will have debt to deal with as a project goes on - even more reason not to recklessly overload it with crummy code.&#8221;<br/>
<i>&#8211; Martin Fowler, from his post on <a href="http://www.martinfowler.com/bliki/TechnicalDebtQuadrant.html">The Technical Debt Quadrant</a></i></p></blockquote>

<p>After thinking about this some more I&#8217;ve come to the opinion that this doesn&#8217;t quite go far enough. We may decide another design may have worked better, but until we&#8217;ve done it we don&#8217;t really know for sure. The new design will have its own share of problems. It might even be considerably better, but there is more than likely another, even better solution that will become apparent once we learn from the mistakes of the second attempt.</p>

<p>One may even make the outrageous assertion that there is no perfect design for any problem, there are only different sets of trade offs.</p>

<h2>Would you like a silver bullet with your free lunch?</h2>

<p>Wow, so <a href="http://en.wikipedia.org/wiki/No_Silver_Bullet">there is no silver bullet</a>, huh? Bet you&#8217;ve never heard that before. ;) While you&#8217;re finishing up sniggering something about me and &#8221;<a href="http://en.wiktionary.org/wiki/Captain_Obvious">Captain Obvious</a>&#8221; into your RSS reader, let me try and explain why I find this is a very helpful realisation in the context of technical debt. :)</p>

<p>If we acknowledge there is no perfect design, then every design decision we make is incurring technical debt that we&#8217;ll need to pay off. This means that if we try and optimise all our decisions for what appears to be the ideal design, we are quite likely to end up taking longer to strive for perfect when the outcome will be sub-optimal anyway.</p>

<p>That elegant object hierarchy, pattern, or great abstraction added to the application architecture that removes lots of duplication and provides a huge potential for future reuse may seem like the lowest debt solution, but it will not be completely debt free. It will accrue interest and end up costing us as the broad generalisation it encapsulates becomes less applicable as development continues. In my experience the larger the change and investment in that change, the greater the interest rate tends to be.</p>

<p>Rather than looking for the ideal, we may be better off favouring decisions that are quick to implement and reversible/easy to change. Low impact, loosely-coupled changes (i.e. ones that require minimal amounts of changes to existing code) that at first seem less ideal maybe actually give us less debt overall. They may not perfectly abstract away the details of the current problem, but they will be easy to change or remove (along with the debt they contribute) as the needs of the project change or become better understood. Instead of picking the perfect fit for current requirements, we can focus on trying to pick the options that let us iterate and adapt to future requirements.</p>

<p>This kind of thinking can lead us to picking options that at first seem counter-intuitive but end up proving remarkably useful, such as using loosely-grouped classes over strongly-layered architectures, Commands to model database calls via a micro-ORM over persisting large object graphs via full-fledged ORM, using a NoSQL data store rather than an RDBMS, or picking a Front Controller pattern or MVC for a web app over an abstraction like WebForms.</p>

<h2>&#8220;Right&#8221; sometimes isn&#8217;t</h2>

<p>At least for the developers I talk to, there seems to be a lot of self-imposed pressure for us to pick the &#8220;right&#8221; design when faced with a design choice; the choice that seems to perfectly abstract the current requirement. The right choice seldom seems the quick, easy option, but is generally one that requires a large amount of work, much of it for the benefit that it looks pure and elegant from an OO and a architectural point of view. I hear (and have made) comments like &#8220;That&#8217;s a bit ugly. We should extract a common interface, add a factory and <em>(insert lots more work here)</em>&#8221;. It&#8217;s as if taking the easy option makes us lazy, bad people; that we are somehow failing to live up to the ideals of <a href="http://manifesto.softwarecraftsmanship.org/">software craftsmanship</a>.</p>

<p>I think our efforts to do the right thing in these cases can mean we inadvertently do a worse job overall; we are optimising for our current needs but going into debt by borrowing from our future needs. Our decision quickly racks up a huge amount of interest that we have to fight every time we need to make a change that does not quite match the previous requirements. The cost quickly outstrips the smaller, more modular change that would have been long gone, quickly refactored away the moment requirements started to push the code in a new direction. It seems like a good example of <a href="http://en.wikipedia.org/wiki/Perfect_is_the_enemy_of_good">perfect being the enemy of the good</a>.</p>

<h2>Conclusion</h2>

<p>This is not intended as an excuse for hacky code. I am talking about choosing between changes that are made with appropriate tests, taking SOLID principles and good programming practices into account, etc. My point is that the common way of thinking about technical debt can lead us to optimise for the wrong things. Rather than aiming for a beautiful OO abstraction that seems a zero-debt choice, we should acknowledge all our design decisions will incur debt and accrue interest, and so also look for low-impact, reversible changes that will add a smaller debt burden. It&#8217;s not a question of whether to take on debt or not; it&#8217;s how much to take and where from.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Bash newbie starts to learn his shell for git and profit]]></title>
    <link href="http://davesquared.net/2011/07/bash-newbie-starts-to-learn-his-shell.html"/>
    <updated>2011-07-27T22:23:00+10:00</updated>
    <id>http://davesquared.net/2011/07/bash-newbie-starts-to-learn-his-shell</id>
    <content type="html"><![CDATA[<p>Since I&#8217;ve started using git I do a lot more work in the bash console for Windows it installs with, and recently I&#8217;ve been trying to lean a bit more on bash to make my life easier. Here&#8217;s a couple of basic commands to help use git (et al.) from the command line. It will all be old news for people that have used bash in anger before, but it might help those who, like me, have not yet found the time to really start trying to learn it.</p>

<h2>Find</h2>

<p>One problem I&#8217;ve had is specifying files to pass to git commands when they&#8217;re defined somewhere deep within my solution&#8217;s folder structure. One option is to use the <code>find</code> command for this:</p>

<pre>git gui blame `find . -name MyFile.cs`
</pre>

<p>This will evaluate <code>find . -name MyFile.cs</code>, which will recursively search from the current path for a file with that name (use <code>-iname</code> for case-insensitive matching). Assuming there is only one match, this works great. If we want to keep operating on that file we can store it in a variable:</p>

<pre>f=`find . -name MyFile.cs`
echo $f #check which files we matched. Good idea before destructive cmds ;)
git annotate $f
git gui blame $f
</pre>

<h2>Loops and filtering</h2>

<p>If we want to match multiple files, we can iterate over the output of <code>find</code>:</p>

<pre>for match in `find . -name *File.cs`; do git gui blame $match; done

# Same thing, but in multiple steps and expanding the loop over multiple lines
# to show what's happening:
$matches=`find . -name *File.cs`
for match in $matches
do 
    git gui blame $match
done
</pre>

<p>We can also do filtering within this loop. The following example deletes all WIP branches: (<em>use with caution!</em>):</p>

<pre>for b in `git branch`; do if [[ $b == WIP* ]]; then git branch -D $b; fi; done
</pre>

<h2>xargs</h2>

<p>I always struggle to remember the loop syntax, and it&#8217;s not exactly aesthetically pleasing. There is another option: <a href="http://en.wikipedia.org/wiki/Xargs">xargs</a>. <code>Xargs</code> is used to build-up lists of argument and execute commands using those arguments. Say we want to delete all our WIP branches again (again: <em>this deletes stuff! Use with caution!</em>):</p>

<pre>git branch | grep 'WIP' | xargs -n1 git branch -D
</pre>

<p>Here we pipe the output of <code>git branch</code> through grep and just pick the branches that contain &#8216;WIP&#8217;. We then pipe this into <code>xargs</code>. The <code>-n1</code> option tells <code>xargs</code> to execute our <code>git branch -D</code> command once for each argument received (we could pass 3 args into the command using <code>-n3</code>, so the command would execute once for every 3 args). This is a more functional approach, and starts to show the strength of the Unix philosophy of composing many small utilities that all do their jobs well.</p>

<p>I&#8217;m only just beginning to get to grips with <code>xargs</code>, but if you read through <a href="http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/">Things you (probably) didn&#8217;t know about xargs</a> and <a href="http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/">xargs: How to control and use command line arguments</a> you should get more of an idea of what it can do.</p>

<h2>Get to know your shell</h2>

<p>I recall <a href="http://pragprog.com/the-pragmatic-programmer">The Pragmatic Programmer</a> advising to learn a shell really well to help you get more productive. While I&#8217;ve only just started looking at this, the more I look the more doors seem to open for automation and getting trivial little operations done more quickly (and importantly, without causing much of a mental context switch from what you are currently working on). Whether you use bash, zsh, PowerShell or even lowly old cmd, I&#8217;d really recommend getting better acquainted with what your shell has to offer.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Abstraction and OO]]></title>
    <link href="http://davesquared.net/2011/06/abstraction-and-oo.html"/>
    <updated>2011-06-09T14:25:00+10:00</updated>
    <id>http://davesquared.net/2011/06/abstraction-and-oo</id>
    <content type="html"><![CDATA[<p>One observation I&#8217;ve heard when people start working with OO (especially when shown something silly like <a href="http://davesquared.net/2010/09/wrap-up-from-tdd-and-ooo-at-sydney.html">OOO</a>) is that all the abstraction and indirection makes it difficult to get a clear picture of all the execution paths through the code. A colleague of mine (not a dev, but someone with a fair amount of programming experience) once told me OO makes it difficult to be able to hold the entire program in your head at once. And this is certainly true to an extent, because it isn&#8217;t what OO is for. For this post I wanted to provide a quick overview of how I think about OO and abstractions.</p>

<h2>Understanding procedural code</h2>

<p>For many of us our first introduction to programming is procedural code. We use variables with different scopes, conditionals and program flow operators like <code>if</code>, <code>while</code> and <code>for</code>, as well as function calls to jump around. This makes it fairly easy and natural for us to trace through program execution by stepping through the code. Now you quickly get to a level of complexity where you can no longer hold the entire program in your head, but there is a certain sense of reassurance that we could start from the beginning of the code, and step our way through to the end to understand the code.</p>

<p>By comparison OO can seem like you&#8217;re drowning in a sea of indirection. You no longer just step through the functions being called, but you also need to understand the state of the objects those functions live in, what types are in use due to polymorphism, or even which method will be called once virtual method dispatch is taken into account. For example, you may try and trace the execution of an <code>IFoo.DoSomething()</code> call, only to find you have no idea which implementation of <code>IFoo</code> is being used. Maybe it&#8217;s a <code>CompositeFoo</code> which aggregates a <code>WidgetFoo</code> and a <code>GadgetFoo</code>, and <code>GadgetFoo</code> may have an instance of an <code>IAmAtABar</code>, which will completely change what it&#8217;s <code>DoSomething()</code> method does. Surely this OO is a horrible beast to be avoided at all costs! (Cue functional programmers nodding in agreement ;))</p>

<h2>Hiding details is the point of abstraction</h2>

<p>This is not a problem with OO. This is the very point of OO. OO allows us to abstract away details so we only deal with a cohesive, understandable amount of information relevant to the abstraction level at which we&#8217;re working. It&#8217;s ok not to understand the whole thing at once. I&#8217;m pretty sure you&#8217;re not meant to.</p>

<p>Rather than tracing through a procedural program from top to bottom, for OO programs we move sideways along a plane of abstraction to find the collaborators at that level. Our <code>ProcessOrderCommand</code> calls <code>BillCustomerCommand</code> and <code>ShipInventoryCommand</code>. I don&#8217;t know that <code>BillCustomerCommand</code> checks the customer is in our loyalty program and this order qualifies for a 10% discount. That&#8217;s a different level of detail that lives at another level of abstraction. All these little details are mercifully hidden so we can understand that processing an order means billing a customer and shipping some inventory to them.</p>

<p>Holding all the combinations of state at each level of abstraction in our heads becomes a near impossibility, but we don&#8217;t need it. That&#8217;s what our abstraction is for; encapsulating all the details and freeing us to work at the optimum level of abstraction for our current problem. We can then switch between levels of abstraction to get the information relevant to the problem we&#8217;re trying to solve.</p>

<h2>How can obscuring details be a good thing?</h2>

<p>We pay a price for the traceability of procedural code. It tends to be hard to change because the code is all about implementation; the &#8220;how&#8221; rather than the &#8220;what&#8221; or &#8220;why&#8221;.  This can also make it hard to test, because isolating a section of code from the execution state is difficult, which makes it even harder to change with confidence.</p>

<p>OO trades of some of this traceability for the ability to use abstractions in the form of objects in our code. By hiding details at one level we can better see the main features of another; we can <a href="http://en.wiktionary.org/wiki/see_the_forest_for_the_trees">see the forest for the trees</a>. Abstractions also let us express the &#8220;what&#8221; and &#8220;why&#8221; of the code. Because we&#8217;re programming to abstractions we can potentially change the details those abstractions encapsulate without affecting the rest of the system. In fact we can modify the behaviour of our system just by adding objects, rather than modifying existing code (see <a href="http://davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html">Open Closed Principle of SOLID</a>). This encapsulation of details also lets us isolate small units for testing purposes.</p>

<h2>Abstraction can be painful</h2>

<p>Now it&#8217;s important to realise there are costs to the OO approach. We&#8217;ve talked about losing some of the ease with which we could trace through procedural code. This can also make concurrency difficult when we need to synchronise bits of state in different places. There can also be problems with the <a href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch">impedance mismatch</a> between abstract concepts and technical implementation. A related problem is that of <a href="http://en.wikipedia.org/wiki/Leaky_abstraction">leaky abstractions</a>, where the encapsulation we&#8217;ve chosen breaks down in places and affects other parts of this system, increasing coupling and actually making the code harder to change (the opposite effect of what OO is designed for).</p>

<p>While we can mitigate against these problems, it&#8217;s worth acknowledging that we will experience some level of pain from all these issues when using OO. OO gives us a lot, but there&#8217;s no such thing as a free lunch. This is, incidentally, a great reason to <a href="http://davesquared.net/2011/01/finding-functional-neatness-with.html">look at other programming paradigms</a>, as well as different frameworks and languages that all address these issues in different ways and to differing extents. Combining techniques (such as functional and object-oriented) can help give you some of the best of all worlds.</p>

<h2>Getting the most from abstractions</h2>

<p>There is a whole lot of design guidance that can help us with OO abstractions. <a href="http://davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html">SOLID</a>, the <a href="http://c2.com/cgi/wiki?XpSimplicityRules">4 rules of simple design</a>, <a href="http://davesquared.net/2010/02/what-exactly-is-tdd-driving.html">TDD</a> and related disciplines, <a href="http://en.wikipedia.org/wiki/GRASP_(object-oriented_design">GRASP</a>, etc. I really recommend looking into all that stuff, but for this post I want to look at it from the more general viewpoint of what we want to get out of the abstractions (read: rant).</p>

<div class="note"><b>Note: </b> Just to clarify, when I talk about abstractions here, I&#8217;m referring to an object or group of objects that encapsulate some part of your system. We&#8217;re really talking OO design, but in the general terms of abstraction.</div>


<ul>
<li>Don&#8217;t mix levels of abstraction. Each piece of an abstraction should be at a similar level of detail.</li>
<li>Abstractions are lots of work. Don&#8217;t have one if you&#8217;re not willing to look after it. You will need to nurture it and help it grow into a useful member of your design society. Corollary: don&#8217;t use too many abstractions. They should be small and cohesive. The aim is to do more with less code.</li>
<li>Define abstractions around things that need to change together. If you need to add lots of views to your app, writing a new view should not require changing bits of 7 different abstractions. Similarly, if you are only going to be using SQL Server, you don&#8217;t need to abstract that fact away so you can plug in a new DB engine (although your data access details will probably live at a similar level of abstraction, so it won&#8217;t be impossible to change either). Optimise for the things that change all the time. One big class that never needs to change is preferable to 30 tiny classes that all need to change all the time.</li>
<li>Favour wide abstractions over deep ones. In other words, favour aggregating/composing several collaborators, rather than having many layers of objects (<code>A</code> uses <code>B</code> uses <code>C</code> uses &#8230; <code>Z</code>). Having to traverse many layers of abstraction down a deep hierarchy to pass one new piece of data is soul crushing.</li>
<li>Keep data close to where it is used. Having to pass the same piece of data through many abstractions is also soul crushing.</li>
<li>Don&#8217;t hide the important stuff. You want to abstract away the unnecessary details, not the key feature of what you&#8217;re working on.</li>
<li>Obey the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>. She will try and tell you when your abstractions are leaking. You can then fix these leaks by applying the <a href="http://pragprog.com/articles/tell-dont-ask">Tell, Don&#8217;t Ask principle</a>.</li>
<li>Tests can be another good guide to tell you when your abstractions are going wrong. If you need to do loads of setup or reach into lots of different collaborators then your abstraction is wrong. Unfortunately it doesn&#8217;t tell you how to do it right, but if you write the test you wish you had first, then you have a better chance of getting the abstraction you need.</li>
<li>Avoid creating abstractions for testability alone. A well abstracted design should be naturally testable, but not all testable designs are well abstracted. The <a href="http://www.codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle (RAP)</a> tells us to look for valuable abstractions. Just <a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx">testing against an interface does not mean we have a good abstraction</a>. (Thanks to <a href="http://twitter.com/xerxesb">Xerx</a> for pointing this out.)</li>
<li>Modelling the real world as objects is probably not what you&#8217;re after. Read Uncle Bob&#8217;s Coffee Maker example (<a href="http://davesquared.net/2007/09/free-articles-every-developer-should.html">linked to here</a>). (Aside: this doesn&#8217;t conflict with the goals of DDD, which works to accurately reflecting business needs and concepts.)</li>
<li>Layered / n-tier architectures do not necessarily help with abstractions. If you need to force your classes into presentation, business logic and data access layers, then your abstractions are not free to grow or (more importantly) shrink as required. Maybe the abstractions you choose will naturally group together into layers, or maybe each abstraction will have its own layers. I think forcing it can be detrimental though. If you want to change how you are loading data for a screen (say, optimised query instead of what your ORM generates), you don&#8217;t want to fight the weight of every other data access abstraction in the code.</li>
<li>Separate infrastructure code concerns from app-specific abstractions. Abstractions should be <a href="http://en.wikipedia.org/wiki/Modular_(programming">modular</a>), the infrastructure can compose them together. An example of this infrastructure is your friendly neighbourhood IoC container. (We also want to keep infrastructure small and prevent it bleeding into our abstractions; again, less is more.)</li>
<li>Ideally you want to be able to be able to try out new abstractions without fighting against the existing ones and without breaking too many conventions. Admittedly I have not found a good solution for this, but keep it in mind when designing and consider how much work it would be to do this differently if required for another feature.</li>
</ul>


<p>Finally, remember there is no single right abstraction for a given problem. This still seems more art than science, and practice and experience will trump all the rules and guidelines you can find. The trick is finding ways of experimenting productively with different options in both real and hobby projects.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Random software dev thoughts (June 2011 edition)]]></title>
    <link href="http://davesquared.net/2011/06/random-software-dev-thoughts-june-2011.html"/>
    <updated>2011-06-03T21:13:00+10:00</updated>
    <id>http://davesquared.net/2011/06/random-software-dev-thoughts-june-2011</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been struggling with a few thoughts on software development lately, and <a href="https://twitter.com/davetchepak/status/76402646948380674">said as much</a> on Twitter. <a href="http://twitter.com/thecolonial">OJ</a> suggested I blog it, so here goes a rambling, incoherent post (even more so than usual). Entirely OJ&#8217;s fault. ;)</p>

<h2>Abstraction</h2>

<p>Back in the old days I spent more time than I care to admit writing procedural code in classes. More recently I&#8217;ve also played around with <a href="http://davesquared.net/2010/09/wrap-up-from-tdd-and-ooo-at-sydney.html">Over-enthusiastic OO (OOO)</a>, using insanely fine-grained objects for everything. These approaches roughly correspond to not enough and too much abstraction.</p>

<p>And so begins my hunt for the right amount of abstraction. I find fine-grained objects with single responsibilities really nice to work with at a micro level, but at a macro level the weight of useless abstractions can be crushing.</p>

<p>This weight is a symptom of violating the <a href="http://www.codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle (RAP)</a>, which Mark <a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx">explains very nicely</a>. (I&#8217;ve also experienced this as the NAF problem, or &#8220;Not Another Factory&#8221;. Anyone else felt this?) The trick is finding the &#8220;right&#8221; abstraction that captures a reusable aspect of the system. This is made harder in C# by the fact(?) that it makes effective unit testing difficult without creating abstractions everywhere.</p>

<p>So my options include hoping to drive out the right abstractions and assume that this will magically make it easy to test without using otherwise useless abstractions, or starting to use more real stuff in unit tests (having larger units, with potentially more suitable abstractions), in which case my tests get messier as I <a href="http://davesquared.net/2009/11/favour-test-driving-logic-over-data.html">throw loads of data through my tests</a> to exercise all the code paths.</p>

<p>One thing I&#8217;ll definitely rule out is going back to procedural code where classes are little more than a namespace, as I found this quickly became unmanageable; difficult to change, impossible to test effectively, and a nightmare to build on. At least my poorly chosen, testable abstractions are better than untestable spaghetti.</p>

<h2>Refactoring</h2>

<p>Another thing I&#8217;m struggling with is refactoring. I find the distinction between <a href="http://davesquared.net/2010/02/refactor-or-redesign.html">refactoring and redesign</a> helps, but it&#8217;s not always quite that simple. Sometimes despite refactoring the little section of the code you&#8217;re currently working in and around, the tendrils of previous design decisions still reach out across many other classes. Once you have coupling like this, when can you ever clean it up by refactoring alone?</p>

<p>Contrived example: if you have ye olde layered architecture and are passing data (or transformations of the same data) through the layers and through multiple classes, untangling all the places this data has spread can only really be tackled in a redesign. And, as described in my original post, once you are in the territory of redesign you are optimising for today&#8217;s cases and possibly making your next change harder if it tries to flex in a different direction. On the other hand, the overall design is getting increasingly bloated and harder to understand as you continue to build on it while making localised design changes using refactoring.</p>

<p>Is it possible to gradually refactor even these wide-spread problems? Or are there times when you just have to bite the bullet and redesign? Is it just a case of writing off the redesign time that could otherwise be spent on features your customers care about, in the hope it will speed you up later? Or is it a case of admitting defeat; the design can actually become too compromised to completely fix if we miss too many cues to refactor, and we just have to go into legacy code mode and keep making local improvements in the hope that these isolated changes will eventually link up and restore a fairly clean, usable design?</p>

<h2>End rant</h2>

<p>So those are the problems that are currently weighing on my mind.</p>

<p>On one hand, I can see an incredible amount of value in using very fine abstractions, but on the other abstractions can quickly turn into a dead weight that impedes progress. I&#8217;m torn between continuing to get the benefit of lots of small classes, and trying to reduce the amount of weight and noise contributed by the less-useful of my abstractions.</p>

<p>On the refactoring side, I&#8217;ve advocated small, localised refactoring in the hope of gradually pushing a design into good shape, but I&#8217;ve also observed times when this just doesn&#8217;t seem possible. In these cases the only options seem redesigning larger areas of code, or just capitulating to entropy and keeping small islands of cleanliness within the mess (this is in keeping with Eric Evans&#8217; somewhat depressing observation that &#8220;the natural state of all software is a big ball of mud&#8221;). The problem here is trying to decide what approach to take, and balancing time invested now versus the future cost of not making that investment. I&#8217;m also bothered by the idea that the only evidence we have to base this decision on is poorly-informed guesswork and gambling based on that.</p>

<p>Experience over multiple projects would most definitely help this, but as the real pain of this stuff seems to be felt in large-ish, multi-year projects, this experience and chances to experiment with different approaches is hard to come by.</p>

<p>As a quick aside, I&#8217;m pretty sure these two problems I&#8217;m having are closely related.</p>

<p>Appreciate any thoughts or counselling on offer. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[An Rx newbie observes property changes]]></title>
    <link href="http://davesquared.net/2011/05/rx-newbie-observes-property-changes.html"/>
    <updated>2011-05-21T00:08:00+10:00</updated>
    <id>http://davesquared.net/2011/05/rx-newbie-observes-property-changes</id>
    <content type="html"><![CDATA[<p>I recently started taking some tentative steps into <a href="http://msdn.microsoft.com/en-us/data/gg577609">Reactive Extensions (Rx)</a> (yes, I realise I&#8217;m way behind on this). The case I started off with was observing a property, and performing an action only when it changed to specific values.</p>

<pre class="brush: csharp">
public interface IProperty&lt;T&gt; {
    T Value { get; set; }
    event Action ValueChanged;
}
</pre>


<p>The traditional way of doing this in .NET is to subscribe to events:</p>

<pre class="brush: csharp">
    [Test]
    public void WithEvents() {
        _property.ValueChanged += OnValueChanged;

        _property.Value = 5;
        _property.Value = 10;
        _property.Value = 15;
        _property.Value = 2;

        _property.ValueChanged -= OnValueChanged;
        Assert.That(_propertyValues.ToArray(), Is.EqualTo(new[] { 10, 15 }));
    }

    private void OnValueChanged() {
        var value = _property.Value;
        if (value &gt;= 10) {
            _propertyValues.Add(value);
        }
    }
</pre>


<p>Here we subscribe to the <code>ValueChanged</code> event. Every time the event fires our <code>OnValueChanged()</code> method will be called, which will check if the property value meets our condition, and if so then add it to <code>_propertyValues</code> (a list of <code>int</code>).</p>

<p>The other alternative is to change from looking at this situation as the procedure of responding to each event by looking up the <code>Value</code>, to looking at the changes as a collection of <code>Values</code> produced by the events. This is where Rx comes in. (At least this is my newbie interpretation; please correct me in the comments.)</p>

<h2>Everything I know about Rx in under 200 words</h2>

<p>Rx seems to revolve around 2 main interfaces (that I believe shipped with .NET4, and are also available from Rx for .NET 3.5). The first is <code>IObserver&lt;T&gt;</code>, which has <code>OnNext&lt;T&gt;(T item)</code>, <code>OnError(Exception error)</code> and <code>OnCompleted()</code>. This interface is the <a href="http://headinthebox.posterous.com/subjectobserver-is-dual-to-iterator">dual of the iterator interface</a> (<code>IEnumerator&lt;T&gt;</code>), which just means that rather than iterating over a collection, we get each member of the collection pushed to us.</p>

<p>The other interface is <code>IObservable&lt;T&gt;</code>, which just has a <code>Subscribe&lt;T&gt;(IObserver&lt;T&gt; observer)</code> method that returns an <code>IDisposable</code>. This lets us register an observer to start receiving pushed items, and dispose of the observable to clean up subscriptions.</p>

<p>Rx itself is a whole heap of supporting implementations and extension methods to help us do incredibly cool things with those two interfaces<b>*</b>. Rather than looking at these incredibly cool things, let&#8217;s look at our previous example instead, partly because the cool things are covered in many places, and partly because this example is adapted from a real problem I had to solve, so I know it&#8217;s something we might be able to use in day-to-day development. :)</p>

<div class="note"><b>*</b> For a real (i.e. factual :)) explanation, try <a href="http://leecampbell.blogspot.com/2010/08/reactive-extensions-for-net.html">Lee Campbell&#8217;s Rx series</a>.</div>

<h2>Observing the values of our changing property</h2>

<p>I initially stuffed up my effort to get this working using Rx by creating a terribly naive implementation, but thanks to <a href="http://stackoverflow.com/questions/6056004/using-net-rx-to-observe-property-changed-with-action-events/6057664#6057664">a very helpful StackOverflow answer</a> from <a href="http://weblogs.asp.net/sweinstein/">Scott Weinstein</a> it&#8217;s now looking pretty neat.</p>

<p>The first thing we need to do is create an <code>IObservable&lt;T&gt;</code> over our property value. Rx has built-in methods to do this from events, but only when they follow the standard event handler signature (whereas ours is declared as <code>event Action</code>). Instead we can use <code>Observable.Create&lt;T&gt;()</code> to help us. (We could do this in-line where we want to use it, but I&#8217;ve wrapped it in an reusable method because I&#8217;ve got lots of properties where this will come in handy.)</p>

<pre class="brush: csharp">
    public static class ObservableEx {
        public static IObservable&lt;T&gt; FromEvent&lt;T&gt;(Func&lt;T&gt; selector, Action&lt;Action&gt; subscribe, Action&lt;Action&gt; unsubscribe) {
            return Observable.Create&lt;T&gt;(obs => {
                Action pushNext = () => obs.OnNext(selector());
                subscribe(pushNext);
                return () =&gt; unsubscribe(pushNext);
            });
        }
    }
</pre>

<p>This looks a little more confusing than it actually is. <code>Observable.Create&lt;T&gt;()</code> takes a delegate which itself takes an <code>IObserver&lt;T&gt;</code> and returns an action that disposes of our observable. In the body of that delegate we put the logic necessary to push values to the observer. This is the subscription logic for any observer that subscribes to our observable.</p>

<div class="note"><b>Aside:</b> If we want to push the initial state of the value to observers when they subscribe, we can add an invocation of `pushNext()` after `subscribe(pushNext)`. This will ensure our collection always has a starting value.</div>

<p>In this case we push whatever value is returned by our <code>selector</code>, and subscribe to the required event so we push whenever that event is raised. When our observable is disposed we unsubscribe. And here&#8217;s how we use it:</p>

<pre class="brush: csharp">
    [Test]
    public void WithObservable() {
        ObservableEx.FromEvent(
                () =&gt; _property.Value, 
                action =&gt; _property.ValueChanged += action, 
                action =&gt; _property.ValueChanged -= action)
            .Where(x =&gt; x &gt;= 10)
            .Subscribe(_propertyValues.Add);

        _property.Value = 5;
        _property.Value = 10;
        _property.Value = 15;
        _property.Value = 2;

        Assert.That(_propertyValues.ToArray(), Is.EqualTo(new[] { 10, 15 }));
    }
</pre>


<p>This is the same test as before but rewritten to use observables. We create our observable over our property value based on its <code>ValueChanged</code> events using our extension method. We then filter this collection of values using a standard, LINQy <code>Where</code> clause. Finally we put our logic for handling occurrences of this particular observation by calling <code>Subscribe</code> and giving it a callback to execute; in this case our logic to add the value to <code>_propertyValues</code>.</p>

<p>Compared with the previous approach, we have consolidated event subscriptions, unsubscriptions, filtering the values and the logic of handling the values we&#8217;re interested in to a single observable declaration. Having all this related information together is great, but being able to declare our intention in the code rather than the procedure we need to implement it is, IMO, awesome. Not to mention how nice it is to have our event subscription cleaned up for us when our observable goes out of scope. :)</p>

<div class="note"><b>Note:</b> Whenever I&#8217;ve showed this to people the first thing they&#8217;ve asked is &#8216;how can I test this?&#8217; Normally we try and test behaviour rather than implementation, so in the case I tried this for I didn&#8217;t need to modify any tests; I just replaced event subscriptions with the observable creation and they all passed. That said, Rx has a package available to support testing, or you could mock the interfaces if, <a href="http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html">despite my warnings</a>, you still think it&#8217;s a good idea. ;)</div>

<p>This may seem like a silly example, so let&#8217;s look at a more realistic case where the same code could come in handy. Say we want to run some diagnostics whenever a device gets connected. We could observe the device&#8217;s connection state, filter it to just look at <code>Connected</code> values, then run the required diagnostics code when we get this value. Better yet, we can also combine observables, so we could subscribe to notifications only when the device is connected, its driver has reported it is ready to use, and the software is running in diagnostics mode. This would make for messy event handling code, but be quite readable as code combining and filtering observables.</p>

<p>I&#8217;m definitely keen to play around with this more, and hope I&#8217;ve managed to get those of you that have been putting it off (like me) interested enough in it to do the same. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Don't mock types you don't own]]></title>
    <link href="http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html"/>
    <updated>2011-04-28T23:57:00+10:00</updated>
    <id>http://davesquared.net/2011/04/dont-mock-types-you-dont-own</id>
    <content type="html"><![CDATA[<p>I&#8217;ve found applying the guideline* of <a href="http://stevef.truemesh.com/archives/000194.html">not mocking types you don&#8217;t own</a> to have helped my designs a fair bit recently. I&#8217;ve <a href="http://davesquared.net/2010/06/tdd-not-utdd.html">mentioned it before</a>, but I wanted to take a closer look at the topic in an attempt to improve my own understanding. And in that spirit of improving my understanding, please feel free to rip this to shreds. :)</p>

<div class="note"><b>*</b> Guideline, not a <a href="http://davesquared.net/2011/01/rules.html">rule</a>. :)</div>

<h2>But I don&#8217;t use mocks! I use stubs!</h2>

<p>Let&#8217;s start by clarifying that by &#8220;mocking&#8221; I&#8217;m referring to the <a href="http://martinfowler.com/articles/mocksArentStubs.html">mocks and stubs</a> we use as test doubles; the kind we tend to get from our mocking/substitution/fake/isolation framework of choice. By &#8220;types we don&#8217;t own&#8221; we&#8217;re talking about any type that isn&#8217;t defined and tested as part of the current build (or solution, to use Visual Studio parlance). This includes anything from types in a base class library to APIs for accessing another system or service.</p>

<h2>When mocking makes sense</h2>

<p>To me the sweet spot for mocking is when we&#8217;re using TDD to drive out the design of a collaborator that doesn&#8217;t exist yet, or one that requires modification.</p>

<p>In these cases we&#8217;re defining how the type will act, and we have the ability to adjust this behaviour as required by the type&#8217;s consumers by altering these tests. At this level our design is really quite fluid; we can shuffle responsibilities between types, and create new or collapse existing types as we see fit. We are working within our own abstractions, optimised (hopefully) for making our code easy to work with.</p>

<p>A more general form of this case is when we want to isolate a class under test from its dependencies. This allows us to configure explicit behaviour for our test doubles so we don&#8217;t need to test a whole host of input combinations; we can assume the collaborators&#8217; behaviours and just worry about the class under test. It also can shield us from long running tests by replacing slow collaborators (such as those that make database or web service calls), or from difficult test setups due to collaborators that need to run in an explicitly configured environment (such as those that make database or web service calls), or from unreliable tests due to collaborators with variable behaviour (such as those that make database or web service calls ;)).</p>

<p>However this more general case has landed me in trouble more than a few times when I&#8217;ve tried mocking (and stubbing) the interactions with types I don&#8217;t own.</p>

<h2>Tests that do little</h2>

<p>When we are writing a test as part of TDD we are primarily specifying how our type will behave, and as a result defining the behaviour of the type&#8217;s collaborators. The interactions between these types are tested as well, but the next step we&#8217;ll do is drive down into the collaborating type and make sure it will work as required. As we have full control over the types at each end of the exchange, we can be reasonably confident our real code is going to do what we expect. After all, we wrote and tested that code in accordance with how our tests told us it was going to behave.</p>

<p>For types we don&#8217;t own we don&#8217;t have this level of transparency. They already have a fixed design and behaviour specified elsewhere. By testing interactions with a mocked version of this type, we really are not using our test to check for the correct behaviour, nor to drive out a collaborator&#8217;s design. All our test is doing is reiterating our guess as to how the other type works. Sure, it&#8217;s better than no test, but not necessarily by much. Just because we have checked we successfully called <code>database.Save(data)</code> doesn&#8217;t mean that we didn&#8217;t need to call <code>database.OpenConnection()</code> first.</p>

<p>Even when we know an external library really well, and so completely understand the interactions required, mocking that library can still give us misleading tests. If we update the library version and there has been a subtle change to the required interactions, our tests can still pass but our code will fail. Just because we have always received events in a particular order from a library doesn&#8217;t mean we can assume that will always be the case, particularly if it is not documented behaviour and/or it was just a side-effect of the implementation used in previous versions.</p>

<p>These integration issues can also be a problem with types we do own, but I&#8217;ve found that because we&#8217;re driving out each step in an end-to-end behaviour, we get added transparency and more focussed abstractions that tend to make this much less common than when dealing with external types.</p>

<h2>Tests that do too much</h2>

<p>Another risk we run in specifying all the required interactions with types we don&#8217;t own is having <a href="http://xunitpatterns.com/Fragile%20Test.html#Overcoupled%20Test">overspecified tests</a>. These tests quickly become a pain point; they are hard to change because they are so coupled to the specific implementation and pattern of interactions with a type, and refactoring which doesn&#8217;t actually change the behaviour can result in tests failing.</p>

<p>This can also mean large, convoluted setups as we attempt to mimic the way the collaborating types work, which can make our tests difficult to read, understand and maintain. In general, once we find ourselves setting up complex interactions and behaviour using mocking framework constructs, chances are we&#8217;re doing it wrong.</p>

<p>This can be mitigated to some extent by the way we encapsulate the library and the setup needed for our test fixtures, but eventually we&#8217;re going to have to pay for the impedance mismatch between our abstractions and the abstractions used for the type we don&#8217;t own. Which segues neatly to our next point&#8230;</p>

<h2>Corrupted abstractions</h2>

<p>Depending on the complexity of the interactions with types we don&#8217;t own, the abstractions used for those types can leak into and influence our own abstractions. We may add abstractions just to give us enough visibility into interactions with these types to ensure we have specified all the calls we expect, rather than because they make sense to the problem we are trying to solve. This could mean a whole lot of wrappers over external types, or new factories to create specific objects required by the interactions.</p>

<p>This isn&#8217;t necessarily bad, but it does mean we&#8217;re no longer writing the code that best solves or abstracts away our current problem, and also can leave us more tightly coupled than we&#8217;d like to the library that owns the type. Because we&#8217;re not defining the behaviour in our tests, we end up coupling our design to the implementation of the external types instead.</p>

<p>Writing and maintaining these additional abstractions can also be quite expensive, especially considering the questions we&#8217;ve already raised about how useful those tests are.</p>

<p>Remember, external libraries are built to help people solve a general problem. When we design code for a project we are trying to solve specific problems. An abstraction that is handy for addressing the more general problem is not necessarily going to match the abstractions that help us.</p>

<h2>Well how do I test code that uses external types then?</h2>

<p>One way of looking at this is that we are switching from TDD-style tests with the aim of driving design to good old fashion &#8220;check this works&#8221; tests. In my experience the mindset is fairly different, as are the tests themselves.</p>

<p>One way of making this switch is to use the real types and do an integration test. Test drive our design down to a level of abstraction that is useful, then integration test over the boundary. Rather than checking our class interacts with an external type in a certain way, we check it gives us the behaviour we require.</p>

<p>If we are testing that we&#8217;re using an ORM properly, we can use the real ORM and database and check that we can load up the required details after a save operation. If we&#8217;re using <a href="http://www.castleproject.org/">Castle DynamicProxy</a>, then we can call our code that uses DynamicProxy and check the object we get back forwards to the interceptor we expect, rather than asserting we return a specific object from a mocked <code>ProxyGenerator</code> class we don&#8217;t own and whose ins and outs we may not fully understand. We&#8217;re checking the consequence of the action, rather than the details of the action itself.</p>

<p>This isn&#8217;t to say that we add a whole host of tests around a component we don&#8217;t own &#8211; we really need to have a little faith in it working as advertised otherwise it would probably be quicker to just write our own. Instead the purpose of our test should focus on getting some degree of confidence that our use of the library has the basic desired behaviour. Rather than stating we need certain interactions with the library, we can use some simple, focussed cases with the real types to sanity check assumptions we made about the required interactions.</p>

<p>I&#8217;ve found that doing this frequently gives me much cleaner tests and implementations than if I had wrapped and tested the different elements of the interactions with types from another library. Provided the abstraction I&#8217;ve ended up with in my own code is OK, testing the behaviour tends not be too arduous.</p>

<p>As an alternative (or ideally complimentary) approach, we can use acceptance tests to make sure the entire feature works as expected (i.e. not just covering the call over the integration boundary, but exercising as much of the real feature as possible). Again, we&#8217;re checking behaviour, not implementation specifics for types where we don&#8217;t have a constant, transparent view into those implementations.</p>

<h2>What about when I just can&#8217;t use the real type?</h2>

<p>There are times when using the real types is not practical. Maybe setting up an email server on each dev machine and checking the email account receives the required email is too much for our regular test suite (possibly a great test to run on a CI server or similar though). Or perhaps actually firing the doomsday device just isn&#8217;t prudent for a test case that runs several times an hour&#8230; :)</p>

<p>In these cases we can create our own fake version of the external system or library. This is probably not going to come from our mocking framework of choice; as previously mentioned, once we find ourselves setting up complex interactions and behaviour using mocking framework constructs, we&#8217;re probably doing it wrong. The important thing is to verify that our calls to the fake are compatible with the real classes, and where possible check that the behaviour matches what we expect. Martin Fowler calls these <a href="http://martinfowler.com/bliki/IntegrationContractTest.html">Integration Contract Tests</a>. The idea is that if we pass a certain range of values to our fake, the real version should also be able to handle these values. If we assume the real version behaves in a certain way and replicate that in our fake, we can write a test to try and verify that assumption.</p>

<p>In some cases we can use the real type with a different configuration as our fake. For example, we could run tests through our ORM configured to use an in-memory database. This won&#8217;t test the final hop to our target RDBMS, but it will give us some confidence that our use of the API is correct, and we can setup more comprehensive tests against the real configuration on our CI server.</p>

<h2>Limitations of integration tests</h2>

<p>It would be remiss of me not to mention <a href="http://www.jbrains.ca">J.B. Rainsberger&#8217;s</a> assertion that <a href="http://www.jbrains.ca/series/integrated-tests-are-a-scam">integration tests are a scam</a>, which is nicely <a href="http://b-speaking.blogspot.com/search/label/integration%20tests">summarised by Gabino Roche Jr</a>. I&#8217;m not proficient enough with this stuff to talk definitively on this, but I&#8217;ll give you my 2 cents, then you should go through J.B.&#8217;s work and see how you can apply it. It&#8217;s also worth looking at the <a href="http://www.growing-object-oriented-software.com/">GOOS book</a>; it has some really interesting examples of how to effectively use acceptance tests for driving development, including the need to deal with other libraries and systems.</p>

<p>My limited understanding of J.B.&#8217;s objections to integration tests is that they are not going to ensure correctness (you can&#8217;t test exhaustively), they can be brittle and difficult to write and maintain, and that they can end up as a <a href="http://blog.thecodewhisperer.com/post/1325858548/integrated-tests-are-a-scam">&#8220;self-replicating virus&#8221;</a> when used as a crutch any time a programmer can&#8217;t find a good, testable abstraction and therefore can&#8217;t unit test the code.</p>

<p>Almost without fail whenever I have mocked a type I don&#8217;t own I end up experiencing pain. Best case is I get lots of fairly useless abstractions that cover different operations within the external library&#8217;s API. Worst case is I get lots of useless abstractions that don&#8217;t work with the real API and are really difficult to change. This only leaves me with integration/acceptance-style tests for code that uses these types, so for me the question becomes one of how to write these tests effectively while trying to limit the legitimate problems J.B. has highlighted. I don&#8217;t have the answer to this, but here&#8217;s some guidelines I&#8217;ve been using:</p>

<ul>
<li>If possible, limit the amount of exposure we have to the external library. When <a href="https://github.com/nsubstitute/NSubstitute/blob/v1.0.0/Source/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs">using DynamicProxy for NSubstitute</a> we could get away with using only a very small part of the library, which meant we <a href="https://github.com/nsubstitute/NSubstitute/blob/v1.0.0/Source/NSubstitute.Specs/Proxies/CastleDynamicProxy/CastleDynamicProxyFactorySpecs.cs">had only a few things to integration test</a>.</li>
<li>The abstractions we choose are important; they can determine how much work we have to do in each interaction with the external system. If there is a big mismatch between the abstraction we&#8217;ve driven out and the external code we need to work with, it might just be a sign we need a better fit for our requirements. (e.g. maybe we should persist using a document DB instead of an RDBMS? Or maybe just throwing data on the file system would fit better?)</li>
<li>Accept we can&#8217;t test exhaustively, and focus on the most helpful cases. We generally want to assume the external stuff works as advertised, so we really just want to get some confidence that we are using it in a way that seems to give us the behaviour we expect.</li>
<li>If we keep coming up against integration bugs, adding integration tests is probably not the answer. We may need to examine ways we can push out more responsibility into unit-testable abstractions to simplify the integration.</li>
<li>Sometimes it is worth the time to develop a good, fake implementation of a complex external system. Examples include simulating external hardware, or using an in-memory database instead of an external one.</li>
<li>Make the tests easy to read and write. If you have long, complicated test setups we need to either look for better abstractions, or encapsulate the setup process in its own abstraction.</li>
</ul>


<h2>Conclusion</h2>

<p>In summary, my current thinking on the subject is that while using mocks and stubs is really beneficial when driving out the design of collaborators using TDD, it is important to identify when we need to stop test-driving and start testing. This is generally the point where we hit a type we don&#8217;t own from a library or external system.</p>

<p>Using test doubles for types we don&#8217;t own can end up with fragile tests that don&#8217;t actually test much of value, or can even compromise our design and the effectiveness of the abstractions we use. Even when mocking a library we know really well we can end up with compromised abstractions and fragile tests due to relying on implementation details or assumptions based on previous versions of the library.</p>

<p>I&#8217;ve found switching to integration and acceptance tests to test over the boundary between my code and the other types a very useful alternative, but it is important to be aware that this approach can bring its own problems, and we need to try and mitigate these when writing our tests.</p>

<p>And finally, we should remember that this is just a guideline. If we find a situation where it is going to be both safe and simpler to mock a type we don&#8217;t own, we may as well do so, but at least we&#8217;ve considered our options. :)</p>

<p>Have I got this all wrong? Please leave a comment or email me at davesquared.net. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why learning TDD is hard, and what to do about it]]></title>
    <link href="http://davesquared.net/2011/03/why-learning-tdd-is-hard-and-what-to-do.html"/>
    <updated>2011-03-21T17:42:00+11:00</updated>
    <id>http://davesquared.net/2011/03/why-learning-tdd-is-hard-and-what-to-do</id>
    <content type="html"><![CDATA[<p>I was recently asked to try some TDD coaching, which involved pairing with an insanely smart dev who was fairly new to TDD. One thing I found interesting was that many of the questions he asked were strikingly similar to the ones that tripped me up when I started out with TDD. Many of these seem to stem from incorrect assumptions about TDD, and the fact we both managed to get similar assumptions got me thinking about their source.</p>

<p>For this post we&#8217;re going to take a look at why I feel TDD is so hard to learn, at least to the point where it can be used effectively for everyday coding tasks. From there we&#8217;ll look at how and why TDD works, with the goal of giving new TDD practitioners a way to critically evaluate the introductory material they come across, and hopefully avoid some of the mistaken assumptions that can slow down the learning process. Finally we&#8217;ll look at some ways this understanding can help people with some of the questions that commonly crop up starting TDD.</p>

<h2>Why is this so hard?</h2>

<p>I think there&#8217;s a few reasons learning TDD is so difficult. Firstly, I think the hype around TDD can be quite detrimental to the learning process. Depending on which sources you read it is easy to get the impression that TDD is the only valid way of writing software, and that by following the simple steps your code will turn out gleaming with cleanliness, filled with only obvious, beautiful abstractions. In my experience, for all but the simplest of examples, this is just not true.</p>

<p>The unrealistic expectations set by the hype can cause people to become overly focused on the process, without understanding the rationale behind it. This can result in developers applying TDD in an ineffective manner. It can also become very frustrating for the leaner, as they see the process that worked so well for implementing a <code>Stack</code> leave them lost and feeling inept when trying to use it to put something simple on a web page.</p>

<p>TDD is not a substitute for thinking. It is not a replacement for design skills. It is not actually driving anything &#8211; the developer is, and so TDD will only go as well as its driver.  A more realistic view of TDD is that it is simply a tool that can be used to help you get a job done. Sure, it can be a remarkably useful and powerful tool, but in the end the goal is to get the job done*, not to start every bit of code with a failing test.</p>

<div class="note"><b>*</b> Writing shoddy code <a href="http://davesquared.net/2010/08/quality-vs-shipping.html">does not qualify as getting the job done</a>.</div>


<p>The other reason I believe TDD is hard to learn is that the TDD process itself actually gives you very little guidance as to how to practice it. When trying to learn something new a common first step is to learn a set of rules and go around applying them fairly mindlessly until enough experience is gained to know when to bend or break them. Eventually you start to see the rationale and patterns behind the rules and realise that how to apply them depends mainly on the context. Hence the infamous standard answer from experts: &#8220;it depends&#8221;.</p>

<p>Now with TDD the <a href="http://davesquared.net/2008/02/tdd-is-easy.html">rules are trivially simple</a>. In fact they are so simple they don&#8217;t give you enough guidance to mindlessly apply them until you gain enough proficiency to start achieving success with TDD. Instead you also need to know how to appropriately abstract your design, which requires in-depth knowledge of OO design, patterns, SOLID, DRY, etc. This can make TDD very hard to use effectively until some of these gaps are filled. Now TDD is an excellent way of highlighting deficiencies in these areas, but it isn&#8217;t quite so forthcoming with the answers.</p>

<h2>Understand the mechanism, then learn to apply it</h2>

<p>I think one way to make TDD easier to learn is to make a concerted effort early on to understand how it works. Sure, start with the trivial, one class kind of example to learn the basics of the red-green-refactor process, like implementing a <code>Stack</code> [<a href="http://www.theserverside.net/tt/articles/content/TDD_Chapter/ch2.pdf">PDF link</a>] or <a href="http://osherove.com/tdd-kata-1/"><code>StringCalculator</code></a>, but then stop and think about what the process is doing.</p>

<p>We start by writing a failing test. Why? Our current tests are all passing, which gives us feedback that there are no known problems with it. By writing a test to expose a deficiency we are clarifying the problem we are trying to solve. Constraining the problem and therefore the possible solutions in this way makes it easier to solve (a divide and conquer-style approach). We&#8217;re also making sure this code will be tested for both correctness and protection against regressions, but that&#8217;s not exclusive to TDD; you can get that writing any decent automated tests.</p>

<p>Most importantly we&#8217;re also sketching out a design, but rather than doing it on a whiteboard (which has its own advantages) we&#8217;re doing it in code and getting immediate feedback as to how usable it is and how easy it is to test. Let&#8217;s think about the kind of questions we need to think about in order to write this failing test:</p>

<ul>
<li>What is our System Under Test&#8217;s (SUT) responsibility? i.e. What should it do, and when should it do it?</li>
<li>What is a convenient API for making the SUT do this?</li>
<li>What does the SUT need to discharge its responsibility? (Data? Collaborators?)</li>
<li>What output or side-effects are there to observe?</li>
<li>How can we tell it worked correctly? How is &#8220;correct&#8221; defined?</li>
</ul>


<p>These are all questions that should be asked at some point when writing any code. The power of TDD is that it provides us with a convenient tool for thinking about these questions. Rather than having to go through each question in turn, resolving potentially conflicting answers as we go, TDD lets us address the more abstract question of &#8220;How can I write the next test?&#8221;, and in the process infer a solution that also addresses the more concrete questions.  This is why TDD is no better than its driver; you still need the skills and experience to answer the same questions. TDD merely gives you a tool to make it easier to think about them.</p>

<p>How exactly does this more abstract question help us? Like all useful abstractions, it gives us a way of thinking of a lot of details as a single whole. We can now tackle all those little details in a single train of thought, in a form that lets us apply both our technical skills and our creativity to finding a solution. And while tackling that single question we get the benefit of another of TDD&#8217;s strengths: rapid, accurate feedback.</p>

<p>The process of writing the test gives us all sorts of feedback. The setup is too long or complicated? We&#8217;ve probably got too many collaborators (or are violating the Law of Demeter for them) and can try encapsulating them behind a new abstraction. Too many scenarios or test fixtures on the same SUT? Our SUT probably has too many responsibilities. Too hard to isolate the behaviour we&#8217;re trying to test, or can&#8217;t find how to write an assert for the behaviour? Maybe the current API or abstraction is wrong, and needs to be broken down differently? With TDD these problems become obvious very quickly. Solving them requires design skills, but so does any other approach to development. Writing the test first gives us ample opportunity to respond to this feedback, to sketch out our design, before committing to it. There is no cheaper time to change your code than before it is written.</p>

<p>Next we make our test pass with the simplest code we can justify. Funnily enough this is the least interesting part of the TDD process. We&#8217;ve done all the hard work in specifying our behaviour, now we just take the relatively simple step of making it work. Is it too hard to get working? Then we drop back to change our test; TDD has just given us feedback that our test is forcing too big a leap. Is the implementation so trivial it has obvious flaws? TDD has just given us feedback to tell us what our next test should expose.</p>

<p>Finally, we have the refactoring step. Our code works, but we&#8217;ve been focussed on passing a very specific test which only shows a very small picture of the application. Now is our chance to <a href="http://davesquared.net/2010/05/on-design-and-missing-forest-for-trees.html">zoom out and take in the entire application</a>. If we&#8217;ve  used a naive implementation to get our tests passing we can clean up this duplication, or extract methods to help our code be more self-describing. Even more important is <a href="http://davesquared.net/2010/05/on-design-and-missing-forest-for-trees.html">noticing larger scale duplication</a>; finding not only repeated code, but repeated activities that can be abstracted or made a transparent infrastructure concern.</p>

<h2>How does this help?</h2>

<p>Glad you asked! Provided you accept my rambling above, we now understand that TDD helps constrain the scope of the problem we&#8217;re looking at, gives us a relatively simple way of thinking about the large number of design questions that apply to that problem, and provides rapid feedback on how well we are addressing these problems. This knowledge can help get us through some of the problems that can trip up people learning TDD.</p>

<h3>What test should I write?</h3>

<p>Well the flippant (albeit accurate) answer is whatever test provides you with the feedback you need to further your design. This answer is hardly going to help people just starting out unless we delve into a bit more detail.</p>

<p>What I struggled with most while learning TDD was moving from the simple, unit test driven examples to using TDD for real code. If I picked a unit test of a class lower down the hierarchy I&#8217;d have problems fitting the driven code back into the design. The problem was I wasn&#8217;t thinking about how I could use my tests to give me feedback on what to do. If I had started with a test that described what I was trying to do then I would have been able to start driving out abstractions that actually helped me solve the problem, instead of blindly abstracting myself in circles. The trick is to pick a test that encapsulates what you know about the current problem.</p>

<p>Let&#8217;s look at a more extreme case to illustrate this. Say we have a brand new project. In this case we&#8217;re not even sure what classes we need. How can we use TDD to get feedback on our class design when we don&#8217;t even have a class? Well, let&#8217;s write a test that describes the feature (or story) we&#8217;re working on. This will be an acceptance test rather than a unit test. <a href="http://davesquared.net/2010/06/tdd-not-utdd.html">No one said TDD has to exclusively use unit tests</a>, but as most examples for new practitioners concentrate on the process they all seem to use unit tests, hence the common misconception. This process will allow us to sketch out a basic architectural skeleton which we can flesh out and modify as we go along. Our test will provide us with feedback on how well we&#8217;ve managed to break the feature&#8217;s requirements into manageable, testable abstractions. We&#8217;ll also get a nice programmable interface to our system for future acceptance and integration tests. I found the <a href="http://www.growing-object-oriented-software.com/">Growing OO Software Guided By Tests books</a> a great source of examples of how to do this.</p>

<p>Once we have defined the basic behaviour for the feature via the acceptance test we can implement some of the infrastructure required for the test (build, CI etc.), and then pick the top class our test has discovered and start using more granular tests to drive out our classes. Or perhaps we won&#8217;t even need more granular tests; the direction given by our acceptances tests might be sufficient for us to implement the feature.</p>

<h3>But you just wrote code before writing a test!</h3>

<p>Sure! TDD gives you feedback on design. When you already know what&#8217;s required (say you&#8217;re filling in pieces to comply with a set architecture) the design part of TDD is not giving you much benefit. The only real positive of writing a test first in this case is that you ensure the test fails first; it&#8217;s a way of testing the test. This is test-first development, not TDD.</p>

<p>The less unknowns we have, the less our tests need to teach us, and the larger steps we can take.</p>

<h3>You picked an MVVM pattern and defined a view without a test!</h3>

<p>We&#8217;ve already learnt that TDD constrains the problem space we are looking at, as well as provides feedback on how well we are addressing this problem. Of course TDD isn&#8217;t the only thing imposing constraints on us, nor our only source of feedback. Our choice of UI technology (WPF, an MVC framework, GTK, WebForms etc) imposes very real constraints on how we deal with our UI.</p>

<p>One way to address these constraints is to use a <a href="http://martinfowler.com/eaaDev/SeparatedPresentation.html">separated presentation pattern</a> such as <a href="http://davesquared.net/2010/02/attempt-at-simple-mvvm-with-wpf.html">MVVM</a>. In that case, we might define the properties and commands we need on a ViewModel without a single test. We&#8217;re constrained by our UI technology, the UI pattern we&#8217;ve chosen, and the screen design we&#8217;ve worked out during discussions (another medium with rapid feedback) with our customers.</p>

<p>TDD isn&#8217;t our only source of constraints and feedback. Where real constraints exist we don&#8217;t need to force new ones by using TDD and ignoring reality.</p>

<div class="note"><b>Aside</b>:  I struggled with how to derive separated presentation patterns from first principles a lot when I first started: how could I use only tests to end up with MVP, MVVM etc? I think this becomes very difficult because at one end you have reality, the UI toolkit you need to work with, and at the other you just have your imagination and your tests. There are some hard constraints that simply don&#8217;t pop out from tests, and they are just as relevant drivers as the feedback from the tests themselves. (That said, if anyone has derived a nice separated presentation implementation purely from TDD please share.)</div>


<h3>I&#8217;m stuck! I&#8217;ve test-driven myself into a hole and can&#8217;t get out</h3>

<p>I&#8217;ve done this an embarrassing number of times. I&#8217;ve used tests to guide every line of code, <a href="http://davesquared.net/2010/05/test-driving-while-refactoring.html">including lines I write as part of refactoring</a>, and ended up stuck in a maze of pointless abstraction. Remember, TDD is not a substitute for thinking, <a href="http://davesquared.net/2010/02/improving-tdd-skills-via-treatment-of.html">you still need to be able to code</a>.</p>

<p>If TDD is not giving you the information you need, back up and try something else. Go through some ideas with colleagues as a white board. Use test coverage at a higher level of abstraction for feedback and work from there. Maybe spike some solutions (it is often faster to try out 3 different solutions than try and figure the best solution up front). TDD is one way of getting good constraints and feedback, but it is not the only way. Do what works for your situation, but make sure you think about it later and try and find the reason you got stuck.</p>

<div class="note"><b>Note:</b> If you can&#8217;t get TDD to help you with a problem, make a note of it before trying something else, and come back to it later. It is important to see if it was just a problem TDD was ill-suited for, or whether a gap in your knowledge has been exposed. You&#8217;ll never know the difference if you give up too easily on TDD. Pursuing these leads is what led me to discover mocking, IoC containers, conventions, BDD etc. And I&#8217;m far from finished finding gaps in my knowledge. ;)</div>


<h3>TDD? BDD? ATDD? Which should I use?</h3>

<p>You might notice that the use of TDD is very problem-specific (and developer-specific for that matter). Different problems raise different design issues that will require different types of feedback. This means we&#8217;re going to have to apply TDD in different ways to give us the feedback we need to drive out our application&#8217;s design. This can be top-down or bottom-up. In some cases we could lean very heavily on acceptance tests. Other times we might like to drive out implementation by a whole lot of unit tests. Still other problems may be well-served by end-to-end tests that actually hit an external database or service (these have their own issues, but if they are helping you get the feedback and design you need, then go for it!).</p>

<p>There isn&#8217;t one right answer; you need to focus on getting the feedback that helps you get the job done.</p>

<h2>Conclusion</h2>

<p>While the rules for the TDD process are simple, learning to use it effectively is anything but. The rules themselves give us only minimal assistance when we&#8217;re starting out, and the hype surrounding TDD can give us unrealistic expectations about what it will do for us.</p>

<p>We can address this by trying to understand how TDD works; the rationale behind the rules. We&#8217;ve seen that TDD is really just a way of constraining a problem, encapsulating the process of design by using the abstraction of a test, and providing rapid feedback as to how well our design is going. The process essentially gives us a nice way to think about and iteratively do design.</p>

<p>Once we understand this we can start trying to apply TDD in a more targeted manner, deliberately applying it in ways to give us the feedback we need to make design decisions. We won&#8217;t restrict ourselves to unit tests alone, but will switch between testing at different levels of abstraction without feeling guilt over not using an unit test as all the introductory examples seem to. Nor will we feel guilty when we make a decision without TDD&#8217;s feedback, instead relying on other feedback like other systems we&#8217;re interacting with, the UI technology we&#8217;re using, or the persistence technology that&#8217;s been mandated company-wide.</p>

<p>We&#8217;re also not going to feel inept when we fail to apply the deceptively simple TDD rules, as we realise TDD is just a tool to help us think about design problems, and that there are always going to be gaps in our knowledge and experience we need to fill before we can solve new problems. The fact TDD has illustrated these gaps quickly gives us an opportunity to learn right away, rather than leaving us in blissful ignorance until things explode as a deadline looms, or worse, keeping us unaware of and repeating the same mistakes time after time.</p>

<p>Design is hard. TDD gives us one great way of thinking about it and learning how to do it better.</p>

<p>If you&#8217;re currently learning TDD I hope these ramblings have given you a different way of thinking about it that will help make your journey a little easier. Best of luck &#8211; I think you&#8217;ll find TDD well worth the effort! :)</p>

<h2>In case this post wasn&#8217;t long enough for you&#8230;</h2>

<p>This post summarises what I&#8217;ve learned to date from my TDD journey. Have a look at the following posts if you&#8217;d to read how my thoughts on this have evolved over the years (they&#8217;re listed from oldest to latest). They&#8217;ll also provide some more details and/or examples of some of the ideas discussed in this post.</p>

<ul>
<li><a href="http://davesquared.net/2009/06/moving-to-scenario-based-unit-testing.html">Scenario-based testing</a></li>
<li><a href="http://davesquared.net/2009/09/example-of-driving-design-through-top.html">Example of driving design through top-down testing</a></li>
<li>Calculators and a tale of two TDD styles <a href="http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-1.html">Part 1</a>, <a href="http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-2.html">Part 2</a>, <a href="http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-3.html">Part 3</a></li>
<li><a href="http://davesquared.net/2009/11/favour-test-driving-logic-over-data.html">Favour test driving logic over data</a></li>
<li><a href="http://davesquared.net/2010/02/what-exactly-is-tdd-driving.html">What exactly is TDD driving?</a></li>
<li><a href="http://davesquared.net/2010/02/improving-tdd-skills-via-treatment-of.html">Improving TDD skills via treatment of test infection</a></li>
<li><a href="http://davesquared.net/2010/05/test-driving-while-refactoring.html">Don&#8217;t test drive while refactoring</a></li>
<li><a href="http://davesquared.net/2010/06/tdd-not-utdd.html">TDD vs UTDD</a></li>
</ul>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Changing previously stubbed calls with Rhino Mocks]]></title>
    <link href="http://davesquared.net/2011/02/changing-previously-stubbed-calls-with.html"/>
    <updated>2011-02-05T11:12:00+11:00</updated>
    <id>http://davesquared.net/2011/02/changing-previously-stubbed-calls-with</id>
    <content type="html"><![CDATA[<p>There are occasionally times when I want to stub a call to return different values at different times in my test. I try and avoid this, as it generally means I&#8217;m writing procedural code and violating the <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Tell Don&#8217;t Ask principle</a>, but there are times when it&#8217;s necessary (say, writing procedural steps for an integration test).</p>

<p>Anyway, I&#8217;ve tended to struggle with this when using Rhino Mocks, and end up randomly trying various forms of repeats, such as <code>Repeat.AtLeastOnce()</code> or <code>Repeat.Any()</code>, before I get it to work.</p>

<p>I recently found a better way thanks to <a href="http://stackoverflow.com/questions/123394/rhino-mocks-re-assign-a-new-result-for-a-method-on-a-stub">a thread on StackOverflow</a>: just use a closure.</p>

<pre class="brush:csharp">public interface IDoStuffWithInts { int GetAnInt(); }

[Test]
public void Change_return_value() {
    int valueToReturn = 0;
    var stub = MockRepository.GenerateStub&lt;IDoStuffWithInts&gt;();
    stub.Stub(x =&gt; x.GetAnInt())
        .Return(0)
        .WhenCalled(x =&gt; x.ReturnValue = valueToReturn);

    valueToReturn = 7;
    Assert.AreEqual(stub.GetAnInt(), valueToReturn);
    valueToReturn = 11;
    Assert.AreEqual(stub.GetAnInt(), valueToReturn);
}
</pre>

<p>Unfortunately you need to set a dummy return value first, but you can then override that using <code>WhenCalled</code>. This makes it easy to change the stubbed value by changing the variable. I&#8217;ve found it useful to sometimes wrap this in a helper method, such as <code>MakeStuffDoerReturn(int value)</code>.</p>

<p>Hardly ground-breaking I know, but hopefully it will save someone else a bit of pain. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rules]]></title>
    <link href="http://davesquared.net/2011/01/rules.html"/>
    <updated>2011-01-28T00:16:00+11:00</updated>
    <id>http://davesquared.net/2011/01/rules</id>
    <content type="html"><![CDATA[<p>I was exchanging tweets today with <a href="http://twitter.com/#!/liammclennan">Liam</a> on how to decide when to use private methods and when to put the logic in a new class. This got me thinking about rules, or at least, rules as applied to software development.</p>

<p>I love rules. I&#8217;m a rules addict. I find them strangely liberating; they restrict my options so that I can better focus on the creative aspect of whatever I&#8217;m doing. They seem to promise repeatability, predictability, and a degree of mechanical perfection that just needs to be nudged together by the developer.</p>

<p>Luckily for me there&#8217;s no shortage of rules. There&#8217;s <a href="http://davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html">SOLID</a>, the <a href="http://c2.com/cgi/wiki?XpSimplicityRules">4 rules of simple design</a>, <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>, <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Tell Don&#8217;t Ask</a> and a whole bunch of step-by-step patterns and refactorings to mindlessly apply. ;)</p>

<p>Of course, it&#8217;s never quite that simple. Rules should come with a disclaimer, <em>WARNING: Some assembly required</em>.</p>

<p>No matter how many books I read, how many blogs I subscribe to, or how many patterns I memorised, a nice design never really seemed to naturally emerge from all that knowledge. So I tried quizzing <a href="http://davesquared.net/2007/11/are-no-smart-guys-there-only-us.html">experts</a> on the rules they use. And pretty much without fail the answer was &#8220;it depends&#8221;. You give them a specific case, they give you an answer. You ask for a rule, and they tend to reply with words like &#8220;generally&#8221; and &#8220;favour&#8221;, not a specific procedure.</p>

<p>At a course a few years back I even quizzed the instructor until I managed to distil the <a href="http://davesquared.net/2009/08/nothin-but-net-sydney-2009-day-4.html">procedure for getting beautiful designs from BDD</a>. The instructor himself couldn&#8217;t explain the procedure, as he said much of the decisions are based on context developed from experience. Of course, that didn&#8217;t stop me trying to get to the rules at the heart of it. Nor did those rules assure my success at producing good designs.</p>

<h2>Rules are the question, not the answer</h2>

<p>Much as I generally dislike some of the grandiose metaphors that are typically attributed to software development by software developers, one that sticks out as quite relevant here is the Japanese martial art concept of <a href="http://c2.com/cgi/wiki?ShuHaRi">Shu Ha Ri</a>, which describes the three stages of learning required to master a discipline. (At least as I understand it. Please correct any egregious errors I make :))</p>

<p>The first stage, Shu, is all about rules. The beginner memorises the rules and applies them dogmatically. They may not see opportunities, or be able to take advantage of them, because they are sticking to the rigid forms they are learning.</p>

<p>The second stage, Ha, is when the beginner gains enough experience and has internalised the rules enough to start pondering the reasons behind the rules. They slowly begin straying from the rules as they start to notice the shortcomings when applying them to the myriad of situations the world throws at them. They also start to gain a better sense of appraising the relative success or failure of these experiments, and so start to apply rules on a case-by-case basis.</p>

<p>The final stage, Ri, is when the practitioner has risen above the rules. Every situation is dealt with on its merits. An observer may notice many elements of the rules in action, but the practitioner is merely doing what is required of the situation as governed by their experience; the context they have built up by applying and deviating from the rules.</p>

<p>While all this may seem a little twee, I can&#8217;t count the large number of times I have talked to people that really seem to know their stuff, and found they seem to have this uncanny knack of seeing straight to the heart of a problem and solving it in an elegant, seemingly effortless way. I can see many of the rules present in their approach, yet their solution never seems to be one I could simply derive by applying the rules or a specific formula. In hindsight, the solution tends to seem obvious; but never before I&#8217;ve seen it.</p>

<p>This has led me to believe that rules are the question, not the answer. They are what starts us on a path of enlightenment. It is only once we have memorised the rules and start noticing their limitations that we start asking ourselves why the rules are there and how they work. Once we start this questioning, we start gaining some mastery of the discipline. And it is only after we no longer need the rules that we are truly proficient, and we have uncovered the answer of how to write good software.</p>

<p>And, as many consultants will tell you, that answer is &#8220;it depends&#8221;. ;)</p>

<h2>So now what?</h2>

<p>Well, I&#8217;m trying to break my rules addiction. I like to think I&#8217;ve spent way too much time in the Shu phase due to it&#8217;s inherent safety (rather than as the result of fundamental incompetence), and that it&#8217;s time to start looking past the safe-haven of those rules. More than anything I think this comes down to writing loads of code, trying out new approaches, <a href="http://davesquared.net/2007/11/are-no-smart-guys-there-only-us.html">taking risks</a> and questioning the meanings and motivations behind the rules.</p>

<p>How much time do you spend looking for rules on how to develop software right? If the answer is &#8220;lots&#8221;, maybe it&#8217;s time to let go of them?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Finding functional neatness with Haskell]]></title>
    <link href="http://davesquared.net/2011/01/finding-functional-neatness-with.html"/>
    <updated>2011-01-13T00:13:00+11:00</updated>
    <id>http://davesquared.net/2011/01/finding-functional-neatness-with</id>
    <content type="html"><![CDATA[<p>I&#8217;ve recently started to learn Haskell (used it at Uni over a decade ago, but I&#8217;m trying to actually appreciate it this time :)). Here is an example I found when working through the recursion chapter of the excellent <a href="http://learnyouahaskell.com/">Learn you a Haskell tutorial</a>. Yes, I know this is a very corny example, but the fact I was able to figure it out just from the algorithm description in the tutorial, despite being a Haskell newbie, really impressed the beauty of the language upon me.</p>

<p>Remember <a href="http://en.wikipedia.org/wiki/Quicksort">quicksort</a>? Picking a pivot, then putting it in the middle of the sorted smaller elements and sorted larger elements? Typically you end up with a wad of procedural code, such as <a href="http://en.csharp-online.net/Quick_Sort">this fairly typical C# example</a>.</p>

<p>In Haskell, you work more with describing what something <em>is</em>, rather than how it works. So we end up with something like this:</p>

<pre>quicksort :: (Ord a) =&gt; [a] -&gt; [a]
quicksort [] = []
quicksort (x:xs) = smaller_sorted ++ [x] ++ larger_sorted
    where smaller_sorted = quicksort [y | y &lt;- xs, y &lt;= x]
          larger_sorted = quicksort [y | y &lt;- xs, y &gt; x]
</pre>

<p>The first line is defining the type of the function, it takes a list of orderable types and returns a list of those types. The second line is our recursion edge case: sorting an empty list returns an empty list. </p>

<p>The third line is the definition of our algorithm. Given <code>(x:xs)</code>, the list head (<code>x</code>) and tail (<code>xs</code>), we put the head between the sorted list of everything smaller and the sorted list of everything larger. </p>

<p>The <code>where</code> clause has our recursive calls using list comprehensions. The line <code>[y | y &lt;- xs, y&lt;=x]</code> selects the elements of the tail (<code>xs</code>) which are less that our pivot (<code>x</code>).</p>

<p>Neat huh?</p>

<p>We can also try and bring the functional-style with us to C#, but you pretty quickly start missing pattern matching, and built-in list syntax like head, tail and concatenation. In many cases (like this one) the C# compiler also won&#8217;t be able to get you the <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail call elimination</a> possible in functional languages like Haskell, which tends to make recursive implementations less desirable.</p>

<pre class="brush:csharp">public static IEnumerable&lt;int&gt; QuickSort(IEnumerable&lt;int&gt; elements) {
    if (!elements.Any()) return new int[0];

    var head = elements.First();
    var tail = elements.Skip(1);

    var sortedSmaller = tail.Where(x =&gt; x &lt;= head);
    var sortedLarger = tail.Where(x =&gt; x &gt; head);

    return QuickSort(sortedSmaller).Concat(new [] {head}).Concat(QuickSort(sortedLarger)); 
}
</pre>

<p>This is simpler than the more procedural version, but not as simple as the Haskell version. And we still have to stay concious of differences such as how tail recursion is handled over different platforms which can have a huge impact on performance. With that in mind though, it&#8217;s neat enough for me to keep digging into functional programming, as even if I don&#8217;t end up doing much with pure functional languages the techniques can provide some very interesting alternative ways of solving problems in any language.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Year's resolution]]></title>
    <link href="http://davesquared.net/2011/01/new-years-resolution.html"/>
    <updated>2011-01-01T20:46:00+11:00</updated>
    <id>http://davesquared.net/2011/01/new-years-resolution</id>
    <content type="html"><![CDATA[<p>I don&#8217;t generally go in for the whole New Year&#8217;s resolution thing. I&#8217;m constantly looking for ways to improve (and have a lot of flaws to choose from ;)), and I&#8217;m not going to wait for a specific date to start trying to fix things. In this case though, the timing has coincided quite nicely. So here it is:</p>

<blockquote>
I will not knowingly contribute, or condone the contribution of, bad code to a project.
</blockquote>

<p>Let me tell you a story.</p>

<h2>A tale of two projects</h2>

<p>Before I start, let me assure you that we are talking about <em>internal code quality</em> here. DRY, SOLID code. The scenarios discussed below are all uncompromising about product quality. The final products go through strict verification protocols to ensure quality. This is obviously completely independent of the internal code design, which is more to do with how easy the code is to change in light of new requirements etc. </p>

<p>So, towards the end of 2010 my team had several quick projects to get through. All had unachievable deadlines, all had a small scope and small domain, and all were of relatively low importance (more research experiments than shippable products).</p>

<p>For the first of these we had 6 weeks. We used an Agile project approach with one week iterations, did TDD, wrote acceptance tests, etc. Clean code all the way. The software that shipped at the end of the third iteration was good enough to fulfil all the major requirements and everyone was really pleased. We added some extra polish and features in the remaining weeks.</p>

<p>The last of these went quite differently. We were given three days. We estimated we&#8217;d need three weeks. We communicated that the deadline could not possibly be met, but resolved to do everything we could to get it working as quickly as possible.</p>

<p>After a bit of debate, <a href="http://davesquared.net/2010/08/there-is-no-u-in-collective-ownership.html">we decided</a> to ditch any attempt at clean code and just do whatever it took to ship it as quickly as possible. It was a small project with next to no maintenance requirements, so we figured write-only code would be ok. No SOLID, DRY code meant no slaving over the IDE extracting interfaces, or driving out a nice design using rapid feedback from TDD. We wouldn&#8217;t use acceptance tests, but would manually test as required, and only automate any testing that was easy. We had a continuous build, but skipped the full deployment build to ship at the end of an iteration; probably because we didn&#8217;t have iterations. The scope was small, so we decided to use only very broad, coarse-grained stories and just work until they were done. We still had our rigorous verification protocol to pass, but it was a small project, how hard could it be?</p>

<p>As I said, we were meant to have it done in 3 days. We figured it would take three weeks. It <em>should</em> have taken three weeks.</p>

<p>It ended up taking three months.</p>

<h2>The way to go fast is to go well</h2>

<p>Our decision to abandon any pretence of working towards clean code was our undoing. Not making much of an effort at design or removal of duplication resulted in tight coupling and obfuscated code. It was effectively a write-only code base; the simplest of changes became all but impossible, including fixing what should be simple bugs or catering for clarifications to requirements. It was very difficult to find seams for testing bits in isolation, and changes would frequently ripple through the code. Our lack of rigour around project planning and story breakdown made it impossible to track our progress. Time saved by not automating repetitive tasks was re-spent ten-fold on mindless, error-prone tasks when we should have been writing code to finish the project.</p>

<p>Let me make this very clear: anyone that tells you working toward clean code is a waste of time and will slow you down is completely wrong.</p>

<p>You cannot <a href="http://davesquared.net/2010/08/quality-vs-shipping.html">trade internal quality for speed</a>. I give you about 30 minutes of coding before you need to change something where an automated test would have made you faster. As you can read from the <a href="http://davesquared.net/2010/08/quality-vs-shipping.html">earlier post</a>, I was pretty convinced of this already, but experiencing such a dramatic illustration by far surpassed my expectations in terms of the return on investment in clean code. Previously I would have guessed we&#8217;d start feeling pain after about a week of hacking, but it started almost immediately. I am now adamant it is impossible to have anything but an illusion of productivity without writing clean code.</p>

<p>As <a href="http://twitter.com/unclebobmartin">Uncle Bob</a> puts it, the way to go fast is to go well.</p>

<h2>Clean code is a journey, not a destination</h2>

<p>None of the above means gold plating your code. Your code need not be a shining beacon of architectural and OO greatness, gazed upon with awe by artisans for centuries to come. Your code is never completely clean; clean code is not an end state, it is about the process of reducing coupling, reducing duplication, simplifying, and clarifying intention whenever you work with your code.</p>

<p>This does not mean you must practice TDD. It does not mean you need to use a particular language or framework. It does not mandate a project methodology. It doesn&#8217;t mean you can&#8217;t spike. It doesn&#8217;t mean you can&#8217;t knowingly take on manageable technical debt. It doesn&#8217;t even mean you must unit test. It means that you need to do what it takes to ensure your code is maintainable. That your code has the qualities we associate with clean code: it works, it is maintainable, it is correct. It is not about trading quality for speed, because that is an illusion. It is about doing what it takes to produce what&#8217;s required as quickly as possible, and that means you can&#8217;t afford to write cruddy code.</p>

<p>In short, <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=7588">don&#8217;t ship $#*!</a>.</p>

<h2>Conclusion</h2>

<p>And so back to my resolution, which was always a high-priority goal of mine but never an unwavering commitment, never before a line I simply will not cross. I will never deliberately contribute or support the contribution of bad code to a project. I will always strive to write clean code, not in pursuit of some programming utopia, but in the sincere belief that it is the only way to quickly and effectively deliver a project.</p>

<p>I wish you all the best of health, happiness, and clean code for 2011. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Revisting replacing a Ruby instance method with a closure]]></title>
    <link href="http://davesquared.net/2010/12/revisting-replacing-ruby-instance.html"/>
    <updated>2010-12-18T01:29:00+11:00</updated>
    <id>http://davesquared.net/2010/12/revisting-replacing-ruby-instance</id>
    <content type="html"><![CDATA[<p>Last month I looked at <a href="http://davesquared.net/2010/11/replacing-ruby-instance-method-with.html">how to replace a Ruby method of a single object instance with a closure</a>, before <a href="http://davesquared.net/2010/11/continuing-adventures-in-adding-methods.html">defining a module that could make this easier</a>. Since then I&#8217;ve learnt another option which I thought I&#8217;d share as it helped me get a greater appreciation for Ruby modules.</p>

<div class="note"><b>Note:</b> I am a Ruby n00bie, so take all this with a suitable amount of salt. If this (or the previous posts) violate Ruby conventions, or there is an idiomatic way of solving this, please let me know.</div>

<h2>Quick recap</h2>
<p>The problem is fully described in the <a href="http://davesquared.net/2010/11/replacing-ruby-instance-method-with.html">original post</a>, but it basically starts with this class:</p>

<pre class="brush:ruby">
class Greeter
    def say_hello
        puts "Hello World!"
    end
end
greeter = Greeter.new
greeter.say_hello
#=&gt; Hello World!
</pre>

<p>I then wanted to replace <code>say_hello</code> on that single <code>greeter</code> instance with a method that would close over a local variable, like this:</p>

<pre class="brush:ruby">
name = "Anonymous Dave"
# replace say_hello on greeter so it puts "G'day #{name}"

greeter.say_hello
#=&gt; G'day Anonymous Dave

name = "Clarence"
greeter.say_hello
#=&gt; G'day Clarence
</pre>

<p>Standard reopening of the instance (or even the <code>Greeter</code> class) and redefining the method won&#8217;t work here, all because we have the pesky requirement of closing over our local <code>name</code> variable, which means we need to use a block (basically a lambda function for C# people). We can use <code>Class.send</code> to call the private <code>define_method</code> which takes a block, but that will add it to every instance of <code>Greeter</code>, not a single instance.</p>

<h2>Modules to the rescue</h2>

<p>We solved this in the original post by referencing the instance&#8217;s <i>metaclass</i> (aka <i>eigenclass</i>), but there is another way:</p>

<pre class="brush:ruby">
name = "Anonymous Dave"
new_say_hello = Module.new do
    self.send(:define_method, :say_hello) do
       puts "G'day #{name}"
    end
end
</pre>

<p>Here we&#8217;ve created a new anonymous module that sends <code>define_method</code> to create a <code>say_hello</code> method using a block, in the same way as we could have reopened the <code>Greeter</code> class and added it to every instance. The difference here is that this module has not been mixed in anywhere yet; we can choose exactly where we want to apply it. In this case, to our single instance:</p>

<pre class="brush:ruby">
# Mixin module to greeter instance to add our new say_hello method
greeter.extend new_say_hello

greeter.say_hello
#=&gt; G'day Anonymous Dave

name = "Clarence"
greeter.say_hello
#=&gt; G'day Clarence

# Other instances are unaffected by this:
another_greeter = Greeter.new
another_greeter.say_hello
#=&gt; Hello World!
</pre>

<p>I think I still prefer the <a href="http://davesquared.net/2010/11/continuing-adventures-in-adding-methods.html"><code>Meta</code> module approach</a>, but this way has the advantage of sticking closely to standard Ruby constructs and manages to avoid metaclasses.</p>

<p>What was most helpful to me out of this as a Ruby n00bie is the understanding that we can work using class scope within a module (avoiding metaclass shenanigans), then apply that scope selectively by including the module in a class, or by extending an instance with the module.</p>
]]></content>
  </entry>
  
</feed>

