admin管理员组文章数量:1295285
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 pound 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?
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 pound 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?
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Feb 14, 2011 at 13:56 KosKos 72.3k27 gold badges172 silver badges238 bronze badges 12- 9 Maybe it's best not to know it :) – Daniel Daranas Commented Feb 14, 2011 at 13:59
- 2 @Daniel: Thoroughly agree! But, unfortunately for the OP, it sounds as if he/she is having to debug legacy code with this kind of crud in it... – Oliver Charlesworth Commented Feb 14, 2011 at 14:01
- I'd say it's best not to know it when you write code, but know it when you read it. :) – Kos Commented Feb 14, 2011 at 14:02
- 1 given this question seems to be about all languages that are not c++ or c, does it really make sense to tag it c++ and c? – jk. Commented Feb 14, 2011 at 16:42
- 3 stackoverflow./questions/3720458/… is the answer for Java , stackoverflow./questions/4644328/… for C# – nos Commented Feb 14, 2011 at 17:16
4 Answers
Reset to default 7According 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.
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 e 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.
For javascript the following article should help.
This article clearly states whether a particular bination of
a OP b OP c
goes from left-to-right and in which order.
I'm don't know about the other languages.
However, how does this situation look in: JS, Java, PHP, C#...
To be perfectly candid, int a = (--t)*(t-2); int b = (t/=a)+t;
looks like crap.
It's nice to have fancy code that can be all pretty and elitist, but there's absolutely no need for it. The solution for every language when confronted with code like this is to add a couple more semi-colons (unless you're 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 order of operations is desired, the adjust the lines accordingly. If you're trying to fix old buggy code, fix the code, don't just re-implement someone else's spaghetti.
Edit to add:
I realized I never specifically answered the original question:
How do languages handle side effects of pound operators?
Poorly.
本文标签: javascriptHow do languages handle side effects of compound operatorsStack Overflow
版权声明:本文标题:javascript - How do languages handle side effects of compound operators? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741612071a2388317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论