- Feb 8, 2004
- 12,604
- 15
- 81
So im simulating a circular buffer in java using an Array, there's a line of code that prevents the Array from going out of bounds.
Thats this line, size is the size of the Array:
It took me a while to figure out that's what the "% size" is actually for (i know its modulus i mean why it was there in the first place) and im still not 100% sure on it. Its like (in + 1) modulus size always keeps it in bounds because it just does is my current level of understanding of this :|
I wrote this instead:
Yeah its longer yeah it takes 4 lines instead of 1 but i find that infinitely easier to understand than the other line, if something then something otherwise ignore it and continue. Is what i wrote significantly worse than the original line?
Thats this line, size is the size of the Array:
Code:
in = (in + 1) % size;
It took me a while to figure out that's what the "% size" is actually for (i know its modulus i mean why it was there in the first place) and im still not 100% sure on it. Its like (in + 1) modulus size always keeps it in bounds because it just does is my current level of understanding of this :|
I wrote this instead:
Code:
if (in == (size - 1))
{
in = 0;
}
Yeah its longer yeah it takes 4 lines instead of 1 but i find that infinitely easier to understand than the other line, if something then something otherwise ignore it and continue. Is what i wrote significantly worse than the original line?