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

后端 未结 30 2454
礼貌的吻别
礼貌的吻别 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:36

    Ah, I have some co-worked favoring the longer form, arguing it is more readable than the tiny !

    I started to "fix" that, since booleans are self sufficient, then I dropped the crusade... ^_^ They don't like clean up of code here, anyway, arguing it makes integration between branches difficult (that's true, but then you live forever with bad looking code...).

    If you write correctly your boolean variable name, it should read naturally:
    if (isSuccessful) vs. if (returnCode)

    I might indulge in boolean comparison in some cases, like:
    if (PropertyProvider.getBooleanProperty(SOME_SETTING, true) == true) because it reads less "naturally".

提交回复
热议问题