• 9
• 9
• 10
• 9
• 10
• entries
8
15
• views
8767

# Post- or Pre-Increment

302 views

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!

## 1 Comment

EDIT:nevermind works now.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account