14

String.IsNullOrEmpty() appears to be an extremely well used method and I find myself wishing there were some shorthand for it. Something like ??? as it would be used in a similar context to the null coalesce operator but be extended to test for empty as well as null strings. I.e.

 string text = something.SomeText ??? "Not provided";

What would be your opinions on this? Would it unnecessarily bloat the language? Would it open the floodgates for other mid-level operations to be granted such deep integration with the compiler? Or would it be a useful addition for the language.

20
  • 7
    Not opposed the idea, but dread the thought of ?, ??, ???, ????, ?????, ??????, ??????? etc Commented Aug 4, 2010 at 14:48
  • 3
    what does it achieve? the purpose of a language is not to combine every statement onto a single line....or is it? Imagine single line programs.... Commented Aug 4, 2010 at 14:50
  • The C# language has plenty of special String knowledge (look at how "string" + "concatenation" + "works") Commented Aug 4, 2010 at 14:52
  • 1
    @ DrJokepu: confusing is relative. Commented Aug 4, 2010 at 14:57
  • 1
    Intellisense reduces the keystrokes for me for this method to just a couple. Why not rely on that and let the code remain readable? Commented Aug 4, 2010 at 18:47

2 Answers 2

24

Phil Haack blogged about this a while ago. Basically, he suggests an extension method on string that lets you do

var text = someString.AsNullIfEmpty() ?? "Not provided.";

The extension method is very simple:

public static string AsNullIfEmpty(this string str)
{
    return !string.IsNullOrEmpty(str) ? str : null;
}

He also suggests a version checking for whitespace instead of just empty with the string.IsNullOrWhitespace() method from .NET 4, as well as similar extensions for the IEnumerable<T> interface.

He also talks about introducing new operators such as ??? for this, but concludes that it would be more confusing than helpful - after all, introducing these extension methods you can do the same thing, but it's more readable, and it's using shorthand syntax that "everybody already knows".

Sign up to request clarification or add additional context in comments.

9 Comments

The problem is, as developers, we get hung up on these geeky pursuits. Ask yourself, "will this help the users of this software", "will this make a better bottom line for the company that uses this software"???
if you want to introduce new extension method, maybe this version will express intensions more clear static class StringExtensions { public static string ValueOrDefault(this string value, string defaultValue) { return string.IsNullOrEmpty(value) ? defaultValue : value; } }
@Mitch: The main reason to introduce these extension methods is not (only) to make this code look nice - it is also to reduce code repetition. How many times have you found yourself wanting to check a string for null, empty or whitespace, and provide a default value if needed? I do that all the time. And with these extension methods, I save time when writing software, which benefits (at least) the company I write software for.
@desco: Now that I think about it, using the ternary operator does make sense. (I updated my post to reflect this.) However, your extension method will never return null - there might be other cases than providing default values for returning null on empty or whitespace, in which case this extension already has it covered. That said, yours is a good complement to this if you think it makes your code more readable and maintainable. You could in fact even use "my" extension in the implementation of yours =)
@Tomas Lycken: Source extension method is intended to be used in pair with ?? operator and together this couple expresses the same thing as my one method: check string and return either itself or some default value.
|
6

Phil Haack talks about this exact operation on his blog. You should check out his article Null Or Empty Coalescing.

Special purpose operators like ??? are a slippery slope. Why stop there - why not introduce a "null or empty or whitespace" operator: ????. Phil talks about this, actually, in his article. His overall conclusion is that such operators would be more confusing than helpfull.

Ultimately you could take many different operations an invent operators to represent them - unfortunately, that would likely destroy the readability of the language.

2 Comments

Sigh... just because I wanted to type out a couple of examples, you beat me by less than a minute...! ;)
@Tomas: It hasn't seemed to hurt your score on this question. Nice job.

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.