This is a good question, and I think the preference that should be used comes down to what is the most common use case. Most use cases are equally simple using the API, but consider this:
You want to get a substring starting at 5 and ending at the end of the line. Using an index-based version (assuming the second index is exclusive), it is as simple as:
str.subString(5, str.length());
With API supporting length:
str.subString(5, str.length() - 5);
This second approach is much less concise and clear. However, this can be solved by simply stating that if the length leads to overflowing the remaining line, it will gracefully support this (e.g. str.subString(5, str.length()); will capture everything from index 5 to the end, even if he can request more characters than left). Ruby does this with the String # splice method in addition to supporting advanced things like negative indices.
In my opinion, the index-based approach is more specific, especially when negative indexes are not allowed. This makes it very obvious what to expect from the API, which may be good; which makes shooting in the leg difficult. However, a well-documented API such as Ruby makes it easy to expand the programmer's capabilities and can make an elegant substring.
I also found that in general, when I perform substring operations, I often know my starting and ending points. When using a length-based approach that would require additional computation when calling the API (e.g. substring(startIndex, endIndex - startIndex) ).
Marc baumbach
source share