Should I use `!IsGood` or `IsGood == false`?

后端 未结 30 2393
礼貌的吻别
礼貌的吻别 2020-12-02 19:49

I keep seeing code that does checks like this

if (IsGood == false)
{
   DoSomething();
}

or this

if (IsGood == true)
{
   D         


        
30条回答
  •  -上瘾入骨i
    2020-12-02 20:54

    For some reason I've always liked

    if (IsGood)
    

    more than

    if (!IsBad)
    

    and that's why I kind of like Ruby's unless (but it's a little too easy to abuse):

    unless (IsBad)
    

    and even more if used like this:

    raise InvalidColor unless AllowedColors.include?(color)
    

提交回复
热议问题