Using NOT operator in IF conditions

Eugene picture Eugene · Jan 23, 2011 · Viewed 256.3k times · Source

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

Answer

Bill the Lizard picture Bill the Lizard · Jan 23, 2011

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.