Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment Operator Precedence Failure

 
Dennis Putnam
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me why this does not not work? According to the documentation ++ has precedence of 2 while >= has a precedence of 6.

The expected output is:

the actual output is:

if I initialize i to 10 I get:

It sure seems to me that the ++ operator is not executing with a higher precedence than >=.
When I change the code to this, I get exactly what I expect:
 
Campbell Ritchie
Sheriff
Posts: 48442
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of question comes up all the time. The i++ operator has the highest precedence of all, shared with i--, with only separators like () and . having higher precedence.
What you are misinterpreting (as everybody else seems to), is what the ++ increments. It alters the value of i but does not alter the value of i++. Yes, you have two values. The value of i behind the scenes might be 10, but the value of i++ that you are using is actually 9.
A similar problem is discussed in this FAQ.
 
Dennis Putnam
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dopey me. Of course that is way it works. What I should have done was:

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48442
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome

You should always be aware of that sort of side‑effect.
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should always be aware of that sort of side‑effect.


And in my opinion, you should avoid writing code which relies on it. If I was working on that code, I would have to stop and ask myself what it did. And I would probably have to go off and read something like the Ranch's FAQ to make sure I understood it correctly.

And code which makes me do that is dangerous -- I might just do what you did and incorrectly assume I knew what it did -- and time-wasting -- I have to stop, figure out what it does, and then try to pick up my original train of thought. Better to rewrite it in such a way that you don't use the value of i++ or ++i.
 
Dennis Putnam
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the sentiment but that is a pretty common construct in other languages (C, Perl, PHP, etc.) and very commonly used. Indeed I use so much that I take it for granted and somehow ignored all my programming experience with it in this case I guess because I am a Java beginner.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:Can someone explain to me why this does not not work? According to the documentation ++ has precedence of 2 while >= has a precedence of 6.


It sure seems to me that the ++ operator is not executing with a higher precedence than >=.


Also note that the conclusion isn't correct -- just because the value isn't what is expected doesn't mean that the precedence "failed". If the ">=" operator indeed did have a precedence greater than the "++" operator, then you wouldn't get that value. Heck, it wouldn't compile, as the ">=" operator won't have a left operand.

Henry
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:I understand the sentiment but that is a pretty common construct in other languages (C, Perl, PHP, etc.) and very commonly used.


It is a particular case of the general rule of thumb that functions with side-effects are evil. Generally if you have a function which returns a value, it should just return that value and not also change something else as well. As you can see, both i++ and ++i return values and also change the value of their operand. So they are evil.

Of course we write functions which are supposed to change things all the time -- changing things is why we write the programs containing those functions, after all. But functions which change things should just do that and nothing else. Returning values from such functions just confuses the matter and makes it harder to understand code which contains such functions.

And that's my point. The languages you mentioned there are all notorious for the way they make it easy to produce "write-only" code, i.e. code which can be easily written but not easily understood. I'm here to say you should produce easily-understood code.
 
Campbell Ritchie
Sheriff
Posts: 48442
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason you see more write‑only C code than Java code is that is is (or used to be) fashionable to write C code to use the fewest keystrokes possible. Maybe to conserve memory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic