API design: (start,length) vs (start,end)

Per Bothner per at bothner.com
Tue Nov 20 10:23:58 PST 2012


The Java APIs have many methods that select a slice of a
sequence or an array, but there is no consistency as to
how to specify the slice: Many places uses (fromIndex,
toIndex), and many other places uses (fromIndex,length).

Specifically, I notice that java.util.streams.Streams uses
(fromIndex,length), while *older* methods in java.util.Arrays
use (fromIndex, toIndex), but the newly added ones use
(fromIndex,length).

This inconsistency is unfortunate.  As mentioned the Java APIs
are already inconsistent, so to some extent we just have to
live with it, but the inconsistency within the Arrays class
seems extra unfortunate.

Was there a considered decision to use (fromIndex,length)
instead of (fromIndex,toIndex) in new methods?  When designing
my own APIs is there any reason to choose one or the other?
-- 
	--Per Bothner
per at bothner.com   http://per.bothner.com/


More information about the lambda-dev mailing list