21 Jul 2010

Code Reviews

Earlier this week, I had a code review.  It was only my third code review in over 5 years of working as a software developer.  I had to repeatedly nag to before it actually happened.  It says something for the state of software development, and the lip service paid to developing quality software solutions, that is by no means uncommon for developers in Northern Ireland.  Note, in the past 5 years, I have worked for 3 local companies, one an international IT Services company, all of whom pride themselves on delivering quality code.   In my last job, I was never given a code review.  Never.


My view on code reviews, and the less formal pair programming, is that it is a fundamental learning activity for people who make their living cutting code. And I’m not alone in that view.  How can you improve if you don’t know what you are doing wrong?  Code reviews are also the best way to ensure the quality of the code that you write.  Nothing focuses the mind on writing quality code more than the knowledge that you will be held accountable for the code you check in. 

If code reviews aren’t happening regularly, the code quality goes out the window.  And if your company isn’t doing code reviews formally, you need to start doing them informally. My biggest regret from my last job is that I didn’t arrange informal code reviews. 

Fevered Coding

I am suffering a nasty chest infection, and it has been tough getting to bed over the last few days.  As a result, I have been staying up late, coding on a few random scripts. 

The result of last night’s fevered coding was a PowerShell script to return the ratings for various TV shows on IMDB.  This is my first attempt at the script, and it makes use of the IMDB web service provided by Dean Clatworthy.

The script consists of two main parts.  The first is a call to the web service, and returning the details for the specified TV show, using an instance of the .NET WebClient.

  1: function Get-ShowDetails
  2: (
  3:   [string]$showName = $(Throw "TV show name is required!")
  4: )
  5: {  
  6:   $webClient = new-object System.Net.WebClient
  7:   $webClient.Headers.Add("user-agent", "PowerShell Script")
  8:   $queryString = $showName.Trim().Replace(" ", "+")
  9:   $query = [string]::Format("{0}={1}", $queryUrl, $queryString)   
 10:   $showDetails = $webClient.DownloadString($query)
 11:   return $showDetails
 12: }

The second part of the script extracts the rating details for the show from the plain text returned by the web service, using a regular expression:

  1: function Extract-RatingDetails
  2: (
  3:   [string]$showName = $(Throw "TV show name is required!"),
  4:   [string]$showDetails = $(Throw "TV show details are required!")
  5: )
  6: {  
  7:   $regexPattern = '"rating":"([a-z\\\/\.0-9]+)","votes":([0-9\"]+)'
  8:   $regexMatcher = [regex] "$regexPattern"
  9:   $matches = $regexMatcher.Matches($showDetails)
 10:   $showInfo = $null  
 11:   
 12:   foreach ($match in $matches)
 13:   {
 14:     if ($match.Success)
 15:     {
 16:       # To return all of the matched expression, use $match.Groups[0].Value
 17:       $rating = $match.Groups[1].Value
 18:       
 19:       if($rating -eq "n\/a" )
 20:       {
 21:         $rating = 0;
 22:       }
 23:       
 24:       $votes = $match.Groups[2].Value.Replace('"', '')      
 25:       $showInfo = New-Object PSObject
 26:       $showInfo | Add-Member NoteProperty "ShowTitle" $showName
 27:       $showInfo | Add-Member NoteProperty "Rating" $rating
 28:       $showInfo | Add-Member NoteProperty "Votes" $votes      
 29:       break;
 30:     }
 31:   }
 32:   
 33:   return $showInfo
 34: }

This isn’t a finished script, though it does work.  There is a limit to the number of calls that can be made to the web service (30 per hour), which means that I will have to revisit the script and instead of calling the web service, scrape the details of each TV series from the IMDB website. 

Download the current script from here.  If you have any comments, I would appreciate all feedback.  Thanks.

Also, just to note that the code for this post was highlighted using the Source Code Formatter for Windows Live Writer.

Code Syntax Highlighter for Code Snippets

After my last blog post (and the first to contain a decent code snippet!), I wasn’t too happy with the way that the code snippet was rendered in the RSS feed:

I am currently using the Insert Code for Windows Live Writer plug-in, which has a decent rating and reasonable reviews.  I decided to check out the other possible code syntax highlighters.

