Search code examples
javaif-statementboolean-operations

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


Solution

  • 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.