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.