Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: Oracle/Google demonstrate human beings cannot write 10 lines of code without making a mistake ;) Date: Thu, 17 May 2012 18:35:57 -0600 Organization: cows with guns Lines: 52 Message-ID: References: <29308868.1994.1337265697084.JavaMail.geo-discussion-forums@pbcuc6> <84131$4fb54067$5419acc3$20839@cache90.multikabel.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="PnllQd880uOddfy6hsxHuQ"; logging-data="21446"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+s85EKrwgHLB1bHbgknlrt" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:8mua+6eWTx/BjyDpfX2Rq/asxms= sha1:/zBlSrvf1KL5NnOxeY6xbF6rBPA= Xref: csiph.com comp.lang.java.programmer:14610 Heikki Kallasjoki writes: > On 2012-05-17, Heikki Kallasjoki wrote: >> On 2012-05-17, Skybuck Flying 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); >>>>> } >>> >>> 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 >> >> Yes, but it is not designed for testing whether fromIndex and toIndex >> are within those bounds. It is used to test whether fromIndex, >> fromIndex+1, ..., toIndex-1 are, which it does. In particular, the >> element with index "toIndex" is not part of the range it's testing, so >> it does not need to be inside the array bounds. > > For "proof", see the original comments that accompany the rangeCheck > function: > > /** > * Checks that fromIndex and toIndex are in range, and throws an > * appropriate exception if they aren't. > * > * @param arrayLen the length of the array > * @param fromIndex the index of the first element of the range > * @param toIndex the index after the last element of the range > * @throws IllegalArgumentException if fromIndex > toIndex > * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 > * or toIndex > arrayLen > */ > > Note in particular the words "the index after the last element of the > range". > > (Source: > http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/TimSort.java?view=co > ) This is in fact a good example of someone doing everything exactly right: the JavaDoc provides a clear, unambiguous specification and the code implements it exactly. I can only wish all code were this "buggy". -- Jim Janney