|
Authored by: Anonymous on Thursday, April 19 2012 @ 11:32 AM EDT |
In my opinion.
It doesn't even do anything significantly functional, merely
checks that the supplied values are reasonable, and within
the bounds of the array, and causes an exception condition
if they are not. It doesn't handle the exception, or decide
what to do with it, merely reports to the application that
the error has occurred (and the application then decided wht
to do, depending on the context, but that's out of the scope
of this.)
As an analogy with a filing cabinet, this would be like you
holding up a piece of paper next to a filing cabinet, and if
the paper is wider, you say "It won't fit".
There is literally nothing to this, it's boilerplate code
and contains no creativity, and almost no meaningful
functionality (though the checks are important in some
applications, they are extremely basic in scope).
Code like this appears in every significant application in
every language.[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Thursday, April 19 2012 @ 11:40 AM EDT |
It would be helpful to know what we are talking about. [ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Thursday, April 19 2012 @ 11:55 AM EDT |
Goodness me, I can find rangeCheck type SUBS that I wrote in Sinclair Basic
>30 years ago!
Sanity checks of input or results are as old as the hills and no more original.
Mac
[ Reply to This | Parent | # ]
|
|
Authored by: bugstomper on Thursday, April 19 2012 @ 01:19 PM EDT |
I won't opine on how creative it is, but I can show what kinds of choices can be
made in writing it.
Here are the nine lines of code, copied here from the
OpenJDK 7 Arrays.java file (and repeated identically in the OpenJDK 7
TimSort.java file) which has to be a permitted use: If you can't copy nine lines
of GPLd source code in a comment posted to a web site like this, of what use is
the GPL?
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);
}
Here is
what the API specification says about those thrown exceptions. The person
writing this code would have worked from this specification (this is an
excerpt):
* @throws IllegalArgumentException if {@code
fromIndex > toIndex} or
* (optional) if the
comparator is found to violate the
* {@link
Comparator} contract
* @throws ArrayIndexOutOfBoundsException if {@code
fromIndex < 0} or
* {@code toIndex >
a.length}
Note that the three if tests and their throw
statements follow the order they are described in the specifications. The code
in this version results in particular error messages when each of the three
conditions are met. Someone implementing their own version of this would not
only be following the API specification but might also want to make sure that
the error messages look familiar to someone used to working with the Sun
version. That would cause even a bit more similarity in how the code was
written.
Here is the functionally equivalent private function that is in an
older version of Android Arrays.java file. It is almost the same, but still
follows the order that things are described in the API specification and
produces similar but not identical error messages.
Note that this
checkFillBounds function is not what is claimed to be copied even though it is
very similar to rangeCheck in the JDK Arrays.java. This may show you just how
close an independent naive implementation of the words in the API Specification
would come to the nine lines of rangeCheck.
This older version of Android
did literally copy the JDK Arrays.java rangeCheck function into TimSort.java.
Here is the independent implementation in old Android
Arrays.java:
private static void checkFillBounds(int
arrLength, int start, int end) {
if (start > end)
{
throw new IllegalArgumentException("start <
end: " + start + " < " + end);
}
if (start
< 0) {
throw new
ArrayIndexOutOfBoundsException("start < 0: " +
start);
}
if (end > arrLength)
{
throw new ArrayIndexOutOfBoundsException("end >
array length: " +
end + " > " +
arrLength);
}
}
Finally here is what
the function looks like in the current version of Android, after a change in
December 2010. At that time the function in Arrays.java was written to be
smaller instead of literally following the steps in the API Specification, while
still producing the same functional result. Also it was changed from private to
public so that it could be called from other classes, the name was changed from
checkFillBounds to checkStartAndEnd, and the rangeCheck function in TimSort was
removed, the calls to rangeCheck being replaced with calls to
Arrays.checkStartAndEnd.
public static void
checkStartAndEnd(int len, int start, int end) {
if (start < 0
|| end > len) {
throw new
ArrayIndexOutOfBoundsException("start < 0 || end >
len."
+ " start=" + start + ", end=" +
end + ", len=" + len);
}
if (start > end)
{
throw new IllegalArgumentException("start >
end: " + start + " > " +
end);
}
}
[ Reply to This | Parent | # ]
|
|
|
|
|