Within a decision structure, does the order of the terms change the result?

9

Is there any difference between:

if (variavelA == variavelB)

and

if (variavelB == variavelA)

In Java and C #, is there any difference between the examples? I emphasize that the question is not limited only to the == operator, but to all that can be used within decision structures.

    
asked by anonymous 25.05.2014 / 18:30

4 answers

6

The order can change the result in languages where there is operator overload .

Differences with overloaded operators

In C #, for example, you can create a specific implementation for the == comparator and several others operators . See a example :

public static DBBool operator ==(DBBool x, DBBool y) 
{
   if (x.value == 0 || y.value == 0) return dbNull;
   return x.value == y.value? dbTrue: dbFalse;
}

Then variavelB and variavelA can be of different types and consequently, the overloaded == method may have been implemented differently in one or even two types.

Some languages that implement operator overload use them as simple method calls. In Ruby, for example, you can even call the operator as any method.

Consider the examples below. Both sum operations result in 3 :

a = 1 + 2 
b = 1.+(2)

The same is for comparison:

if a == b
if a.==(b)

This may be different than:

if b == a
if b.==(a)

Obviously the above examples may differ if a and b are different classes.

No differences when it is not possible to extend the language

In languages such as Java or PHP, the comparison order of the variables is not important, as the == operator will always compare:

  • Values, for primitive variables.
  • If they are objects, if both point to the same instance.
  • Final considerations

    The concepts presented here may vary subtly or abruptly from one language to another or even between versions of the language itself.

    The important thing is for the developer to understand the current mechanism that the language uses underneath the cloths.

        
    26.05.2014 / 16:36
    2

    I will give a partial answer, focused on .NET and Mono.

    The behavior of the == operator varies as follows:

    • For types by reference, it checks whether the references are the same, ie if both objects are actually the same;
    • For types by value, it will execute an implementation of its own type equality. Every kind of value has to have its own logic of equality. Try to set a struct , and then create two instances of that struct and try to verify that they are equal == . Your code will not even compile;) Note that in general the Framework's native structs ( bool , int , float etc.) have their own implementation for this operator.

    Note that in both cases it is possible to overload the == operator. It is actually mandatory to overload it in the second case if you are going to use it in your code.

    If you overload the operator, then the logic of equality is completely your own. You know what the left and right operators are, and you can do whatever you want with them. In that case, whether the order matters or not is a decision of the developer. If the operator is not overloaded, the order really does not make a difference.

    The same goes for comparison operators ( > , < , >= and <= ), if you refer to comparisons that should be equivalent, ie: a < b in relation to b > a . It depends only on the operators being overloaded, and the logic that was used in the overload. For the standard comparable types in .NET and Mono (% with%,% with%, and even% with%), the comparison is strictly numeric, so for these types it does.

        
    26.05.2014 / 17:13
    1

    For the "equal" operator ( == ) the order of the conditions (variables, values and etc) do not change the result.

    Independent of language.

        
    25.05.2014 / 21:43
    0

    In Java it makes no difference at all.

    I've done it several times using the equality operator == and the result is the same

        
    25.05.2014 / 22:17