Post- or Pre-Increment

Published August 21, 2004
Advertisement
I have been told off quite often for my use of post as opposed to preincrementing integer variables, especially in my NeHe SDK, as it is bad practice, and therefore teaching bad practice.

The reason for this is that in certain situations a temporary variable is constructed to hold the value before incrementing, so readability must be compromised in the quest for efficiency.

Unfortunately, I try to only use postincrement in for-loops and discrete statements, such as the following example:

for(int ct=0; ct     iter_ct++;


Strangely, when using integers (which is what we're talking about), both the above snippet and its preincrement twin compile to exactly the same code without optimization.

Pre-increment is useful in certain cases, but in most, it is merely a case of premature optimisation.

Efficency does not always mean lack of readability. Readability should always be in the forefront of code design.

Edit: put the NeHe SDK into my webspace, as NeHe doesn't allow remote linking. Thanks for letting me know Ainokea!
Previous Entry First Entry
Next Entry Documenting
0 likes 1 comments

Comments

Ainokea
Your link does not work.

EDIT:nevermind works now.
August 21, 2004 09:59 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement