string.IsNullOrEmpty() vs string.NotNullOrEmpty()

前端 未结 14 973
遇见更好的自我
遇见更好的自我 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: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;
    }
    

提交回复
热议问题