Why does a `StringBuilder`have a method `append` b...
# announcements
r
Why does a `StringBuilder`have a method
append
but no method
prepend
, that does not make much sense to me. Anyone knows why there's none?
r
Because you can use
insert(0, ...)
(or any other index for that matter)
a
There is `Insert`: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#insert(int,%20java.lang.String) But yeah, maybe there should be some sugar. Keep in mind, that prepending is expensive operation, unlike appending.
r
There is none, but that's probably because I am doing MPP? 😞
a
It's also available for JVM:
typealias StringBuilder = StringBuilder
👍 1
but yes, nothing else except JVM and native
r
I'll give it a try in #C3PQML5NU Tyvm
k
You can probably write an extension function if you plan to use prepend a lot
r
@kd IntelliJ suggested that, but I can only do that in the files itself (so the dependencies), which are read only 😞
d
It's because it would be very inefficient implementation wise to support it
1
The most common use case of StringBuilder is to avoid concatenation of many strings, creating many intermediate steps
It's most efficient to put them in an array from left to right and not have to move them around
j
There’s no reason why a circular buffer can’t be used, which means prepending or appending should both be equally cheap operations. Only inserting in the middle would be “expensive”. Even if it’s not implemented that way currently, the performance cost of a specific implementation should not be a good reason to cripple an API design.
👌 1
d
StringBuilder just isn't intended to be used like that. You can do it though with insert. The vast majority of use cases can simply be expressed using appends, which makes it a StringBuilder, not a CharacterList. For a silly analogy - you don't move half of a house over after you've built it to make space for the other.
It's just a buffer that has improved repeated concatenation performance because it is not required to be immutable
r
StringBuilder just isn't intended to be used like that.
How do you figure?
You can do it though with insert.
Problem is, there is no insert..
For a silly analogy - you don't move half of a house over after you've built it to make space for the other.
That's a strange analogy imho, which is not about the use case; we want to prepend, that's not the discussion. If the implementation for 'insert'/'prepend' is to move the house, then maybe the implementation is inefficient, but I agree with @Jay that it is strange to leave it out because of that. What about reverse + append + reverse? Even more inefficient I'd think?
d
There is insert
I agree that they could have made the implementation such that operations other than append and replace would theoretically perform a bit better
But what I'm saying is I think they didn't because it is not the target use case
If you're in a situation when you need such control of a String you're making, and the performance of insert is too bad, it might be a good idea to make a StringBuilder that is tailored to your use case.