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

Jonathan Gibbons jonathan.gibbons at oracle.com
Tue Nov 20 10:52:51 PST 2012


Don't forget to examine the added factor of closed intervals vs 
half-open intervals.

-- Jon

On 11/20/2012 10:34 AM, Brian Goetz wrote:
> That's a good point.  I had thought there was more precedent for (start,
> length) but I am not finding it right now.  (The inconsistency is made
> more unfortunate by the fact that both use (int,int) so its easy to get
> it wrong.)
>
> We'll take a look and see what the impact would be here.
>
> On 11/20/2012 1:23 PM, Per Bothner wrote:
>> 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?
>>



More information about the lambda-dev mailing list