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.
精彩评论