string.IsNullOrEmpty() vs string.NotNullOrEmpty()

前端 未结 14 919
遇见更好的自我
遇见更好的自我 2021-02-01 02:56

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

e.g.

if (!string.IsNullOrEmpty())
相关标签:
14条回答
  • 2021-02-01 03:43

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

    0 讨论(0)
  • 2021-02-01 03:43

    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);
            }
    
    0 讨论(0)
  • 2021-02-01 03:46

    "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;
    }
    
    0 讨论(0)
  • 2021-02-01 03:48

    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
    0 讨论(0)
  • 2021-02-01 03:51

    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.

    0 讨论(0)
  • 2021-02-01 03:53

    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.

    0 讨论(0)
提交回复
热议问题