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?
It's generally regarded as easier to understand.
if (!ICanDoThis)
{
    // don't do it
}
else
{
    // do it
}
vs.
if (ICanDoThis)
{
    // do it
}
else
{
    // don't do it
}    
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:
if (!myParametersAreValid)
{
    // fail out
}
DoWorkWith(myParameters)
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:
if (ICanDoThis == false)
{
  // I can't do this
}
else
{
  // I can do this
}
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
if (true) {
} else {
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论