Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NOT operator in IF conditions

Is it really a good practice to avoid using NOT operator in IF conditions in order to make your code better readable? I heard the if (doSomething()) is better then if (!doSomething()).

like image 325
Eugene Avatar asked Jan 23 '11 17:01

Eugene


People also ask

How do you use not in if condition?

OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Which operator is not used with if condition?

The not operator is a logical operator, represented in Java by the ! symbol. It's a unary operator that takes a boolean value as its operand. The not operator works by inverting (or negating) the value of its operand.

Can we use operator in if condition?

The use of the AND Boolean operator takes precedence over the OR Boolean operator as shown in these examples. The number of conditional expressions you can specify on the IF statement is limited to 255. AND processing returns a TRUE result for the IF statement only if all the conditional expressions evaluate as TRUE.

How do you use not in an if statement in Python?

The 'not' is a Logical operator in Python that will return True if the expression is False. The 'not' operator is used in the if statements. If x is True, then not will evaluate as false, otherwise, True.


1 Answers

It really depends on what you're trying to accomplish. If you have no else clause then if(!doSomething()) seems fine. However, if you have

if(!doSomething()) {     ... } else {     // do something else } 

I'd probably reverse that logic to remove the ! operator and make the if clause slightly more clear.

like image 104
Bill the Lizard Avatar answered Oct 03 '22 21:10

Bill the Lizard