Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

StringBuilder inconsistent delete methods

StringBuilder builder = new StringBuilder();

builder.append("hello");

The length of builder is 5.
So, if I call builder.deleteCharAt(5) I rightfully get an IndexOutOfBoundsException,
yet I can call builder.delete(0,5) and this works successfully.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t think the creators of the library did not make this deliberate. So, why the inconsistency?

>Solution :

I’d say this is fine. The intent is different.

Lets try this for example:

String builder sb = new StringBuilder();
sb.append("hello").append("hello");
sb.delete(0,5);
sb.toString();

It prints "hello".

Had you read the documentation, you’d see that the end is non-inclusive, meaning it will delete from start to end-1, hence no IndexOutOfBounds.

However, try this:

StringBuilder builder = new StringBuilder();

builder.append("hello");
builder.delete(0,7);

and you will most likely get an index out of bounds.

EDIT:
Personal gripe – it’s a fine balance between stopping someone from shooting themselves in the foot by throwing an exception, and just letting them do it but actually doing nothing in that case.

One example of "you really can’t do this, and will get an exception" is e.g. calling init() twice on something important that should really be initialized only once.

One example of "You shouldn’t do this, but I won’t crash your program if you do" is e.g. calling init() twice on a logger.

In this particular case, trying to delete a character that is out of bounds, falls into the first category. So that "it’s your bug, not mine" kinda thing.

But deleting a range – personally – shouldn’t even throw an exception. If the range is bigger than the length of the string, it’s my thing’s job to delete everything (in the range you specified), and it’s your bug to figure out why you deleted more than you did.

And this is exactly the kind of thing that demonstrates why writing documentation is important.

So that if you did this:

String builder sb = new StringBuilder();
sb.append("hello").append("Jello");
sb.delete(0,6); //e.g. you somehow thought that hello is of length 6
sb.toString();

and get a Jello it’s your thing to figure out why. Your bug. Not mine.
if you did sb.delete(0,7); and get a ello, it’s your thing to figure out why. Your bug, not mine. The docs are clear on what will happen.

But it depends on what the "thing" is. If it’s important, nope, you’ll get an exception. If it’s something stupid, sure, go ahead, shoot yourself in the foot, harmlessly.

However, in this particular case, i’d allow even this code to work fine:

String builder sb = new StringBuilder();
sb.append("hello").append("hello");
sb.delete(0,200);
sb.toString();

and not throw an exception. But internally, i’d only do delete(0,10)

TL;DR – it’s not inconsistent, it’s working exactly like it says it works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading