1

Which is better in terms of best practice / efficiency?

if (x == 1
    && y == 1
    && z == 1)
{ do things }

or

if (x != 1 ||
    y != 1 ||
    z != 1)
{ don't do things and go to a different bit of logic.} 

Is there any difference in efficiency when short circuiting ANDs and ORs? Is it (generally) better to check positively or negatively when multiple logical assertions need to be made?

  • 4
    You are not testing for the same condition. – Mena Dec 02 '14 at 09:00
  • 3
    it is best practice to test `positive`. e.g. `x==1` is better than `x!=1` and `something.isEmpty()` is better than `!something.isEmpty()`. But that's only because it is easier to understand when reading. – GameDroids Dec 02 '14 at 09:02
  • You are absolutely right, I'm not. I'll rephrase. – Daniel Lyons Dec 02 '14 at 09:02
  • 2
    @mena: he is, there's an else in the second one. – Oliver Charlesworth Dec 02 '14 at 09:03
  • You need to choose which ever does explicitly that which you intend and more importantly, which is most blatantly obvious so that a few months down the line when you have to revisit the code you instantly understand what you were up to at the time. – Matthew V Carey Dec 02 '14 at 09:04

4 Answers4

2

For pure optimization of the code it depends case-by-case. The scenario that will on average do the least amount of comparisons.

For code design it is also case-by-case. The if-cases should match what you are actually looking for. A function that tests if a string is inputted correctly for example. (the tests are made up)

public boolean isValidString (string s) {

    if (s.isEmpty())
        return false;

    if (s.length() < 12)
        return false;

    if (s...)
        return false

    return true;
}

In this case the most logical approach is the ||. It could be written.

public boolean isValidString (string s) {

    if (s.isEmpty() || s.length() < 12 || s...)
        return false;

    return true;
}

With http://en.wikipedia.org/wiki/De_Morgan%27s_laws this could be rewritten to not and. However it is not what we want to test, even though they yield the same result.

So stick to the logical approach in general cases.

Emz
  • 1,280
  • 1
  • 14
  • 29
1

If you think about efficiency then think about how often each case will occur. The most likely one should be put in front so the whole expression is shortcircuited immediately.

Florian Blum
  • 648
  • 6
  • 16
0

Better you use "==" instead of going for "!=".

This is also recommended with PMD.

The following is good and improves redability.

    If(true){
//
    }else{
//
    }

than

   If(!true){
// 
    }else{
//
    }
janasainik
  • 811
  • 5
  • 20
  • 40
  • I'd say it depends. In some cases you want to test if it is separated from a value. Then using the `!=` operator is the right way. – Emz Dec 02 '14 at 09:18
0

Well, in some JVM implementations boolean values are stored as integers in the JVM. int value 1 meaning true and int value 0 meaning false. Also, comparison logic at processor level is architecture dependent. Some machines might subtract 2 operands, then add and then compare, others might compare byte by byte etc.. So, unless you are looking at a specific hardware architecture (which you shouldn't.. atleast for java programming language), I don't think this matters much..

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • That is incorrect. http://stackoverflow.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java It should be noted that it can cary from JVM to JVM. – Emz Dec 02 '14 at 09:19
  • @Emz - You meant about the representation of boolean values? – TheLostMind Dec 02 '14 at 09:23
  • @Emz - from [oracle jvm spec](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.3.4). *Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.* – TheLostMind Dec 02 '14 at 09:25
  • In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element. Is an example. – Emz Dec 02 '14 at 09:29
  • A compiler can in some cases optimize booleans further than ints as well. – Emz Dec 02 '14 at 09:30
  • @Emz - Yes.. That's why I've used the word *usually*. :)..There can be several other ways of representing boolean values.. in fact, some JVM implementations might actually support them but the JVM specification doesn't make it mandatory. – TheLostMind Dec 02 '14 at 09:31
  • I should not have been that strict by saying it is incorrect. Instead it should change something like "Fur a further read on how booleans are stored go here..". – Emz Dec 02 '14 at 09:34