Any references / MSDN tutorials to show support for either “if (!condition)” or “if (condition == false)”? [closed]

不想你离开。 提交于 2019-12-23 17:43:04

问题


For those who misread the question: let's pretend we're on Wikipedia.
I'm not looking for “the right way”, I'm looking for verifiable references to support either side.

(Please read to the end)

The Context

In a comment on a different thread, a person said his friends would beg to differ with my opinion on whether

if (!condition)

or

if (condition == false)

is preferred in C#.

As much as I was sure I know how to do the Right Thing™, I was unable to find any evidence my opinion is official in neither C# coding nor design guidelines.

The Question

Is there anything substantial to show in support of either side, apart from the common sense?

(a paragraph in a widely admired book or any document hosted at microsoft.com that uses or prescribes either style over another will answer the question)


回答1:


if (!condition) and if (condition) are preferred over if (condition == false) and if (condition == true) because the former is just as readable and less verbose.

Bottom section of page 13 of C# Coding Standards for .NET created by Lance Hunt says:

Avoid evaluating Boolean conditions against true or false.

// Bad! 
if (isValid == true) 
{ ... } 
// Good! 
if (isValid) 
{ ... }

However it is not by any means official and it doesn't come from Microsoft.




回答2:


Sometimes I use if (condition == false) if the condition being false is an extreme edge case - it brings attention to that fact for me.



来源:https://stackoverflow.com/questions/6336735/any-references-msdn-tutorials-to-show-support-for-either-if-condition-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!