Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do languages handle side effects of compound operators?

Assume such situation:

int a = (--t)*(t-2);
int b = (t/=a)+t;

In C and C++ this is undefined behaviour, as described here: Undefined behavior and sequence points

However, how does this situation look in:

  • JavaScript,
  • Java,
  • PHP...
  • C#
  • well, any other language which has compound operators?

I'm bugfixing a Javascript -> C++ port right now in which this got unnoticed in many places. I'd like to know how other languages generally handle this... Leaving the order undefined is somehow specific to C and C++, isn't it?

like image 622
Kos Avatar asked Feb 14 '11 13:02

Kos


2 Answers

According to the ECMA Script specification, which I believe javascript is supposed to conform to, in multiplication and addition statements, it evaluates the left hand side before evaluating the right hand side. (see 11.5 and 11.6). I think this means that the code should be equivalent to

t = t - 1;
int a = t * (t - 2);
t = t / a;
int b = t + t;

However, you should not always trust the specification to be the same as the implementation!

Your best bet in confusing cases like this is to experiment with various inputs to the ambiguous lines of code in the original operating environment, and try to determine what it is doing. Make sure to test cases that can confirm a hypothesis, and also test cases that can falsify it.

Edit: Apparently most JavaScript implements the 3rd edition of ECMAScript, so I changed the link to that specification instead.

like image 99
Null Set Avatar answered Oct 22 '22 08:10

Null Set


Practically speaking, if you have to ask or look up the rules for an expression, you shouldn't be using that expression in your code. Someone else will come back two years from now and get it wrong, then rewrite it and break the code.

If this was intended as a strictly theoretical question I unfortunately can't offer details regarding those other languages.

like image 35
Mark B Avatar answered Oct 22 '22 09:10

Mark B