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.

12
  • Easier to read, Faster, More Efficient. Pick One :) Commented Dec 21, 2012 at 0:34
  • Your second option won't work, null as string is still null. Commented Dec 21, 2012 at 0:34
  • 2
    The If(!String.IsNullOrEmpty one is the easiest to read - IMHO. Regarding speed is there a difference in the IL produced? Commented Dec 21, 2012 at 0: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. Commented Dec 21, 2012 at 0:35
  • 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. Commented Dec 21, 2012 at 0:37

3 Answers 3

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('|');
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Split() returns a string[]. ToArray isn't necessary.
1

Personally I'd use

string idStr = Request.QueryString["ids"];
ids = idStr == null ? new string[0] : idStr.Split("|");

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.