Thursday, September 18, 2008

Post & PreIncrement...

Please don't use post-increment. It is just evil.

So use ++i instead of i++.


Because I've seen a *lot* of code like this:
int nValue = pData[ i++ % 32 ];

When they were clearly trying to do this:
int nValue = pData[ ++i % 32 ];

And actually, it should be even clearer:
int nIndex = i % 32;
int nValue = pData[ nIndex ];

Remember to code for readability!

(Bug fixed)


Edmundo said...

Your code has a bug. I think you were trying to write:

int nIndex = i % 32;
int nValue = pData[nIndex];

R Caloca said...

Thanks Edmundo! Bug fixed.