Behaviour-Driven Development with StoryQ

Over the weekend of the 24th and 25th of July I gave two presentations at the South Australian Code Camp. The first was on the subject of behaviour-driven development, with examples using StoryQ. Here are my slides and sample application: 

Slide1 Slide2 Slide3
Slide4 Slide5 Slide6
Slide7 Slide8 Slide9
Slide10

Example Application


mongoDB Management Studio

This weekend I have been in Sydney at the MS Web Camp, learning about web application development. At the end of the first day we came up with application ideas and pitched them. My idea was to build a web management application for mongoDB.

mongoDB

I pitched my idea, put down the microphone, and then someone asked, “what’s mongo?”. Good question. MongoDB is a document database that stores JSON style documents. This is a JSON document for a tweet from twitter:

 db.tweets.find()[0]
{
        "_id" : ObjectId("4bfe4946cfbfb01420000001"),
        "created_at" : "Thu, 27 May 2010 10:25:46 +0000",
        "profile_image_url" : "http://a3.twimg.com/profile_images/600304197/Snapshot_2009-07-26_13-12-43_normal.jpg",
        "from_user" : "drearyclocks",
        "text" : "Does anyone know who has better coverage, Optus or Vodafone? Telstra is still too expensive.",
        "to_user_id" : null,
        "metadata" : {
                "result_type" : "recent"
        },
        "id" : {
                "floatApprox" : 14825648892
        },
        "geo" : null,
        "from_user_id" : 6825770,
        "search_term" : "telstra",
        "iso_language_code" : "en",
        "source" : "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>"
}

A mongodb server can have many databases, each database has many collections (instead of tables) and a collection has many documents (instead of rows).

Development

Day 2 of the Sydney MS Web Camp was allocated to building our applications. First thing in the morning I identified the stories that I wanted to implement:

Scenario: View databases
Scenario: View Collections in a database
Scenario: View Documents in a Collection
Scenario: Delete a Collection
Scenario: Delete a Database
Scenario: Delete Documents

Over the course of the day the team (3.5 developers) implemented all of the planned stories (except ‘delete a database’) and also implemented the following:

Scenario: Create Database
Scenario: Create Collection

Lessons Learned

I’m new to MongoDB and in the past I have only accessed it from Ruby (for my hare-brained scheme). When it came to implementing our MongoDB management studio we discovered that their is no official MongoDB driver for .NET. We chose to use NoRM, honestly just because it was the only one I had heard of. NoRM was a challenge. I think it is a fine library but it is focused on mapping strongly typed objects to MongoDB. For our application we had no prior knowledge of the types that would be in the MongoDB database so NoRM was probably a poor choice.

Here are some screens (click to enlarge):

image image


Unnamed Refactoring

This post is a message in a bottle. I cast it into the sea in the hope that it will one day return to me, stuffed to the cork with enlightenment. Yesterday I  tweeted,

what is the name of the pattern where you replace a multi-way conditional with an associative array?

I said ‘pattern’ but I meant ‘refactoring’. Anyway, no one replied so I will describe the refactoring here.

Programmers tend to think imperatively, which leads to code such as:

public int GetPopulation(string country)
{
   if (country == "Australia")
   {
       return 22360793;
   } else if (country == "China")
   {
       return 1324655000;
   } else if (country == "Switzerland")
   {
       return 7782900;
   }
   else
   {
       throw new Exception("What ain't no country I ever heard of. They speak English in what?");                    
   }
}

which is horrid. We can write a cleaner version, replacing the multi-way conditional with an associative array, treating the conditional as data:

public int GetPopulation(string country)
{
   if (!Populations.ContainsKey(country))
       throw new Exception("The population of " + country + " could not be found.");

   return Populations[country];
}

private Dictionary<string, int> Populations
{
   get
   {
       return new Dictionary<string, int>
           {
               {"Australia", 22360793},
               {"China", 1324655000},
               {"Switzerland", 7782900}
           };
   }
}

 

Does this refactoring already have a name? Otherwise, I propose

Replace multi-way conditional with associative array