4

Say I have the following code:

Request.QueryString["ids"].Split('|');

If ids is not present in the query string this will throw an exception. Is there a generally accepted way to handle this type of situation. I think all of the following options would keep this from thorwing an error, but I'm wondering if one (or some different method entirely) is generally accepted as better.

string[] ids = (Request.QueryString["ids"] ?? "").Split('|'); 

or

string[] ids;
if(!String.IsNullOrEmpty(Request.QueryString["ids"]))
{
   ids = Request.QueryString["ids"].Split('|')
}

or

?

I think all of these will work, but they look sort of ugly. Is there a better* way?

*better = easier to read, faster, more efficient or all of the above.

Abe Miessler
  • 75,910
  • 89
  • 276
  • 451
  • Easier to read, Faster, More Efficient. Pick One :) – RonaldBarzell Dec 21 '12 at 00:34
  • Your second option won't work, `null as string` is still null. – Lee Dec 21 '12 at 00:34
  • 2
    The `If(!String.IsNullOrEmpty` one is the easiest to read - IMHO. Regarding speed is there a difference in the IL produced? – Jeremy Thompson Dec 21 '12 at 00:35
  • The soft cast (qs as string) will not work. It will still return null. The null-coalesce operator (??) appears to be the 'cleanest' solution IMHO. – Jason Whitted Dec 21 '12 at 00:35
  • @Lee, you were right. Removed `null as string` option. – Abe Miessler Dec 21 '12 at 00:37
  • It's generally best to validate all of your input (check that `Request.QueryString["ids"]` is non-null, throw appropriate error messages) prior to processing it (doing the `Split()`), imho, rather than combining them like that. – Carson63000 Dec 21 '12 at 00:37
  • 1
    Also, your first and third options are different since `ids` will be null in the third if the check fails, whereas it will be empty in the first. – Lee Dec 21 '12 at 00:37
  • @Lee, actually I think in the first one it would have one element, correct? – Abe Miessler Dec 21 '12 at 00:39
  • @AbeMiessler - Yes, you're right it would contain the empty string. – Lee Dec 21 '12 at 00:41
  • @Lee, still a good point. Not the best example maybe, but I hope it gets the idea of what I'm asking across. – Abe Miessler Dec 21 '12 at 00:42
  • Better = stating your intent clearly. If an empty string/null has no meaning, don't propogate that assumption further into your code. Test early and don't create meaningless intermediate results (such as those caused by splitting an empty string). – spender Dec 21 '12 at 01:11
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 21 '12 at 01:15

3 Answers3

12

I like using an extension method for this:

public static string EmptyIfNull(this string self)
{
    return self ?? "";
}

Usage:

string[] ids = Request.QueryString["ids"].EmptyIfNull().Split('|');
McGarnagle
  • 96,448
  • 30
  • 213
  • 255
  • 1
    I don't like this. Null isn't an empty string and leads to a spurious entry in the split array. The better approach is to handle the null there and then and not to leave any assumptions to be dealt with downstream. – spender Dec 21 '12 at 01:04
  • 2
    `Split()` returns a `string[]`. `ToArray` isn't necessary. – spender Dec 21 '12 at 01:05
1

Personally I'd use

string idStr = Request.QueryString["ids"];
ids = idStr == null ? new string[0] : idStr.Split("|");
Lee
  • 133,981
  • 18
  • 209
  • 268
  • Good. No spurious entry caused by empty strings. +1. If it were me, I'd probably test for `string.IsNullOrEmpty`, but it's not clear whether an empty string represents a useful value to the OP. – spender Dec 21 '12 at 01:06
0
string[] ids = (Request.QueryString["ids"] as string).Split('|');

This will fail in the same manner as Request.QueryString["ids"]

string[] ids;
if(!String.IsNullOrEmpty(Request.QueryString["ids"]))
{
   ids = Request.QueryString["ids"].Split('|')
}

Heavier and may call the data retrieval logic twice (and you might have side-effects done twice by error) => use a temporary to store the data but heavier.

string[] ids = (Request.QueryString["ids"] ?? "").Split('|'); 

Definetely the easiest, cleanest and more efficient way as the compiler will generate a temporary itself.

If you encounter this kind of processing a lot of time you can build your own plumbing library with a bunch of methods with fluent names and behaviors.

Pragmateek
  • 12,648
  • 9
  • 66
  • 105