"Heikki Kallasjoki" wrote in message news:slrnjra97...@iris.zem.fi...
On 2012-05-17, Skybuck Flying <Window...@DreamPC2006.com> wrote:
>> private static void rangeCheck(int arrayLen, int fromIndex, int toIndex)
>> {
>> if (fromIndex > toIndex)
>> throw new IllegalArgumentException("fromIndex(" + fromIndex +
>> ") > toIndex(" + toIndex+")");
>> if (fromIndex < 0)
>> throw new ArrayIndexOutOfBoundsException(fromIndex);
>> if (toIndex > arrayLen)
>> throw new ArrayIndexOutOfBoundsException(toIndex);
>> }
> A very perverse way. The mission is to check if indexes are outside the
> array "out of bounds".
>
> Furthermore it makes no sense, copieing from high to low should be
> possible
"
Who said anything about copying?
"
It seems likely this code is called right before the array is accessed to
try and prevent an access violation, instead it tries to throw a somewhat
nicer exception message to indicate a problem with array access.
(Reading/writing to an array can be considered "copieing").
"
I believe it is checking whether the range fromIndex, fromIndex+1, ...,
toIndex-1 -- i.e., a range given as the first index and one past the
last index, not an uncommon practice -- is within the array, in which
case it has no bugs. (toIndex may legally equal the length of the array
when the range extends to the last element, and requiring the range to
be specified "the right way around" is not especially perverse.)
"
It's simply not valid. Out of bounds has a very clear meaning in programming
practice. It's either within bounds or it's not. The bounds of a java array
are very clearly defined. Thus the only logical conclusion is that the code
is simply bugged.
Either in logic or in description. Either change the exception-description
or fix the code.
If this were some stack based structure then it would make sense that the
index could go one beyond the size. However it's not a stack, but an array.
The perversity is not with checking if "to" is beyond "from". The perversity
is the logical dependency of the "from" out of bounds check onto the "to"
out of bounds check.
Both indexes can be considered separate indexes and there a simply routine
which would check each index individually would be better/preferred.
However this routine seems to try and do multiple tasks all at once, which
is another bad coding practice.
Therefore advice:
1. Split routine up into two separate routines:
First routine to check if a single index is out of bounds.
Second routine to check if the indexes are in proper order.
Further more checking the indexes independently might even execute faster
because of instruction level parallelism, so this attempted optimization
might actually be counter productive, except for perhaps less instruction
decoding.
The call overhead of using multiple routines could be counter acted by
inlining features. I am not sure if java has "inlining" support.
Bye,
Skybuck.