2

I am developing in Java and I am using IntelliJ as my IDE. I wrote an if statement as follows.

if( list1.size() >= 1 || list2.contains(itemX) ) {
    //do something
}

IntelliJ suggested a transformation (DeMorgan's Law) and it transformed it to:

if( ! ( list1.size() < 1 && !( list2.contains(itemX) ) ) ) {
    //do something
}

So it applied a very common discrete mathematics theory on simplifying boolean expressions. What I am wondering is how does this optimize anything?

|| operator anyways does not execute the whole condition if the first part is itself true, and only executes the RHS only if the first part is false.

Is the transformed condition effective? How?

subject-q
  • 91
  • 3
  • 19
  • 1
    Even `AND` operator also, if first condition is false it will not evaluate second – Ryuzaki L Aug 02 '19 at 06:06
  • Aware of it, Wondering what might be the difference between both ways of writing condition – subject-q Aug 02 '19 at 06:07
  • 4
    You're worrying about the wrong thing. Use what is the most readable and matches with your intention. Both are equivalent in terms of performance, but the first one is clearly more readable. – JB Nizet Aug 02 '19 at 06:11
  • Please post as answer and I will mark it. JB Nizet or user207421. – subject-q Aug 02 '19 at 06:19
  • How exactly does IntelliJ suggest that? It is not a warning, is it? If it is only an intention, then it is only up to you what you use. – Meo Aug 02 '19 at 06:22
  • 1
    It is an `intention`, but documentation https://www.jetbrains.com/help/idea/intention-actions.html said "..searches for ways to optimize it..". Hence I wondered how in my case one could be more optimal than other? – subject-q Aug 02 '19 at 06:28
  • As a matter of fact the second expression in your title would be better simplified via de Morgan's laws to the first, rather than the other way around, but the IDE is suggesting more than that, as it is also reversing conditions to avoid introducing all three negations. – user207421 Aug 02 '19 at 06:40
  • "..searches for ways to optimize it..", but in what sense? Readability? Many of the intentions only refactor the code. – Meo Aug 02 '19 at 07:02

2 Answers2

5

This is somewhat subjective, but a good general rule of thumb is to remove as much complexity as possible. By complexity, I mean the number of operations you need to perform in order to obtain the desired result.

In this sense, !a && !b is worse than !(a || b) because in one case you're negating a and b, then performing the OR and operator resulting in 3 operations whereas in the latter case, you're only performing 2. Of course this is vacuous when you're talking about two conditions, but when you're dealing with many, this can make a big difference.

But in your scenario it doesn't make any sense for your IDE to change it as the later has lower number of operations. Probably its the IDE trying to desperately woo you :)

Hope this makes sense !!

vizsatiz
  • 1,933
  • 1
  • 17
  • 36
4

Both are exactly the same statements.

I agree that OR operator does not evaluate the second part if the first part is TRUE, however, it is also true that the AND operator does not evaluate the second part if the first part is FALSE.

In fact, it will take more time and space to evaluate the ~(~A && ~B) as opposed to A||B.

Hope this helps :)

Twini
  • 195
  • 16