开发者

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

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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)开发者_Go百科

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)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