As always, Scott Hanselman has already posted on this topic, and he came out in favour of Syntax Highlighter, a JavaScript library.  It looks great (see here), but there are a number of other options. These include:

  1. BlogTrot Code Window – An online tool to generate HTML for your code snippet.
  2. NeatHighlighter - Similar to BlogTrot.
  3. Google Code Prettify – A JavaScript library that does a similar job to the Syntax Highlighter.  There is a Code Prettify for Windows Live Writer plugin that can be used to automatically surround your code snippets with <pre> tags for use with the Prettify library.  Unfortunately, Google does not host the Prettify library, which would have been a major plus for me
  4. Source Code Formatter – Another code syntax highlighter for Live Writer, similar to the plug-in I am already using.

I have a number of code based posts that will appear over the next few weeks, so I will try out each of the above methods in a separate post, and post again on this topic to give you some feedback and my preferred method of displaying code snippets.  If you have any other methods of code highlighting that you would like me to consider, please get in touch.

19 Jul 2010

Use a Visual Studio Macro to Insert Copyright Headers into Source Files

In work today, I needed to add a copyright header to a large number of C# classes.  Instead of simply pasting the header across the 20+ files, I took a look at the various tooling options.

The Code Header Designer looked like a possible option. However, I wanted something lightweight and easily configurable, so I settled on using a VS macro to insert the headers.  A quick Google revealed a number of example macros already written to do exactly what I required.  This is my version of James Welch’s code:

 

I then followed the instructions in this article to add a context menu entry to call the macro by right-clicking the target file.  This resulted in the following header comment being inserted in each file:

   1:  // <copyright file="Sample.cs" company="My Company Name">
   2:  // Copyright (c) 19/07/2010 23:01:05 All Right Reserved
   3:  // </copyright>
   4:  // <author>Andy Parkhill</author>
   5:  // <date>19/07/2010 23:01:05 </date>
   6:  // <summary>Class representing a Sample entity</summary>

It is worth noting that by taking 20 minutes to research the possible options, I was able to find modify an existing macro that is significantly faster than copying and pasting boilerplate text across the various files, and that can be modified for other uses.

If I had to do this on a new project, I would instead create a custom class template specifically for the project with required header and class structure.

18 Jul 2010

Cycle Courier Service in Belfast

Came across this cycle courier service, Cycle Send It, which has started up in Belfast.  Very cool.  Courtesy of someone on Twitter… Dawn maybe?

9 Jul 2010

The Weekly Links 10

This is the 10th post in an ongoing series of weekly roundups of links useful to developers.

Tools

  • WebSharper – A web development framework based on F#.  Yes, a functional programming language.  Absolutely insane. The standard version is a free download.
  • WebMatrix – Scott Guthrie announces the release of a new light-weight web development platform from Microsoft.
  • Scripting Toolkit ISO – SAPIEN Technologies has released a free “Scripting Toolkit” CD that contains copies of all of their free tools, trial versions of their paid applications, samples of ebooks and training videos.
  • Knockout – A UI Library for JavaScript.
  • Microsoft Download Centre – Get email notifications of all the recent releases from Microsoft.  Link courtesy of Simon.

Information

Events

You can now check out developer events in Northern Ireland using my calendar.

It is obviously the summer, as there are NO local events in the coming week.

8 Jul 2010

Sons of Anarchy… In Belfast?

I recently started watching Sons of Anarchy, an American TV series, after my brother sang its praises for the past few months.  I think it is also currently showing on terrestrial TV here in the UK as well. 

Unbelievably, they will be filming in Belfast for the third season of the show, and are looking to cast lookalikes for the leading characters in the series.   Check out the full story here.  If you haven’t been following the show, the bikers finance themselves with some gunrunning on the side, and have links to an IRA offshoot, blah, blah.  It says something for the quality of the show that despite a tenuous grasp of the realities in Norn Iron, and some very dodgy Irish accents (ah, begorrah!), I still enjoy watching the SOA.

Thanks to Paddy for the heads up!

5 Jul 2010

Graduating…

