Wednesday, 21 November 2012

Website Update

It's been over 2 years in it's current form and I really must get around to updating my website. As you can see, I went for a really retro "early days of the web" approach; possibly even with the caveat that it's best viewed in a text only browser. So moving on, time for an update - I've not drawn up any designs, so that should be interesting but the site will probably just be an aggregation of me on the web. Think I'm going to write the first version in .NET, using MVC4 and Razor and whilst I'm not sure that there will be much demand, I'll host the code on my github account So watch this space for updates!

Tuesday, 20 November 2012

Add NuGet Reference in Resharper

Today Jetbrains announced on their blog that they've released a resharper nuget package that will obtain project references via nuget, rather than making direct references to the locally installed code.

As we are currently in the early stages of transitioning from VS2008 to VS2012 we aren't ready to start using NuGet(*) but I think I'll be grabbing this plugin for when we do.

(*) In my last role we were using VS2010 and making heavy use of NuGet so can't wait to get back into being able to use it again!

Saturday, 10 November 2012

Project Euler

Over the past couple of years I keep on coming back to Project Euler, it's nice when you just want a quick challenge, or to try your maths skills. It's highlighted how rusty some of my concepts were, and I've learnt a whole load of new ones along the way. I really like the way that the example problem given is quite quick to determine using brute force, but the desired answer always needs a more considered approach.

I've just completed problem 26, to determine value of d (1/d) where d < 1,000 and resulted in the largest recurring cycle of the decimal fraction. My first (working) solution completed the task in 2.8 seconds, well within the proposed ideal time frame. What's really helpful is then reading through the forum of previous solutions you can pick up some ideas that start to bring the entire process down to under a second!

You can see my progress in the graphic on the right hand side, or can check my progress on the project euler site.

Friday, 9 November 2012

To code review or not to code review?

There are many articles on the web about how to do good code reviews, with mostly all of them either discussing their merits or how to obtain maximum value from them.

In a new role I've recently helped introduce and roll out the agile process and to start with as part of each story we always added a task to "code review" all work. We made sure that differences from source control were used to make sure only the changes made were reviewed, made sure that everyone had chance to partake in reviews so it wasn't just one or two people providing all the feedback on the other team members work. It worked well, the reviews were productive and provided really good feedback for everyone.

But there was something about them that didn't feel right for the entire team, but it was the sprint planning process of tasking the stories that gave us the answer to what was bothering us. For every story we added the code review task to the END of the story, which of course by it's very nature was when all the development had been done! We analysed most of the feedback that came out of the review process and realised that, similar to the wedding singer, that information would have been useful to us "yesterday".

So as a little experiment we added a task right at the beginning of the story that involved at least 2 developers, sometimes a tester and for more complex stories, the entire team. Outside of the task planning session the developer(s) assigned the task spend 30-60 minutes detailing the approach they intend to take to solve the story - the other participants in the process get to question the approach and by the end of the discussion we've normally ironed out all the bumps. During these experimental stories we kept the code review task, but the initial discussion task along with taking a TDD approach coding against Gherkin provided by the test team we quickly identified that the code reviews had become redundant.

We've now taken the decision to remove the code review task completely and haven't noticed any decrease in code quality or maintainability. The number of issues reported in the produced code has stayed pretty constant, but we're no longer have to spend any time at the end of the story reworking any code as a result of review feedback!

Thursday, 8 November 2012

Reducing code noise for argument checking / assignment

Leading on from the previous blog post on reducing code noise when checking for null parameters, these help functions can be utilised to further reduce code noise that generally occurs in class constructors. Most constructors are purely made up of the following duplicated checking / assignment code.

public class MyClass
{
private readonly object var1;
private readonly object var1;
 
public MyClass(object arg1, object arg2)
{
if (arg1 == null)
{
throw new ArgumentNullException("arg1");
}
 
if (arg2 == null)
{
throw new ArgumentNullException("arg2");
}
 
this.var1 = arg1;
this.var2 = arg2;
}

Again putting together a small helper function, making use of the previous Throw class can remove 90% of the previous argument checking / assignment code resulting in:

public class MyClass
{
private readonly object var1;
private readonly object var1;
 
public MyClass(object arg1, object arg2)
{
this.var1 = ReturnParameter.OrThrowIfNull(arg1, "arg1");
this.var2 = ReturnParameter.OrThrowIfNull(arg2, "arg2");
}

Get the code from GitHub.

Reducing code noise when checking for null parameters

The problem with writing defensive fail early code, it doesn't take long before methods are 70% argument checking, 30% logic:

public void Method(object arg1, object arg2)
{
if (arg1 == null)
{
throw new ArgumentNullException("arg1");
}
 
if (arg2 == null)
{
throw new ArgumentNullException("arg2");
}
 
...
...
...
}

Code contracts can be a great way to move code out of the method body, but how can you clean up the code just through traditional refactoring? For the last few projects I've worked on, I've been using a small helper class that abstracts all the conditional code out, leaving just a single line call for each check you need.

public void Method(object arg1, object arg2)
{
Throw.IfNull(arg1, "arg1");
Throw.IfNull(arg2, "arg2");
 
...
...
...
}

String parameter checking can wrap up even more conditional logic. The following code throws an ArgumentNullException if the string is null, or just an ArgumentException if it is empty or whitespace.

Throw.IfNullEmptyOrWhitespace(stringValue, "stringValue");

Get the code from GitHub.