Thursday, September 18, 2008

Post & PreIncrement...

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

So use ++i instead of i++.

Why?

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:
++i;
int nIndex = i % 32;
int nValue = pData[ nIndex ];


Remember to code for readability!

(Bug fixed)

2 comments:

Edmundo said...

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

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

R Caloca said...

Thanks Edmundo! Bug fixed.