My younger brother John graduated today from the University of Ulster, at the Magee campus.  It felt very strange to be at the graduation ceremony with my folks; the last graduation I was at was my own, back in 2005.  Actually, the  two ceremonies were separated by exactly 5 years; it feels considerably longer.

The ceremony was graced with the presence of James Nesbitt, the UUC’s new Chancellor, who had the right mix of gravitas and humour.  An honorary doctorate was awarded to Colin Bateman, a local crime fiction author.  I’ve never been a fan of his books, but his acceptance speech was quite humorous.  There was a lovely moment when a degree was awarded to the parents of a student who sadly passed away before graduating, and the entire assembly rose to give them a standing ovation.  I actually really enjoyed being there, and I’m very glad my brother invited me.

Today’s ceremony in Derry was in direct contrast to my own graduation at QUB.  The ceremony there came across as very smug, and was incredibly tedious.  Some nobody from industry I had had never heard off before, and have never heard of since, was awarded an honorary degree, and he preceded to bore us to death in his acceptance speech (think Father Ted acceptance’s speech on winning the Golden Cleric…).  People who graduate from QUB have the nasty habit of looking down on anyone who went to to UU; after speaking to some of my brother’s friends today, and hearing about some of the work going on at the UU, I can’t see why.

Automation, and Gaining Time for Fun Stuff…

 

In a recent post, I foolishly promised to get up at 5am every morning to work on personal projects and to write more blog posts.  Dear reader, do you really need to ask how many times over the last two weeks I have welcomed daybreak by typing away on my laptop?  Yeah, that's right, not once.

This won’t be a surprise to anyone who knows me- I’m just not an early riser.  However, I am still committed to spending more time on my personal projects.  So over the past week, I have tried to use Launchy, the open-source program launcher, as much as possible to automate the routines actions that I repeat daily, and that just eat up mouse clicks and keystrokes.

One common action that I do often in work is to use Outlook to setup calendar appointments (shared with my Google Calendar using the Google Calendar Sync), and to send myself email reminders. 

I took a look at the Outlook shortcuts to automatically create a new email message, and added the command below in the Runner plugin (see previous post for details of adding commands)

Name Program Arguments
Email C:\Program Files\Microsoft Office\Office\OUTLOOK.EXE /c ipm.note

I then wanted to extend this to create new email messages to be sent to specific email addresses, i.e. my own webmail account.   To do this, you simply change the arguments to be ‘/c ipm.note /m <insert email address>’.

Similarly, the argument to create a new appointment is ‘/c ipm.appointment’.


So, by simply typing ‘Email-Reminder’ into the Launchy window, I can create a new mail message to send to myself.


Expect a number of other posts in the near future around scripting and productivity.

2 Jul 2010

The Weekly Links 9

This is the 9th post in an ongoing series of weekly roundups of links useful to developers.

Tools

  • SteakyBeak – Is an advanced logging module for web apps that will log each request to your web server and also provide a easy interface to view these requests. Base on the ever useful NLog logging framework.
  • PowerGUI Visual Studio – Quest have released a version of the PowerGUI IDE for PowerShell that integrates into Visual Studio.  Other options to develop and execute PowerShell scripts in VS include the VS Command Shell, and PowerConsole.
  • TOAD Extension for Visual Studio – Again from Quest, a extension to manage Oracle databases within VS in the same way as SQL Server databases.
  • Enterprise Localization Toolkit – An oldie but a goodie.  A colleague in work (Thanks, Mike!) came across this framework from Microsoft for resource string management via a database.
  • Posterous – the easiest way to blog.  Create a beautiful looking blog with the minimal effort.  I’m seriously considering switching to use it… Watch this space.

Information

Events

You can now check out developer events in Northern Ireland using my calendar.

There are no developer events in the coming week.

1 Jul 2010

Goodbye Last.fm, Hello Jango

No doubt I coming late to this party, but a colleague shared Jango, the advertising-supported Internet radio website.  I immediately feel in love with it.   A nice clean website, easy to navigate, and a great selection of artists and tunes.  I’ve just finished uninstalling the Last.fm dektop client; it is history.  If I need some tunes now, I just have to click on the Jango bookmark.  Which I have just done, and I’m now enjoying some Stone Temple Pilots.