为什么要在 if 块中使用正逻辑?
我不明白为什么在 if 块中使用正逻辑是最佳实践
http://msdn.microsoft.com/en-US/library/aa629483.aspx
首选:
if (true)
{
...
}
else
{
...
}
为什么在 if 块中使用正逻辑是最佳实践?
I don't understand why it is a best practice to use positive logic in an if block
http://msdn.microsoft.com/en-US/library/aa629483.aspx
Preferred:
if (true)
{
...
}
else
{
...
}
Why it is a best practice to have positive logic in an if block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常认为更容易理解。
今天你
的逻辑可能非常清晰,但想想几年后遇到这个问题的开发人员。
但与其他一切一样,这只是一个指导方针。具体来说,我使用诸如“负逻辑”之类的错误检查:
我避免了“正逻辑”本来需要的一系列条件。
It's generally regarded as easier to understand.
vs.
Your logic may be crystal-clear to you today, but think of the developer who happens across it a couple of years from now.
But like everything else, this is only a guideline. Specifically, I use something like "negative logic" with error checking:
I avoid a cascade of conditionals that "positive logic" would otherwise require.
我有时会做这样的半正逻辑。我认为这在技术上是正逻辑,因为它不使用“非”运算符。有时我发现很难总是使用正逻辑,因为它最终会使我的代码变得不干净:
I sometimes do a semi-positive logic like this. I think it's technically positive logic because it doesn't use "not" operators. I sometimes find it hard to always use positive logic because it ends up making my code un-clean:
我总是看到人们首先测试最可能的结果。例如,模式对话框通常有一个默认按钮(突出显示)您看到这个是因为它最有可能或最常见。因此,如果您期望 true 您会看到
I have always seen people test the most probable outcome first. For example modal dialogs normally have a default button(highlighted) You see this because it is most probable or most common that this will be used.So if you are expecting true you would see