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

Struggling to refactor code to remove duplication

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

Problem

I really need some help removing some duplicated code. I've linked the methods causing me issues. http://pastebin.com/cZJihM4J

As you can see each method does similar things but retrieve different values. i.e one method to insert a string at a certain line. Another to get the the lines index and another to get the contents at a particular line

There are things like:

String text = textArea.getText();
int start = 0;
int count = 0;
String buildNewTextArea = "";


I could make these values global but not sure if this is the best way to approach this.
My brain can't find away around this :(

Thanks in advance

```
public String insertCodeBlock(String newBlock) {
String text = textArea.getText();
int start = 0;
int count = 0;
String buildNewTextArea = "";
while (start >= 0) {
int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());
if (nextLineStart == -1) {
nextLineStart = text.length();
}
String[] temp = diffBlocks.get(indexOfLine).split(",");
int startLine = Integer.valueOf(temp[0]);
int endLine = (Integer.valueOf(temp[1]) - 1) + startLine;
if (count == startLine) {
buildNewTextArea += newBlock;
} else if (count (startLine + (endLine - startLine))) {
buildNewTextArea += text.substring(start, nextLineStart);
}
count++;
start = text.indexOf(NEW_LINE, nextLineStart);
}
return buildNewTextArea;
}

private int getLineIndex(int line) {
String text = textArea.getText();
int start = 0;
int count = 0;
String buildNewTextArea = "";
int index = 0;
while (start >= 0) {
int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());
if (nextLineStart == -1) {
nextLineStart = text.length();
}
if (count == line) {
return index;
}
buildNewTextArea = text.substring(start, nextLineStart);
index += buildN

Solution

It seems like each method is iterating over the list of lines and doing something. It is the "doing something" that is unique. Consider having a single method that iterates over the lines and takes an interface to do the "something". For each method, an appropriate interface instance would be passed (possible anonymous inner class) that takes the appropriate action.

Something like this:

private interface ProcessLine{
     void processLine(String line, int index, int charIndex);
     void complete();
}

private ProcessLine process(ProcessLine processor){
  String text = textArea.getText();
  int start = 0;
  int count = 0;
  String buildNewTextArea = "";
  int index = 0;
  while (start >= 0) {
    int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());
    if (nextLineStart == -1) {
        nextLineStart = text.length();
    }

    processor.processLine(line, count);
    index += buildNewTextArea.length();
    count++;
    start = text.indexOf(NEW_LINE, nextLineStart);
  }
  return processor;
}

private int getLineIndex(final int line) {
     int result = -1;
     return process(new ProcessLine(){
         public void processLine(String line, int index, int charIndex){
             if (index == line)
                 result = charIndex;
         }
     });
     return result;
}

Code Snippets

private interface ProcessLine{
     void processLine(String line, int index, int charIndex);
     void complete();
}

private ProcessLine process(ProcessLine processor){
  String text = textArea.getText();
  int start = 0;
  int count = 0;
  String buildNewTextArea = "";
  int index = 0;
  while (start >= 0) {
    int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());
    if (nextLineStart == -1) {
        nextLineStart = text.length();
    }

    processor.processLine(line, count);
    index += buildNewTextArea.length();
    count++;
    start = text.indexOf(NEW_LINE, nextLineStart);
  }
  return processor;
}

private int getLineIndex(final int line) {
     int result = -1;
     return process(new ProcessLine(){
         public void processLine(String line, int index, int charIndex){
             if (index == line)
                 result = charIndex;
         }
     });
     return result;
}

Context

StackExchange Code Review Q#6797, answer score: 2

Revisions (0)

No revisions yet.