|
Authored by: TheOldBear on Thursday, May 10 2012 @ 09:51 AM EDT |
Back in the dark ages of PC C compilers, I retrofitted something similar to
range
check to the
library function memcpy(int, void *, void *) to compensate
for
clumsy programmers at our company.
As I recall this was needed in the
supplied libraries for Lattice C, but not in
Computer Innovations C [Microsoft
was selling a re-labeled Lattice C in their
initial release] [ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Thursday, May 10 2012 @ 09:54 AM EDT |
Here's a spec for the function:
The RangeCheck function is only needed in the class it's
part of, should be useable without needing an
object/instance of the class, and
needn't return any values. It should take 3 values, all of
them being integers, the first describing the length of an
array, and the second and third respectively describing the
beginning and end of a range of array indexes within the
range of the array described by the first parameter.
The function should check the validity of these parameters,
and throw an exception where there is a problem.
[For extra points, before reading any further, what should
you test for? Fill in your answer in the space provided
below.]
-------------------------
-------------------------
Answer, and continuation of the spec for the function:
Naturally, the start and end points should be correctly
ordered. Compatibility requires the function should throw an
IllegalArgumentException if they are not. A message would be
useful.
The start index cannot be less than the beginning of the
array. For compatibility, it should throw an
ArrayIndexOutOfBoundsException with a message if it is.
The end point index cannot be beyond the end of the array.
Compatibility requires that it should throw an
ArrayIndexOutOfBoundsException with a message if it is.
-------------------------
This works exactly as you would expect something that checks
bounds to work: it checks that the beginning is before the
end, it checks that the start isn't negative, and it checks
that the endpoint isn't outside of the range. In each case
it throws an exception if it finds a problem. This takes
exactly 3 if statements, spread over 12 or 13 lines, if you
include the method declaration and the close bracket and
blank lines. The spec is longer than the code. There are no
'else' parts to the if statements in the copied code, though
there could be. But they would bring no benefit in the most
common case, so any developer who understands anything about
performance would exclude them.
There are exactly 6 ways you could
order these 3 statements. Two of these ways make slightly
more sense from a clean coding perspective because two of
the three lines should throw the same exception, but it's a
pretty marginal style issue.
So if you were to rewrite
this with the same parameter names, and throwing the same
exceptions, there's a 1 in 6 chance your implementation
would be substantially similar to the Sun implementation.
And much better than 1 in 6 if you ordered your code in the
way I ordered the specification.
This function is not rocket science. Except if you are a
lawyer working for Oracle or an expert witness working for
Oracle, apparently. The description of it is substantially
longer than the actual thing, rather like the brief Oracle
submitted, explaining how very, very important it was.[ Reply to This | Parent | # ]
|
|
|
|
|