Circular increment: Which is "better"?

Posted by Helper Method on Stack Overflow See other posts from Stack Overflow or by Helper Method
Published on 2010-05-10T09:26:51Z Indexed on 2010/05/10 9:34 UTC
Read the original article Hit count: 167

Filed under:
|
|

When you have a circular buffer represented as an array, and you need the index to wraparound (i.e., when you reach the highest possible index and increment it), is it "better" to:

return (i++ == buffer.length) ? 0: i;

Or

return i++ % buffer.length;

Has using the modulo operator any drawbacks? Is it less readable than the first solution?

© Stack Overflow or respective owner

Related posts about subjective

Related posts about coding-style