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 other 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;
}
So variavelB
and variavelA
may be of different types and consequently the method overloaded ==
may have been in implemented differently in one or even in the 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 value 3
:
a = 1 + 2
b = 1.+(2)
The same goes for comparison:
if a == b
if a.==(b)
This might 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 order of comparison of variables is not important, because the operator ==
will always compare:
- The values for primitive variables.
- If they are objects, if they 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 that the developer understands the current mechanism that the language uses underneath the scenes.
In what language? This varies.
– Maniero
Any language: Java, C#, VB, Shellscript, Ruby, PHP...
– ptkato
The problem is reticence. I can’t speak for all these languages. But in C++, for example, the order of the operands can make a difference depending on how the operator was implemented. I can’t guarantee that all languages
"abc" == "abcdef"
and"abcdef" == "abc"
give equal results. In fact I know that few people know that a slightly different way givestrue
in a case andfalse
in another. Perhaps if you explain more where you want to get this question, you can produce more interesting answers.– Maniero
In PHP
1 == "1"
givestrue
but1 === "1"
givesfalse
. The question is about theif
, on the equality operator, the specific operator==
or about relational operations? I find it difficult to answer without having language or type of the known variable, besides having clear about what the question is.– Maniero
Another factor can be how the language treats the null or variable not started, also note that some languages convert types automatically and this could change results in some way, apart from this "equal" should be "equal".
– Motta
In this case, the question relates to the order of the terms.
– ptkato
At least we know that the issue has nothing to do with
if
. Order of terms in what? Order specifically on==
? What if the language doesn’t have exactly that operator? What if it has other forms of equal operator? What if it uses one<
or>=
? Is this important to the question or just the==
even?– Maniero
"Independent of compilers or languages" can not answer! No one knows all the languages that exist, and it is quite possible that there is one where the order of the factors makes a difference (me I know none, but I know few languages). There is no universal operator definition for all languages, each implements this with its own terms.
– bfavaretto
And I didn’t even mention the typing. What type are the two "terms"? In some languages this may not make a difference, there is language that makes. I am trying to save the question, for this these questions need to be turned. Otherwise, it is too broad.
– Maniero
The order of the assessment of both sides of equality can also affect the outcome. Imagine that one operand is a variable, and the other is a function that changes the value of that variable... Any language that has side effects is subject to this situation (perhaps except if the language makes a rigid separation between procedure and function - and not let subroutines with side effects return values). And this without getting into the merit of Weak Typing, overload of operators, etc.
– mgibsonbr
This will depend on the language!!! so if you could improve the question is to limit which! who knows improvement and would be of great value this question @Patrick.
– user6026
Okay, I made a few changes and limited it to two languages I use.
– ptkato
It has improved a lot. I still have doubts if it is easy to answer because to have a correct answer, one needs to understand about both languages. I had not been able to speak properly about Java. And if it is to have separate answers, perhaps I will fall into this problem http://meta.pt.stackoverflow.com/questions/289/o-que-fazer-com-questions-que-podem-ter-multiplas-answers. Even to answer in C# although it brings a good learning, at the beginning of the research I’ve realized that there may be so many unusual cases, I doubt I’ll cover all.
– Maniero
So the answer would be: under normal conditions the order of equality and inequality affects nothing. But there are countless really extraordinary situations that can make that not true. In other relational operators can already make all the difference. Simple:
1 > 3
and3 > 1
produce obviously different results.– Maniero
What about
1 > 3
and3 < 1
?– ptkato
@Patrick if you change the operator you are making another completely different comparison, you are no longer talking about order terms in the same operation.
– Maniero
I do not know if terms would be the most appropriate word... But in short, it would be this: operators and values.
– ptkato
I tried to save it. This question is very broad and without a complete chapter of a book it can only be partially answered (the answers show this), even after the editions and attempts of clarification (which I think has worsened the situation, became even wider). The problem is very complex. The only way to respond succinctly is "anything can happen". Only one example of things that can affect can be found in http://answall.com/questions/18910/qual-a-diferenca-no-uso-do-metodo-equals-para-o-operator/18920#18920. Many other factors exist.
– Maniero