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

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

    According to Code Complete a book Jeff got his name from and holds in high regards the following is the way you should treat booleans.

    if (IsGood)
    if (!IsGood)
    

    I use to go with actually comparing the booleans, but I figured why add an extra step to the process and treat booleans as second rate types. In my view a comparison returns a boolean and a boolean type is already a boolean so why no just use the boolean.

    Really what the debate comes down to is using good names for your booleans. Like you did above I always phrase my boolean objects in the for of a question. Such as

    • IsGood
    • HasValue
    • etc.

提交回复
热议问题