Backspace character

In December I asked a question: What’s wrong with this code? I agree that the question was rather broad without context. I just asked it in the same way a friend asked me.

Many people noticed '\b' character in the code… Is it good or bad? Well, it depends.

What is '\b' character?

Quote from an answer by T.J. Crowder at Stack Overflow: On most terminals or consoles, '\b' is a nondestructive backspace. It moves the cursor backward, but doesn’t erase what’s there.

Another answer provides a detailed explanation of how the backspace character works.

If you’re interested, see The “backspace” escape character ‘\b’ in C, unexpected behavior? question.

What can be wrong?

As long as the output is written to terminal or console, it does what it’s supposed to do, most of the time at least. But what about capturing the output to a file? Then you’ll see a weird character in the file.

In my opinion, backspace character in that code serves no purpose. Much better way would be just to remove the last space from the buffer:

sb.deleteCharAt(sb.length() - 1);

It is the safest approach and it has no (possible) undesirable effects.

Another issue is if objs is empty, the opening '[' would be removed from the string. I think the assumption here is that objs is never empty which can be enforced by the calling code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: