34

I'm curious if any developers use string.IsNullOrEmpty() more often with a negative than with a positive

e.g.

if (!string.IsNullOrEmpty())

This is how I use the method 99% of the time. What was the design decision for this?

6
  • 14
    I think methods with boolean result should be "positive" in the sense that they should use "Is..." instead of "Not..." ... just an idea why it's that way. Commented Apr 9, 2009 at 14:01
  • @Johannes yeah...definitely positive! Commented Apr 9, 2009 at 14:02
  • Why does MS find it necessary to add in extra bloat? Ugh.. Commented Apr 9, 2009 at 14:45
  • 10
    how is it bloat? if anything, your redundant use of "extra" for the word "bloat" sounds like bloat to me... Commented Apr 9, 2009 at 17:32
  • 4
    Review 99% of IsNullOrEmpty() calls in your code and try to apply 'Replace Nested Conditional with Guard Clauses' refactoring. Commented May 6, 2010 at 9:29

14 Answers 14

64

Because "IsNullOrEmpty" is easier to understand than "NotNullOrEmpty". The latter could be interpreted as:

  1. It's not null and it's not empty
  2. It's not null or it is empty
Sign up to request clarification or add additional context in comments.

Comments

38

Double negatives are usually discouraged in naming stuff. !string.NotNullOrEmpty(...) would make one.

3 Comments

We actually had a guy name a variable "isNotGuam" in our code base. Its always real fun trying to decipher what is meant by (in Delphi) if not isNotGuam then
if (!string.NotNullOrEmpty(..)) { } else { //goooooo! }
You don't have to make the verb negative though, that was a bad example
12

For those logicians out there, !string.IsNullOrEmpty is not equivalent to string.IsNotNullOrEmpty. @Guffa has it correct. Using DeMorgan's law, it would have to be string.IsNotNullAndNotEmpty to be equivalent.

¬(null ∨ empty) ⇔ ¬null ∧ ¬empty

¬(null ∨ empty) ≠ ¬null ∨ empty

The point here, I guess, is that the way it is currently is unambiguous, where as making the opposite unambiguous would be cumbersome.

Comments

8

I always create an extension method for "HasContent()" which generally makes sense, follows the "positive" specifications, and saves on code bloat because I use it much more often than its counterpart:

public static bool HasContent(this string s) {
    return !string.IsNullOrEmpty(s);
}

2 Comments

Thank you. I was just thinking about the best name for this. I came up with "HasValue" and "IsPopulated," but HasContent is probably better than both.
I also add an IsEmpty() extension method for brevity and readability. I know people might complain about the word Null not being used, but I'm happy with this exception for the string class.
7

C# naming conventions dictate that your expressions should be in the positive such as "Is..." and not "IsNot..."

EDIT: Typically, I use it when doing error checking and input validation at the beginning of a method and raise an exception if the parameter is null or empty.

if (string.IsNullOrEmpty(myParameter))
{
throw new ....
}

Comments

7

I prefer the extension method:

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }
}

I find it reads better to say:

if(myValue.IsNullOrEmpty())

or

if(!myValue.IsNullOrEmpty())

8 Comments

@Dan: You can actually, stackoverflow.com/questions/647643/…. +1.
@Dan: I suppose that is just philosophy... I have no problem with extension methods that work on null as long as it is clear in the method name: value.IsNull(), value.ThrowIfNull() or value.IsNullOrEmpty().
But what do you think about extension methods that are specifically named Null ?
I'm sure that Uncle Bob would probably disapprove but I don't think that the sky will fall on our heads if we do foo.IsNull() - I would say it is pretty obvious what you are trying to achieve
@Brian: I can see your point. Personally I don't think I'll ever be comfortable with calling methods on an uninstatiated variable because I think it sets an unsafe precedent. On an aside I'm quite disappointed that we can't create static extension methods - I'd like to be able to write AType.IsNullOrEmpty(aVar); on more types.
|
4

Perhaps because then the name would have to be the lengthy IsNotNullAndNotEmpty to be as specific.

Comments

2

Of course you could always use string.IsNullOrWhiteSpace(string) now instead of string .IsNullOrEmpty(string) from .NET 4.0

Comments

1

That is the most common usage I have seen.

Comments

1

"NotNullOrEmpty" is ambiguous, it could mean "(not null) or empty" or it could mean "not (null or empty)". To make it unambiguous you'd have to use "NotNullAndNotEmpty", which is a mouthfull.

Also, the "IsNullOrEmpty" naming encourages use as a guard clause, which I think is useful. E.g.:

if (String.IsNullOrEmpty(someString))
{
   // error handling
   return;
}
// do stuff

which I think is generally cleaner than:

if (!String.IsNullOrEmpty(someString))
{
   // do stuff
}
else
{
   // error handling
   return;
}

Comments

1

I would actually be inclined to offer a different answer from the "it's ambiguous" explanation provided by several others (though I agree with that answer as well):

Personally, I like to minimize nesting in my code, as (to me) the more curly braces code has, the harder it becomes to follow.

Therefore I'd much prefer this (for example):

public bool DoSomethingWithString(string s) {
    if (string.IsNullOrEmpty(s))
        return false;

    // here's the important code, not nested
}

to this:

public bool DoSomethingWithString(string s) {
    if (!string.IsNullOrEmpty(s)) {
        // here's the important code, nested
    } else {
        return false;
    }
}

This is a pretty specific scenario (where a null/empty string prompts an immediate exit) and clearly isn't the way a method using IsNullOrEmpty would always be structured; but I think it's actually pretty common.

Comments

0

Personally I prefer to cater for the non negated scenario first. It just makes sense to me to do the true part first and then the false. Comes down to personal style.

Comments

0

I've always thought it seemed the wrong way round as I use the negative much more often than the positive.

I would also like there to be an instance IsEmpty() or IsNotEmpty() for use when the variable is declared within the function. This could not be IsNullOrEmpty() or IsNotNullOrEmpty() as if the instance was null then you would get a null reference exception.

Comments

0

I had the same question before I realized all I had to do to flip the question was to put the Not operator in front of the conditional. I think it cleande up my code some.

 // need to check if tBx_PTNum.Text is empty
        /*
        if (string.IsNullOrWhiteSpace(tBx_PTNum.Text))
        {
            // no pt number yet
        }
        else
        {
            ptNum = Convert.ToInt32(tBx_PTNum.Text);
        }
        */
        
        if(!string.IsNullOrEmpty(tBx_PTNum.Text))
        {
            ptNum = Convert.ToInt32(tBx_PTNum.Text);
        }

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.