Give formatting its own line

I got bitten by this not once but twice on Sunday as I hurriedly worked on a new phone app. Can you spot the difference between the the following two lines?

 Uri uri = new Uri(string.Format("/View/DataView.xaml?collection={0}&id={1}", collection, currentItem.getID(), UriKind.Relative));

Uri uri = new Uri(string.Format("/View/DataView.xaml?collection={0}&id={1}", collection, currentItem.getID()), UriKind.Relative);

Typically I like to put any String.Format() call on its own line because they tend to be long and sometimes complicated. This is an example of why. Notice the placement of the parenthesis? The first line compiles just fine, but it includes the UriKind as a parameter to String.Format and not to Uri(). Then at runtime the formatting just ignores it as “unused 3rd parameter” but when I try to navigate to this Uri in my phone app, the navigation service chokes because it requires a relative Uri. The error then says something like “can’t find view /View/DataView.xaml”. You look in that folder –yep the XAML is there. You look at the code – yep it seems to check out ok. This can turn into a real head scratcher.

So anytime you call an API that has variable number of parameters like String.Format() its not a bad idea to do it on its own line like this

 string tmp = string.Format("/View/DataView.xaml?collection={0}&id={1}", collection, currentItem.getID());

Uri uri = new Uri(tmp);