Monday, May 16, 2011

var++ & ++var

As far as possible one should use ++i and --i rather than i++ or i--. In a nutshell one should always use the pre increment/decrement forms rather than the post ones.The reason being, post increment retains the old value (usually in a temporary variable) and usually one does not end up using the old value, so that temp variable ends up by just wasting instructions.

Thus always use the pre-increment/decrement statements rather than the post ones. This is most important for complex types like iterators. It tends to be mostly irrelevant for integers