Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Post- or Pre-Increment

Sign in to follow this  


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!
Sign in to follow this  

1 Comment

Recommended Comments

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

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!