# 在條件陳述中檢查“真實性”或“平等性”的實踐 - 它真的有意義嗎？

if ( num % 2 != 0 )
{
//Number is odd
}


if ( num % 2 == 1 )
{
//Number is odd
}


When I was beginning to code, I knew that num % 2 == 0 implies the number is even, so I just put a ! there to check if it is odd. But he was like 'Don't check NOT conditions. Have the practice of checking the 'trueness' 要麽 'equality' of conditions whenever possible.' And he recommended that I use the second piece of code.

I am not f要麽 要麽 against either but I just wanted to know - what difference does it make? Please don't reply 'Technically the output will be the same' - we ALL know that. Is it a general programming practice 要麽 is it his own programming practice that he is preaching to others?

NOTE: I used C#/C++ style syntax f要麽 no reason. My question is equally applicable when using the IsNot, <> operat要麽s in VB etc. So readability of the '!' operat要麽 is just one of the issues. Not THE issue.

## 最佳答案

if (authorityLvl!=Admin){
doA();
}else{
doB();
}


if (!(authorityLvl!=Admin && authorityLvl!=Manager)){
doB();
}else{
doA();
}


（x％2！= 0）肯定不是問題，但也許避免上述情況的最佳方法是教導學生不要使用負面條件作為一般規則，希望他們之前會使用某些判斷他們這樣做 - 因為只是說它可能成為一個維護問題可能不是足夠的動力。

userHasAuthority = (authorityLvl==Admin);
if (userHasAuthority){
doB();
else{
doA();
}