java - Why usage of prefix incrementation is considered better than postfix incrementation in standard for construction -
i installed checkstyle plugin eclipse , think awesome. 1 of warnings gives me bit obscure. exact warning "using ++
not allowed". postfix ++
in row like
for(int = 0; < something; i++)
ok, 'm aware foreach
better construction iteration, can't applied everywhere, old-school ++
alternative.
when change row
for(int = 0; < something; ++i)
the warning disappears. know difference between i++
, ++i
, point of life considered them interchangeable in standard for
construction. checkstyle considers i++
harmful (or error prone).
question: why prefix incrementation better postfix incrementation in for
constructions? or... checkstyle wrong that?
postfix incrementation makes sense when used in expression need old value prior modification. in void contexts value discarded (as in for
loop), saving old value makes no sense.
in other words:
// makes sense because need old value subscript array stack[top++] = x; // in void context, old value discarded top++;
in c++ in particular, both of these operators can overloaded, , implementation of postfix 1 can inefficient because of requirement return old value - involves copying old object comply original semantics of postfix operator.
with primitive types, decent compiler produce identical code both of cases, second 1 better semantic standpoint of language.
Comments
Post a Comment