29

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()).

Eugene
  • 55,777
  • 85
  • 212
  • 324
  • 3
    "`if (...)` is better than `if (!...)`" - Wait, wat? –  Jan 23 '11 at 18:00
  • Question has been slightly extended to make it more clear. – Eugene Jan 23 '11 at 18:06
  • For me isn't the fact of one be better than another, simply you must use which be appropriate in every situation, the conditional world is very wide and diverse. – Luis Carlos Jun 13 '17 at 06:40

7 Answers7

50

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.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
  • 1
    I (nearly) always structure if elses so that the most common branch of execution comes first. – Dunes Jan 24 '11 at 08:14
  • 1
    There is the PMD rule ConfusingTernary to check this: http://pmd.sourceforge.net/pmd-5.0.3/rules/java/design.html#ConfusingTernary – Marco Eckstein Apr 25 '13 at 14:36
19

As a general statement, its good to make your if conditionals as readable as possible. For your example, using ! is ok. the problem is when things look like

if ((a.b && c.d.e) || !f)

you might want to do something like

bool isOk = a.b;
bool isStillOk = c.d.e
bool alternateOk = !f

then your if statement is simplified to

if ( (isOk && isStillOk) || alternateOk)

It just makes the code more readable. And if you have to debug, you can debug the isOk set of vars instead of having to dig through the variables in scope. It is also helpful for dealing with NPEs -- breaking code out into simpler chunks is always good.

Donnelle
  • 5,508
  • 3
  • 23
  • 31
hvgotcodes
  • 109,621
  • 25
  • 195
  • 231
12

No, there is absolutely nothing wrong with using the ! operator in if..then..else statements.

The naming of variables, and in your example, methods is what is important. If you are using:

if(!isPerson()) { ... } // Nothing wrong with this

However:

if(!balloons()) { ... } // method is named badly

It all comes down to readability. Always aim for what is the most readable and you won't go wrong. Always try to keep your code continuous as well, for instance, look at Bill the Lizards answer.

Community
  • 1
  • 1
Kyle Rosendo
  • 23,930
  • 7
  • 75
  • 114
4

In general, ! is a perfectly good and readable boolean logic operator. No reason not to use it unless you're simplifying by removing double negatives or applying Morgan's law.

!(!A) = A

or

!(!A | !B) = A & B

As a rule of thumb, keep the signature of your boolean return methods mnemonic and in line with convention. The problem with the scenario that @hvgotcodes proposes is that of course a.b and c.d.e are not very friendly examples to begin with. Suppose you have a Flight and a Seat class for a flight booking application. Then the condition for booking a flight could perfectly be something like

if(flight.isActive() && !seat.isTaken())
{
    //book the seat
}

This perfectly readable and understandable code. You could re-define your boolean logic for the Seat class and rephrase the condition to this, though.

if(flight.isActive() && seat.isVacant())
{
    //book the seat
}

Thus removing the ! operator if it really bothers you, but you'll see that it all depends on what your boolean methods mean.

Mig82
  • 3,665
  • 1
  • 30
  • 55
1

try like this

if (!(a | b)) {
    //blahblah
}

It's same with

if (a | b) {}
else {
    // blahblah
}
Jae Lee
  • 61
  • 6
0

I never heard of this one before.

How is

if (doSomething()) {
} else {
   // blah
}

better than

if (!doSomething()) {
   // blah
}

The later is more clear and concise.

Besides the ! operator can appear in complex conditions such as (!a || b). How do you avoid it then?

Use the ! operator when you need.

Alexandru
  • 22,519
  • 17
  • 61
  • 78
0

It is generally not a bad idea to avoid the !-operator if you have the choice. One simple reason is that it can be a source of errors, because it is possible to overlook it. More readable can be: if(conditionA==false) in some cases. This mainly plays a role if you skip the else part. If you have an else-block anyway you should not use the negation in the if-condition.

Except for composed-conditions like this:

if(!isA() && isB() && !isNotC())

Here you have to use some sort of negation to get the desired logic. In this case, what really is worth thinking about is the naming of the functions or variables. Try to name them so you can often use them in simple conditions without negation.

In this case you should think about the logic of isNotC() and if it could be replaced by a method isC() if it makes sense.

Finally your example has another problem when it comes to readability which is even more serious than the question whether to use negation or not: Does the reader of the code really knows when doSomething() returns true and when false? If it was false was it done anyway? This is a very common problem, which ends in the reader trying to find out what the return values of functions really mean.

FabianB
  • 273
  • 1
  • 6