How do languages ​​handle the side effects of complex statements?

Suppose this situation:

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

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

However, what this situation looks like:

  • Javascript
  • Java
  • PHP ...
  • FROM#
  • ok, any other language that has compound statements?

I am now fixing Javascript -> C ++ - the port where it has become invisible in many places. I would like to know how other languages ​​usually handle this ... Leaving the order undefined is somehow specific to C and C ++, right?

+8
c ++ c javascript undefined-behavior programming-languages
source share
4 answers

According to the ECMA Script specification , which, I believe, should conform to javascript in the multiply and add operators, it evaluates the left side before evaluating the right side. (see 11.5 and 11.6). I think this means the code should be equivalent

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

However, you should not always trust the specification as much as the implementation!

Your best bet in confusing cases like this is to experiment with various inputs to ambiguous lines of code in the original operating environment and try to determine what it does. Be sure to check the cases that can confirm the hypothesis, as well as check the cases that can falsify it.

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

+7
source share

In practice, if you need to ask or find the rules for an expression, you should not use this expression in your code. Someone else will return in two years and make a mistake, then rewrite it and break the code.

If this was intended as a strictly theoretical question, unfortunately, I cannot offer detailed information about these other languages.

+3
source share

For javascript, the article should help.

This article clearly indicates whether a particular combination

a OP b OP c goes from left to right and in what order.

I do not know about other languages.

+2
source share

However, this situation looks like: JS, Java, PHP, C # ...

To be completely frank, int a = (--t)*(t-2); int b = (t/=a)+t; int a = (--t)*(t-2); int b = (t/=a)+t; looks like shit.

It's nice to have a fancy code that can be pretty and elitist to everyone, but this is not necessary. The solution for each language that comes across this code is to add a couple more half-columns (if you are not dealing with python):

 --t; int a = t * (t-2); t /= a; int b = t + t; -or- int b = t * 2; -or- int b = t << 1; //whichever method you prefer 

If a different procedure is required, adjust the corresponding lines. If you are trying to fix the old buggy code, fix the code , not just re-implement other people's spaghetti.

Edit to add:

I realized that I never answered the original question:

How do languages ​​handle the side effects of complex statements?

Poorly.

+1
source share

All Articles