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

后端 未结 30 2392
礼貌的吻别
礼貌的吻别 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条回答
  •  無奈伤痛
    2020-12-02 20:30

    I always chuckle (or throw something at someone, depending on my mood) when I come across

    if (someBoolean == true) { /* ... */ }
    

    because surely if you can't rely on the fact that your comparison returns a boolean, then you can't rely on comparing the result to true either, so the code should become

    if ((someBoolean == true) == true) { /* ... */ }
    

    but, of course, this should really be

    if (((someBoolean == true) == true) == true) { /* ... */ }
    

    but, of course ...

    (ah, compilation failed. Back to work.)

提交回复
热议问题