<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <title>Jennifer Smith's Blog</title>
  
  <link href="http://www.jennifersmith.co.uk" />
  <updated>2011-08-07T10:25:11-05:00</updated>
  <id>http://www.jennifersmith.co.uk</id>
  <author>
    <name>Jennifer Smith</name>
    <email>nospam@jennifersmith.co.uk</email>
  </author>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/JenniferSmithsBlog" /><feedburner:info uri="jennifersmithsblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title> QCon 2011 - Friday session - Node.js: Asynchronous I/O for Fun and Profit</title>
    <link href="http://feedproxy.google.com/~r/JenniferSmithsBlog/~3/j3UUTfGORmw/qcon-2011-friday-session-node-js-asynchronous-io-for-fun-and-profit" />
    <updated>2011-03-27T00:00:00-05:00</updated>
    <id>http://www.jennifersmith.co.uk/blog/2011/03/27/qcon-2011-friday-session-node-js-asynchronous-io-for-fun-and-profit</id>
    <content type="html">
      &lt;p&gt;Stefan Tilkov started this talk by discussing the difference between
      thread and event based I/O. I get the feeling that this might have been
      covered in the previous talk (or just old hat to most people) but I
      really appreciated the explanation as now I think I finally understand
      the difference between the two models. Let’s see shall we…&lt;/p&gt;
      
      &lt;p&gt;Most commonly used web servers - like Apache, IIS etc. use the thread
      based model. Each request is served by a dedicated thread. A request for
      a resource might look something like this:&lt;/p&gt;
      
      &lt;p&gt;&lt;img src="http://www.websequencediagrams.com/cgi-bin/cdraw?lz=Q2xpZW50LT5TZXJ2ZXIgdGhyZWFkOiBSRVFVRVNUIEdFVCAvbWVudXMvZnJpZGF5IChSZWFkIHJlcXVlc3QpCm5vdGUgb3ZlciAiADYNIjogUGFyc2UgYW5kIHByb2Nlc3MALggKAGANLT5EYXRhYmFzZTogc2VsZWN0ICogZnJvbSAAcwUgd2hlcmUgZGF5ID0gJ0YAgQAFJwBsCwAzCgpEaWcgYXJvdW5kIGluIG15IGluZGV4ZXMgCnRvIGZpbmQgc29tZSBtYXRjaGluZyBkYXRhCmVuZCBub3RlCgB-CACBexFNZW51IHJlY29yZCBmb3IgAH4GAIFeHENhcnJ5IG9uAIFxCGluZwCBZBhGaWxlIHN5c3RlbTogAIJWBSJNZW51cy50ZW1wbGF0ZSIAglcMACMLIjogTG9jYXRlIHRoZSBmaWxlCgBBCwCDQBEAOhwAgykQTW9yZQCBMAsuLi4AgyIQAIQwBjogUkVTUE9OU0UgPGh0bWw-Li4uPC8ABQUKCgoK&amp;s=napkin" alt="image" style="height:504px"&gt;&lt;/p&gt;
      
      &lt;p&gt;The thread spends much more time sitting around waiting to read/write
      data and waiting for data from other collaborators (Database, File
      system, Client) than it does actually processing the request. While
      waiting for external events to happen, the thread blocks and does not do
      much apart from use a lot of memory. The number of threads you are going
      to need is going to scale linearly with the amount of concurrent
      requests you have (this is correct?), eating memory up and leading to
      thread starvation under a high load. The problem will be exacerbated by
      an upstream connection experiencing performance problems (like a dodgy
      database server): more threads will be hanging around doing nothing
      waiting for the upstream service to finish meaning there are less
      threads hanging around to serve new requests and thread starvation gets
      quicker.&lt;/p&gt;
      
      &lt;p&gt;Evented I/O based servers on the other hand will deal with I/O
      activities asynchronously and avoid blocking on I/O operations
      alltogether. They will make use of events and callbacks to pick up the
      processing work again (on whatever thread it happens to be invoked on).
      So if in our thread based model a thread blocks while waiting for a
      client request to be read from the socket, in the Evented I/O model the
      thread will call the corresponding async socket read function, passing
      in a callback to be called when the async operation is complete. As it
      does not have to wait for the I/O to complete, the calling thread can go
      off and do other things - like picking up the next request. Infact, at
      the heart of such a server you would probably see an event loop similar
      to GUI applications which will just cycle round waiting for incoming
      events.&lt;/p&gt;
      
      &lt;p&gt;The nginx webserver operates in an evented I/O model and Stefan showed
      some interesting comparisons of its performance with Apache
      (&lt;a href="http://blog.webfaction.com/a-little-holiday-present"&gt;nginx_performance&lt;/a&gt;)&lt;/p&gt;
      
      &lt;p&gt;The evented I/O model is traditionally regarded as a low level and
      complicated pattern. There is support for it in most standard platforms
      (.NET IO completion ports and java.nio for example) but it is not widely
      used. Thread based is still very much the default.&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/p&gt;
      
      &lt;p&gt;Stefan then went on to talk about everyone’s favourite browser scripting
      language. Javascript has gone from being reguarded as a ‘toy’ language
      with some strange design features and browser compatibility issues to
      one with great framework support, ironing out dom compatibility issues.
      With things like the Crockford ‘Good Parts’ book being widely read, we
      as an industry have a good understanding of the language itself - as
      well as a good knowledge of the ‘bad’ parts. All fairly well-covered
      stuff but it lead him nicely to:&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;Evented I/O + JavaScript =&gt; Node.js&lt;/strong&gt;&lt;/p&gt;
      
      &lt;p&gt;So we come on to Node.js - a framework for writing Evented I/O in
      JavaScript. Node.js runs on the V8 engine, free from the browser so you
      can use it to write performant networking programs - like web servers
      etc. As Stefan put it: ‘High-performance network runtime, using
      JavaScript as a high-level DSL’.&lt;/p&gt;
      
      &lt;p&gt;Carrying on with the car based puns, under the hood:&lt;/p&gt;
      
      &lt;p&gt;&lt;img src="http://dl.dropbox.com/u/18288740/mytw/nodejs.png" alt="image_2" /&gt;
      image_2&lt;/p&gt;
      
      &lt;p&gt;Other interesting parts are the low level C libraries:&lt;/p&gt;
      
      &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;libev&lt;/strong&gt;: event loop library&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;libeio:&lt;/strong&gt;async I/O&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;c_ares:&lt;/strong&gt;async DNS&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;http_parser:&lt;/strong&gt;superfast http request/response parser written
      especialy for node.js by its creator &lt;a href="https://github.com/ry"&gt;Ryan
      Dahl&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;p&gt;From the code samples, it seems that getting started with basic programs
      is pretty easy:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;net&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"net"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;net&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Echo server\r\n"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setEncoding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ascii"&lt;/span&gt;&lt;span class="p"&gt;);;&lt;/span&gt;
        &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8124&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;This listing sets up a server using the net library. When an incoming
      socket is accepted, it binds to the data receive event and simply writes
      back to the socket the data it receives uppercase. Both I/O parts of
      this program - accepting an inbound connection and waiting to receive
      data on a socket - are handled asynchronously by passing in event
      handlers.&lt;/p&gt;
      
      &lt;p&gt;Stefan went through a few more simple code samples which he has put up
      on github: &lt;a href="https://github.com/stilkov/node-samples"&gt;Stefan Tilkov
      node-samples&lt;/a&gt;.&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;Spaghetti code&lt;/strong&gt;&lt;/p&gt;
      
      &lt;p&gt;One drawback of working in an asynchronous/callback world is that it
      tends to lead to tangled, spaghetti code. If you wanted to call two
      operations in a synchrnonous way, it might look like this:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;jump_to_the_left&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="nx"&gt;step_to_the_right&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;You can call each method procedurally in the order you desire. In
      node.js it might end up looking like this (presuming these were I/O
      operations):&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;jump_to_the_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
          &lt;span class="nx"&gt;step_to_the_right&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Which presumably if it gets to any kind of complicated, the code would
      start to resemble:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;jump_to_the_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
         &lt;span class="nx"&gt;step_to_the_right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
         &lt;span class="nx"&gt;put_your_hands_on_your_hips&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
              &lt;span class="nx"&gt;bring_your_knees_in_tight&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
          &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Then if you add in error handling to the mix, synchronously:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;jump_to_the_left&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nx"&gt;step_to_the_right&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; 
          &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Failed to execute timewarp sequence"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;… which wouldn’t work asynchronously, as the try/catch would not be
      surrounding the point at which the handlers are executed. A convention
      has been adopted to pass in the error status as the first parameter to a
      callback, leading to:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;jump_to_the_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
          &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Failed to jump to the left"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;step_to_the_right&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;It’s not hard to see that any program above the level of ‘trivial’ is
      going to get very complex and unreadable.&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;Libraries for node&lt;/strong&gt;&lt;/p&gt;
      
      &lt;p&gt;Fortunately you can mitigate the complexity of working with evented I/O
      in node - as well as perform a number of other standard operations -
      using the multitude of libraries available for node. See this &lt;a href="https://github.com/joyent/node/wiki/modules"&gt;list of
      node libraries&lt;/a&gt;. These can
      be managed using its very own package manager
      &lt;a href="https://github.com/isaacs/npm"&gt;npm&lt;/a&gt;. As node is primarily intended for
      web/http server usage, there are many libraries to deal with common
      HTTP/web tasks: templates, routing as well as libraries allowing you to
      run multiple nodes to explore multiple processors. You can even get an
      in-browser debugger:
      &lt;a href="https://github.com/dannycoates/node-inspector"&gt;node-inspector&lt;/a&gt;.&lt;/p&gt;
      
      &lt;p&gt;&lt;a href="https://github.com/creationix/step"&gt;Step&lt;/a&gt; is a particularly interesting
      library that attempts to smooth out some of the dodgy spaghetti code
      that arises from working asynchronously.&lt;/p&gt;
      
      &lt;p&gt;Stefan also mentioned that alongside all these modules there is also an
      active community around node.js. The node.js community are quite chummy
      with the NoSQL community too, so apparently you can generally expect
      good library support for your NoSQL technology of choice.&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;
      
      &lt;p&gt;In conclusion, Stefan stated that node.js “Popularises the ‘right way’
      of network programming.” and that it is easy to get started with and
      also fun to use (profitable too?). In the Q&amp;A, it was interesting to
      note that he had more experience using node.js as a client rather than a
      server (I don’t think we write web servers every day): he might use
      node.js to quickly knock up a test client or utility program. I can
      really see the value in using node.js this way - as a quick and
      productive way of writing networking applications.&lt;/p&gt;
      
      &lt;p&gt;This was one of the most enjoyable talks I attended at qcon. Stefan is
      an engaging and laidback presenter and I particularly appreciated his
      explanation through code samples. I attended the talk to understand more
      about node.js (which was just defined in my head as
      ‘javascript-on-the-server-whats-the-point-of-that’) and left with a good
      understanding and eager to use it when I get a chance.&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/JenniferSmithsBlog/~4/j3UUTfGORmw" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.jennifersmith.co.uk/blog/2011/03/27/qcon-2011-friday-session-node-js-asynchronous-io-for-fun-and-profit</feedburner:origLink></entry>
  <entry>
    <title>Static methods: feel the fear and do it anyway</title>
    <link href="http://feedproxy.google.com/~r/JenniferSmithsBlog/~3/UKFGY_dZSPQ/static-methods-feel-the-fear-and-do-it-anyway" />
    <updated>2010-07-25T00:00:00-05:00</updated>
    <id>http://www.jennifersmith.co.uk/blog/2010/07/25/static-methods-feel-the-fear-and-do-it-anyway</id>
    <content type="html">
      &lt;p&gt;When your tests go green, it’s often a good idea to think about tidying
      up the internals of the method you just added/changed. One of my
      favourite things to do here is to extract out meaningful methods from
      the implementation e.g&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toolkit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hammer&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hammer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toolkit&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hammer&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
          &lt;span class="n"&gt;Day&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Morning&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hammer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;Day&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Evening&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hammer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;ThisLand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AllOver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hammer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;… At the click of an alt-shift-m becomes :
      i&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IHadAHammer&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;IdHammerInThe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Day&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Morning&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;IdHammerInThe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Day&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Evening&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;AllOverThisLand&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// OK I took that one a bit too far.&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Very often, extracting methods will leave you with a helpful suggestion
      from our friend resharper: “This method could be made static”. I have
      often noticed a reticence from fellow developers for following this
      advice. Static methods tend to be considered harmful and seem to take us
      back to the bad old days of global methods (not to mention global
      state…). Added to that, the performance improvement you get is
      negligible and might smell like unnecessary optimisation. What tends to happen is
      that ReSharper is ignored (or the message is disabled) and we continue
      with something else. Thisc course of action does rather miss the point:
      the method makes no use of ‘this’. So even if you don’t put the static
      keyword in front of it, the method is still basically static anyway.
      Does that not tell you that maybe that method might not belong here? But
      where should it go? This is where the ‘do it anyway’ part comes in. Go
      make the method static. If there are several similar methods (and there
      usually are), go make all of them static and then look for patterns:&lt;/p&gt;
      
      &lt;ul&gt;
      &lt;li&gt;A set of functions that take in a type you control:&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;div class="highlight"&gt;&lt;pre&gt;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;HasExpiredPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
         &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;HasPermissionToEditResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Perhaps this reasoning could be part of the User class as instance
      methods.&lt;/p&gt;
      
      &lt;ul&gt;
      &lt;li&gt;Methods which take in a system type as the first parameter, all with
      the same/similar name, for example:&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt;  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;IsValidEmailAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;){...}&lt;/span&gt;
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetEmailHostName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;emailAddress&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Maybe email address warrants a class of it’s own containing these
      methods. The EmailAddress class could end up being a magnet for many
      other similar methods dotted around other classes too.&lt;/p&gt;
      
      &lt;ul&gt;
      &lt;li&gt;Either of the above but taking in a collection/IEnumerable of the
      same, e.g. :&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;MailAddressCollection&lt;/span&gt; &lt;span class="nf"&gt;BuildSenderList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt; &lt;span class="n"&gt;emailAddresses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt; &lt;span class="nf"&gt;GetAllUsersInGroup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Group&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;pre&gt;&lt;code&gt;Here, extension methods are your friend for a quick fix (ReSharper
      can totally help you out here). Chances are that if 'User' is part
      of your domain model, then 'Users' should be too. So why not create
      a type representing the collection - Users, EmailAddresses - and
      migrate the static methods there.
      &lt;/code&gt;&lt;/pre&gt;
      
      &lt;p&gt;Simply by following our refactoring tool’s suggestion, we have made the
      code more expressive and readable. The design of our code has fallen
      into place without us having to think too hard (a very good thing).&lt;/p&gt;
      
      &lt;p&gt;So what if you can’t find a way to move around those static methods? I
      would suggest that you should still go static to acknowledge that the
      methods are slightly disjunct from the class. Maybe as your codebase
      grows, you can revisit and find a place for these new concepts.&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/JenniferSmithsBlog/~4/UKFGY_dZSPQ" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.jennifersmith.co.uk/blog/2010/07/25/static-methods-feel-the-fear-and-do-it-anyway</feedburner:origLink></entry>
  <entry>
    <title>Google letter template</title>
    <link href="http://feedproxy.google.com/~r/JenniferSmithsBlog/~3/14W6duL3cNc/google-letter-template" />
    <updated>2010-06-06T00:00:00-05:00</updated>
    <id>http://www.jennifersmith.co.uk/blog/2010/06/06/google-letter-template</id>
    <content type="html">
      &lt;p&gt;I was hunting for a decent letter template on google docs and could not
      for the life of me find one. I like to have the recipient address on the
      left and the sender on the right rather than all on one line. There is a
      lot of weird stuff in the public templates site! Anyway, here is the
      template:&lt;/p&gt;
      
      &lt;iframe width="620" height="170" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://docs.google.com/embeddedtemplate?id=0Adhg3tLfif75ZGc0aHJyeDdfMTE1ZnFtd3E3Y3E"&gt;
      Test &lt;/iframe&gt;
      
      
      &lt;p&gt;… Enjoy&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/JenniferSmithsBlog/~4/14W6duL3cNc" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.jennifersmith.co.uk/blog/2010/06/06/google-letter-template</feedburner:origLink></entry>
  <entry>
    <title>Spark codec for OpenRasta</title>
    <link href="http://feedproxy.google.com/~r/JenniferSmithsBlog/~3/dJBno_CL4-Y/spark-codec-for-openrasta" />
    <updated>2009-05-29T00:00:00-05:00</updated>
    <id>http://www.jennifersmith.co.uk/blog/2009/05/29/spark-codec-for-openrasta</id>
    <content type="html">
      &lt;h3&gt;Update&lt;/h3&gt;
      
      &lt;p&gt;I haven’t really had the chance to support this too much due to various
      reasons (new job, laptop with vmware image dying, supreme laziness and
      attention span issues). Anyway, the project is now on github and (thanks
      to some work from Lee Henson) is the most up to date:
      &lt;a href="http://github.com/jennifersmith/openrasta-sparkcodec"&gt;http://github.com/jennifersmith/openrasta-sparkcodec&lt;/a&gt;
      Hope that helps you all out and thanks for your interest!&lt;/p&gt;
      
      &lt;hr /&gt;
      
      &lt;p&gt;It all started after I went to Sebastian Lambla’s talk on
      &lt;a href="http://www.ohloh.net/p/openrasta"&gt;OpenRasta&lt;/a&gt; at a
      &lt;a href="http://vbug.co.uk/"&gt;VBUG&lt;/a&gt; event. I really got into the framework - it
      felt to me as a cleaner and lighter way to think about MVC when compared
      to ASP.NET MVC. At the same time, I really wanted to have look at the
      &lt;a href="http://dev.dejardin.org/"&gt;Spark View Engine&lt;/a&gt; as I think the syntax is a
      breath of fresh air and welcome relief from all the tagsoup we seem to
      get into when writing MVC views. When I asked ‘Is there a Spark codec
      for OpenRasta?’ I had the reply ‘No, but why don’t you write one’. Never
      one to back down from a challenge I did. And you can download it from
      &lt;a href="http://code.google.com/p/openrastasparkcodec/"&gt;GoogleCode&lt;/a&gt; (though I
      would prefer you grab the trunk cos the latest version is probably
      already out of date!).&lt;/p&gt;
      
      &lt;h3&gt;OpenRasta meet Spark, Spark meet OpenRasta&lt;/h3&gt;
      
      &lt;p&gt;If you are not familiar with either of these projects, then this project
      probably means nothing to you. If you get one and not the other then
      read on.&lt;/p&gt;
      
      &lt;p&gt;&lt;strong&gt;OpenRasta&lt;/strong&gt; ‘is a Resource-Oriented framework to build
      MVC-style applications on asp.net 2 and above.’ (I copied this from
      &lt;a href="http://www.ohloh.net/p/openrasta"&gt;here&lt;/a&gt;). Easiest way to get into it I
      think is to either hear a talk on it, or use &lt;a href="http://svn.caffeine-it.com/openrasta/trunk/doc/content/Tutorials/Create-First-Site.html"&gt;this
      tutorial&lt;/a&gt;.&lt;/p&gt;
      
      &lt;p&gt;The &lt;strong&gt;Spark View Engine&lt;/strong&gt; is written for ASP.NET MVC and provides a
      slightly alternative syntax for specifying views. Basically it uses an
      extended set of attributes and tags on top of plain old HTML to let you
      do crazy things like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span class="nt"&gt;&lt;viewdata&lt;/span&gt; &lt;span class="na"&gt;products=&lt;/span&gt;&lt;span class="s"&gt;"IEnumerable[[Product]]"&lt;/span&gt;&lt;span class="nt"&gt;/&gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&lt;ul&lt;/span&gt; &lt;span class="na"&gt;if=&lt;/span&gt;&lt;span class="s"&gt;"products.Any()"&lt;/span&gt;&lt;span class="nt"&gt;&gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&lt;li&lt;/span&gt; &lt;span class="na"&gt;each=&lt;/span&gt;&lt;span class="s"&gt;"var p in products"&lt;/span&gt;&lt;span class="nt"&gt;&gt;&lt;/span&gt;${p.Name}&lt;span class="nt"&gt;&lt;/li&gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&lt;/ul&gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&lt;else&gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&lt;p&gt;&lt;/span&gt;No products available&lt;span class="nt"&gt;&lt;/p&gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&lt;/else&gt;&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Simple, readable and pretty cool I think, when your alternative is a big
      old mess of tagsoup. Out of all the alternatives, it is definitely the
      clearest and most logical I have seen so far. Any
      web-designers/front-end devs out there want to tell me if they agree?&lt;/p&gt;
      
      &lt;h3&gt;Using the Codec from OpenRasta&lt;/h3&gt;
      
      &lt;p&gt;In a later post I might choose to
      talk about my design approach, but when it came to OpenRasta, this
      approach was very much ‘Copy what the webforms codec does’. So to use
      the spark view engine, you start by adding this to the top of your
      configuration block:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span class="n"&gt;ResourceSpace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Uses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SparkCodec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;This does some behind the scenes magic to register all the stuff you
      need to use the SparkCodec. Next, for the resources you want to render
      using spark, you configure along the lines of:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;ResourceSpace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Uses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SparkCodec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;ResourceSpace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Has&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResourcesOfType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AtUri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/shoppinglist"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandledBy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AndRenderedBySpark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"shoppingList.spark"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;… note the ‘AndRenderedBySpark’ extension method there which passes in
      the name of the spark template you are going to use. The Spark lookup
      procedure takes in a root folder as part of its configuration and I have
      defaulted this to ‘Views’. So ShoppingList.spark must exist in the
      folder views/. There you are - you are all ready to start getting Sparky
      with your OpenRasta. Note that OpenRasta allows you to mix and match
      your codecs - so you can just have one or two views rendered using Spark
      if that is what you want.&lt;/p&gt;
      
      &lt;h3&gt;Extensions to the Spark syntax&lt;/h3&gt;
      
      &lt;p&gt;One of the
      things I liked about OpenRasta was some of the markup extensions that it
      contains. I.e. you can do something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;   &lt;span class="n"&gt;Xhtml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextBox&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&gt;&lt;/span&gt;&lt;span class="n"&gt;MyResource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;And through the magic of expressions, the output html is something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"MyResourceTypeName.Name"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Whatever the current value of the name is"&lt;/span&gt;&lt;span class="nt"&gt;/&gt;&lt;/span&gt;   
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;The base view class from which all spark views inherit
      (SparkResourceView) exposes the Xhtml interface so you can just use the
      extension method as is. However, I thought that seeing as the point of
      the Spark syntax is to hook tidily into html, I wanted to provide an
      alternative. So I have used the extensions facility in the Spark View
      Engine (which was simple once I got the hang of it), to add some custom
      attributes. Now if your view contains the following:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&lt;viewdata&lt;/span&gt; &lt;span class="na"&gt;resource=&lt;/span&gt;&lt;span class="s"&gt;"Customer"&lt;/span&gt;&lt;span class="nt"&gt;/&gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&lt;input&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"resource.Name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;anotherattribute=&lt;/span&gt;&lt;span class="s"&gt;"somethingelse"&lt;/span&gt;&lt;span class="nt"&gt;/&gt;&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;The output becomes:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Customer.Name"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Fred"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;anotherattribute=&lt;/span&gt;&lt;span class="s"&gt;"somethingelse"&lt;/span&gt;&lt;span class="nt"&gt;/&gt;&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Similarly to hook into the URI-resolving stuff in OpenRasta you can have
      something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&lt;viewdata&lt;/span&gt; &lt;span class="na"&gt;resource=&lt;/span&gt;&lt;span class="s"&gt;"Customer"&lt;/span&gt;&lt;span class="nt"&gt;&gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&lt;a&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s"&gt;"resource"&lt;/span&gt;&lt;span class="nt"&gt;&gt;&lt;/span&gt;Click here to view the customer&lt;span class="nt"&gt;&lt;/a&gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&lt;a&lt;/span&gt; &lt;span class="na"&gt;totype=&lt;/span&gt;&lt;span class="s"&gt;"IEnumerable&lt;Customer&gt;"&lt;/span&gt;&lt;span class="nt"&gt;&gt;&lt;/span&gt;Click here to view all customers&lt;span class="nt"&gt;&lt;/a&gt;&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;On render, this figures out the URIs based on what you set up for the
      resources in question as part of the configuration. Anyway, this syntax
      is far from complete - it works enough to just about power the demo
      application that I packaged with the codec source so I suggest you take
      a look at this for more pointers.&lt;/p&gt;
      
      &lt;h3&gt;Futures&lt;/h3&gt;
      
      &lt;p&gt; This started as a small
      pet project but if you want to give me a hand please please do catch up
      with me on &lt;a href="http://twitter.com/JenniferSmithCo"&gt;twitter&lt;/a&gt;. The same goes
      for any other feedback (apart from ‘it’s crap’ - that would not be very
      nice). I have a small idea for an application that I wanted to have a go
      at with OpenRasta and as I get into implementing this I am sure I will
      come up with a few more extensions to the syntax. Now it is far too hot
      and I am going out to get an ice lolly.&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/JenniferSmithsBlog/~4/dJBno_CL4-Y" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.jennifersmith.co.uk/blog/2009/05/29/spark-codec-for-openrasta</feedburner:origLink></entry>
  <entry>
    <title>Extension method assertions versus standard assertions</title>
    <link href="http://feedproxy.google.com/~r/JenniferSmithsBlog/~3/s3fjOanrSuY/extension-method-assertions-versus-standard-assertions" />
    <updated>2009-04-24T00:00:00-05:00</updated>
    <id>http://www.jennifersmith.co.uk/blog/2009/04/24/extension-method-assertions-versus-standard-assertions</id>
    <content type="html">
      &lt;p&gt;I have been playing around lately with assertions in my tests and
      figuring out the best (funnest?) way to work with them. Within the
      context of BDD style testing there seems to have emerged a style for
      using extension methods for asserts. As I found out at the
      &lt;a href="http://www.skillsmatter.com" title="SkillsMatter"&gt;SkillsMatter&lt;/a&gt;
      &lt;a href="http://nbehave.org/" title="NBehave bdd framework"&gt;NBehave&lt;/a&gt; talk, NBehave
      comes with a set of extension method wrapped assertion methods so I
      guess that is where the link comes from perhaps.&lt;/p&gt;
      
      &lt;h3&gt;What do I mean by extension method assertions?&lt;/h3&gt;
      
      &lt;p&gt;OK so a normal vanilla unit test might
      check that the conditions of the test are met as follows:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foobar123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Or if you have been bitten by the fluent bug:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;That&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EqualTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;That&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foobar123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NotNull&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Anyway, if you use the extension methods approach you will typically
      have defined somewhere something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;AssertionExtensions&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;ShouldBe&lt;/span&gt;&lt;span class="o"&gt;&lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;That&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InstanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
      
              &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;ShouldBeNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;That&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;Leaving your asserts looking something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;   &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShouldEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="n"&gt;foobar123&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;You could even read that back and it might make some sort of sense: “Foo
      shoud equal bar”. “Foobar123 should not be null”. As far as C# goes,
      that is pretty damn readable (I see you Ruby-heads smirking at the
      back!).&lt;/p&gt;
      
      &lt;h3&gt;Taking it a bit further&lt;/h3&gt;
      
      &lt;p&gt; I have started to get into BDD style
      testing for project number one (well, the parts that aint smart-ui
      anyway), and I have got slightly carried away with my extension method
      asserts. As each test is testing a different outcome of the behaviour
      under test, I think the best tests only have one line of code in them.
      Something like:&lt;/p&gt;
      
      &lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;TestFixture&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;When_user_sends_article_to_a_friend&lt;/span&gt; 
      &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="p"&gt;....&lt;/span&gt;
             &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
             &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Email_is_sent_to_the_friends_address_via_email_service&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
             &lt;span class="p"&gt;{&lt;/span&gt;
                     &lt;span class="n"&gt;EmailService&lt;/span&gt;
                          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EmailWasSent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;To&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fred@flintstone.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
             &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;/pre&gt;
      &lt;/div&gt;
      
      
      &lt;p&gt;(For the record my assertions have never got to more than one method,
      but it would be possible right!). Arguably you could just say this is a
      bit of syntactic sugar, but I think it hides the complexity of what it
      means to satisfy these requirements… which I believe is A Good Thing.
      At first glance, I should be able to read this code out loud and have
      half a chance at figuring out the intentions of what was supposed to be
      tested. I don’t need to worry whether EmailService is a
      stub/mock/stubbed-mock(!) - and indeed if it starts life as a mock then
      progresses to a concrete, this test doesn’t need to change.&lt;/p&gt;
      
      &lt;h3&gt;It’s not for BAs&lt;/h3&gt;
      
      &lt;p&gt;One argument I have heard for extension-method assertions is
      that it ‘makes it readable for business analysts’. Now, if your business
      analyst happens to be a former programmer, you might be on to something.
      However, that is not usually the case, our BAs can’t understand it and
      such a claim rather detracts from the usefulness of these methods.
      Really what we should be thinking about is the how readable extension
      method assertions are for us! If someone asks me ‘what does this
      function do’ and I wrote it about 10 days ago (this is about the maximum
      time I can remember anything about something I have created), I can find
      out easily and quickly. Similarly, the poor dev who comes after me could
      probably have a go at comprehending it too.&lt;/p&gt;
    &lt;img src="http://feeds.feedburner.com/~r/JenniferSmithsBlog/~4/s3fjOanrSuY" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.jennifersmith.co.uk/blog/2009/04/24/extension-method-assertions-versus-standard-assertions</feedburner:origLink></entry>
</feed>

