2

When we're using an if-conditional, we specify the condition in a boolean expression such as following :

if(boolean expression). 

If I have two variables in a boolean expression, such as (bagWeight > WEIGHT_LIMIT), does the order of the two variables in which they appear matter? In other words, can I swap those two variables' places such as following? (WEIGHT_LIMIT < bagWeight). Notice it would still be bag weight is less than weight limit, but I just switch the order of which one appears first in the boolean expression. AND Does it depend on which one becomes a subject, like one that gets focused on and evaluated? (In this case, we're trying to figure out if the bag weight is heavier than the limit or not. So the bag weight gets evaluated according to something.. I would call it a subject.)

Eclipse doesn't scream at me that it's wrong, and intuitively it makes sense, but somehow it just bothers me whether there's a more common programming practice or not. So my questions were, can I swap the two variables' places and would not matter? and does it depend on the context of which being a subject? and which is a more common programming practice?

Chris Jefferson
  • 7,225
  • 11
  • 43
  • 66
C.Jinny
  • 31
  • 1
  • 5
  • 2
    *In other words, can I swap those two variables' places such as following? (WEIGHT_LIMIT < bagWeight).* Yes. `if (a < b)` is equivalent to `if (b > a)` – Elliott Frisch Sep 27 '16 at 20:16
  • In a comparison the order of the variables doesn't matter. I prefer to write a comparison in "natural order": In spoken language you usually say "if the bags weight is less than the weight limit" and not "if the weight limit is greater than the bag weight". – Thomas Kläger Sep 27 '16 at 20:21

4 Answers4

1

You can freely change the order of the two variables as you prefer. Eclipse (or the compiler) doesn't care, it just evaluates the expression and returns a value, either true of false.

andyfed
  • 41
  • 7
0

can I swap those two variables' places such as following? (WEIGHT_LIMIT < bagWeight)

Yes, it will still work exactly the same.

Order only comes in to play when using short circuit operates such as || or &&.

examples:

if (boolean1 || boolean2)

In this case, boolean1 will be evaluated first. If it evaluates to true, then boolean2 will not be evaluated, since the first one meets the criteria of the if statement.

if (boolean1 && boolean2)

In this case if boolean1 is evaluates to false, then boolean2 will never be evaluated because the fact that boolean1 is false means that even if boolean2 was true, the condition of the if statement would never be satisfied.

Hope that helps.

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
  • 1
    "Order only comes into play..." Order of operands is important if the operands have side effects. For example, `i++ < i` isn't equal to `i > i++`. – Andy Turner Sep 27 '16 at 20:38
0

The order that Java evaluates && and || is not so important if everything is already evaluated into variables as in your example. If these where method calls instead then the second part of the "if" will not be called unless necessary. Examples of when the myMethod() would not be evaluated at all.

if (true || myMethod())

or

if (false && myMethod())

That's why you might see statements similar to this in actual code.

String myStr = null;
if (myStr != null && myStr.trim().size() > 0)

If Java were to evaluate the second part then you would get a NullPointerException when myStr is null. The fact that Java will bypass the second part keeps that from happening.

fedup
  • 1,209
  • 11
  • 26
0

The order of the operands doesn't matter in your specific case. Since the if statement is comparing two values, the two values must be evaluated. However, there are some cases when order does matter:

  1. || and &&

|| and && are shorthand logic operators. The second operand of || will not be evaluated if the first is true. The second operand of && will not be evaluated if the first is false.

  1. ++ and --

These can yield different results:

if (i++ > i)

if (i < i++)

In the first line, i is evaluated after the increment is done, so the first operand is 1 less than the second.

In the second line, i is evaluated first, which evaluates to i, then i++, which evaluates to i as well.

Sweeper
  • 213,210
  • 22
  • 193
  • 313