Re:Strict APIs vs. Forgiving APIs
by thamer on Wednesday October 28, 2020 @02:41PM
(#24922517)
For reference, this is what String.substring(int, int) does[1] in Java:
public String substring(int beginIndex, int endIndex) {
int length = length();
checkBoundsBeginEnd(beginIndex, endIndex, length);
...
Where checkBoundsBeginEnd[2] does:
static void checkBoundsBeginEnd(int begin, int end, int length) {
if (begin < 0 || begin > end || end > length) {
throw new StringIndexOutOfBoundsException(
"begin " + begin + ", end " + end + ", length " + length);
}
}
[1] https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/f0ef2826d...
[2] https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/f0ef2826d...