我应该使用 `!IsGood` 还是 `IsGood == false`? [英] Should I use `!IsGood` or `IsGood == false`?

查看:8
本文介绍了我应该使用 `!IsGood` 还是 `IsGood == false`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断看到这样的检查代码

I keep seeing code that does checks like this

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

或者这个

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

我讨厌这种语法,总是使用以下语法.

I hate this syntax, and always use the following syntax.

if (IsGood)
{
   DoSomething();
}

if (!IsGood)
{
   DoSomething();
}

是否有任何理由使用 '== true' 或 '== false'?

Is there any reason to use '== true' or '== false'?

这是一个可读性的东西吗?人们只是不理解布尔变量吗?

Is it a readability thing? Do people just not understand Boolean variables?

另外,两者在性能上有区别吗?

Also, is there any performance difference between the two?

推荐答案

我的语法和你一样,不那么冗长.

I follow the same syntax as you, it's less verbose.

人们(更多是初学者)更喜欢使用 == true 只是为了确保这是他们想要的.他们习惯于在条件中使用运算符……他们发现它更具可读性.但是一旦你变得更高级,你会发现它很烦人,因为它太冗长了.

People (more beginner) prefer to use == true just to be sure that it's what they want. They are used to use operator in their conditional... they found it more readable. But once you got more advanced, you found it irritating because it's too verbose.

这篇关于我应该使用 `!IsGood` 还是 `IsGood == false`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