Friday, June 29, 2012

Using C# Named Parameters for Code Readability

In C# 4.0, Microsoft introduced a feature called 'Named Parameters', which lets you refer to parameters in a method that you are calling in a syntax like this:
public void MyMethod(firstParameter: firstValue, secondParameter: secondValue)

This lets you call the method with parameters in a different order, and also helps to facilitate optional parameters. Aside from these benefits, I've come to like using them to improve code clarity, particularly in unit tests.
Lets say you have a method like this one:
public decimal CalculateFuelCostPerKm(double kilometresTravelled, double litresInFuelTank, decimal costToFillTank)
If you were to call that method, you might do something like this:
CalculateCostPerKm(500D, 60D, 72.58M);

However if you are reading that and don't know the parameters, you're going to have to dive deeper to understand it. A way to improve it might be:
var kilometresTravelled = 500D;
var litresInFuelTank = 60D;
var costToFillTank = 72.58M;
CalculateCostPerKm(kilometresTravelled, litresInFuelTank, costToFillTank);

Which is much clearer, but is a fair bit of effort. But with C# named parameters you can do this:
CalculateCostPerKm(kilometresTravelled: 500D, litresInFuelTank: 60D, costToFillTank: 72.58M);

Simple to do, and clear to read. In fact, this is probably one of the rare situations where C# being written
more like Objective-C is a good thing. Take this example for creating an alert dialog in the iOS SDK:
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
Which you would call like:
[[UIAlertView alloc] initWithTitle:@"Error" message:@"You stuffed up" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

Or in C# you might do something like this (ignoring the delegate business):
UIAlertView.InitWithTitle("Error", "You stuffed up", myDelegate, "OK", null);

As you can see, the verbose nature of Objective-C makes for clearer code in this instance, however, we could write the C# method like:
UIAlertView.InitWithTitle(title: "Error", message: "You stuffed up", delegate: myDelegate, cancelButtonTitle: "OK", otherButtonTitles: null);

Which makes it just as clear.
I've found this technique particularly useful in unit tests, which often tend towards using constant values. For instance I might often have a method to setup a certain state in my stubs where I take in the important parameters required, this technique makes this much easier to read.
I’d love to hear your thoughts on this technique, either in the comments on this post or via Twitter (@rodh257)

Sunday, June 17, 2012

What's been happening?

It's been a fair while since I last posted to this blog, so as I start lining up a few more blog posts I thought I'd make a quick post to let you know what I've been up to over the last 6 months.

The last post I made was about an experimental technique I'd created for running Revit API add-ins as scheduled tasks. This was a part of my Autodesk University 2011 virtual presentation, which is online now. If you don't have access to the AU Online website I've uploaded the slides and handout and the sample code for the Revit Remote Boot and Html Server Admin are on Github. This was a pretty niche topic, but it was fun to dive into, and I hope people got some good use out of it. It's worth noting that Revit Server 2013 is now out, and makes a few changes which could affect this code (I'd be interested to hear if you were using this code).

After this post was made I have started work with a new company, Envoy Advanced Technologies. Envoy are an agile software development firm that primarily work on web-based business applications. This has given me the opportunity to work on some big software systems with some really brilliant developers using some great development techniques (as a shameless plug, if you're looking to have a software system developed, whether it involves Revit or not, you could do much worse than to get into contact with Envoy).
Aside from the larger projects, I recently worked on the iOS and mobile web apps for this years Revit Technology Conference. Envoy were a sponsor of RTC AUS and the USA version of the conference is starting in a couple of weeks time, if you haven't had a look yet I recommend you head to their website and look over the schedule. It looks to have some really good classes, including one on Revit Server.

This change in employer has meant that I've not been working as much on Revit add-ins as I used to, hence the lack of posts on the topic. I am however still tinkering away with it in my spare time, and have a few ideas floating around my head for nifty ways to use the new 2013 API features which I hope to post about in the future.
In my spare time, I've released an update to Convoy, my iOS app to make it easier to communicate when travelling in a group of vehicles. Primarily it includes a graphics overhaul which is thanks to Vanessa Grixti.

This and other projects I've been working on are great as they give me the chance to dive into some product development/management, which is an interest of mine, I've been blogging about these sorts of topics over at Cejest, but I'm planning on moving those posts over to this blog.

In addition to these posts, I'll be writing more about general software development topics, both technical (C#, ASP.NET MVC, iOS etc) and non-technical, including posts on workflows, development practices etc. A reminder that if you aren't interested in reading this, and just want Revit content, there's a link to a Revit topics only RSS feed on the right of the blogs main page.

If you've got any feedback or questions feel free to email me ( or ping me on twitter (@rodh257).