IDE suggests me to change `stringValue.substring(0...
# intellij
p
IDE suggests me to change
stringValue.substring(0, index)
to
stringValue.take(index)
but those functions are different in case of index > string length: substring will throw StringIndexOutOfBoundsException, take - will return original string.
b
Are you saying there are cases where throwing is preferable?
p
I'm saying that those two functions are semanticaly differ and should not be suggested by IDE for replacement.
b
I guess this is debatable, the IDE can suggest to improve the code, even if that makes it behave differently. But I agree that maybe the different behavior should be clearly specified.
p
Copy code
val s = try {
    "abcd".substring(0, 10) // suggestion to change to take fun is incorrect and it would change semantics
} catch (_: StringIndexOutOfBoundsException) {
    "outOfBounds"
}
println(s)
b
Yes I understand that, but is that a realistic piece of code? Should this useful (in my opinion) suggestion not exist because of this unlikely scenario?
p
It doesn't matter.
b
All right then 😅
l
I'd say the catch is unrealistic, but replacing with
take
could easily hide errors.