HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Insert a character into a string

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
intostringinsertcharacter

Problem

For practicing reasons I would like to write a method which inserts a character into a string.

I would like to know:

-
What is the best practice concerning placement of a comment within methods? For example, is the way that I commented about what raises an exception fine, or are there better practices? Would I need more comments, and if so, where?

-
In this particular example, are throwing unchecked exceptions preferred or checked?

-
How can I make this method more reliable?

-
How can I make this method more scalable?

-
While dealing with immutable strings there are always discussions around performance. Can I make this method run faster?

-
Is this code thread safe?

/**
 * Inserts the character ch at the location index of string st
 * @param st
 * @param ch
 * @param index
 * @return a new string 
 */
    public static String insertCharAt(String st, char ch, int index){
        //1 Exception if st == null
        //2 Exception if indexst.length()

        if (st == null){
            throw new NullPointerException("Null string!");
        }

        if (index  st.length())
        {
            throw new IndexOutOfBoundsException("Try to insert at negative location or outside of string");
        }
        return st.substring(0, index)+ch+st.substring(index, st.length());
    }

Solution

-
The Javadoc style headers you have provided for this method are already more than enough. If you are adding comments to your method it should only be either for API documentation, or where the code is sufficiently complex to warrant an explanation that you cannot provide by simplifying or more appropriately naming the variables.

You should however make it clear that your method throws these two exceptions by including them in the method definition, e.g.

public static String insertCharAt(String str, char ch, int index) throws NullPointerException, IndexOutOfBoundsException


This way it will be obvious from reading the method definition that these exceptions can be thrown, and your comment will be unnecessary when we read the line above each exception-throwing statement and see the logic that causes it to be thrown.

In general, if your comment is telling us nothing more than what the code already does, make your code as simple and readable as possible and delete the comment. At best it will add noise to the code file and at worst it will get out of sync with the code and become misleading.

-
In this example, presumably prefer to use a checked exception so that the method calling insertCharAt and can catch these exceptions and handle them in some way. See also https://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions

-
This depends what you mean by more reliable. Do you mean robust in regards error cases?

-
This also depends on what you mean by more scalable. What do you mean?

-
This method is very simple and there isn't a great deal of room for performance improvement that I can see.

Edit:

-
This method is thread safe as it does not modify any parameters passed to it (and strings are immutable anyway) and does not modify any class-local variables. Even if you had assigned the return value to a temporary variable before returning it, it would not be shared between threads as method level variables belong to each thread's stack.

Code Snippets

public static String insertCharAt(String str, char ch, int index) throws NullPointerException, IndexOutOfBoundsException

Context

StackExchange Code Review Q#44309, answer score: 5

Revisions (0)

No revisions yet.