Agile FAQs
  About   Slides   Home  

 
Managed Chaos
Naresh Jain's Random Thoughts on Software Development and Adventure Sports
     
`
 
RSS Feed
Recent Thoughts
Tags
Recent Comments

Refactoring Teaser Part 1

How would you refactoring the following code? (This code is in Java, but you can refactoring using any language of your choice).

Following test explains the functionality of the production code:

1
2
3
4
5
6
7
8
9
public class StringUtilTest {
  @Test
  public void testSplit() {
    assertEquals("'Hello', 'World', 'Java', 'Hello World', 'World Java', 'Hello World Java'", StringUtil.split("Hello World Java", 6));
    assertEquals("'Hello', 'World', 'Java', 'Hello World', 'World Java', 'Hello World Java'", StringUtil.split("Hello World Java", 10));
    assertEquals("'Hello', 'World', 'Java', 'Hello World'", StringUtil.split("Hello World Java", 4));
    assertEquals("'Hello'", StringUtil.split("Hello World Java", 1));
  }
}

General use case is that for a given string (content), users might want split the same string to get different numbers of keywords in the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public final class StringUtil {
  private static final Pattern REGEX_TO_SPLIT_ALONG_WHITESPACES = Pattern.compile("\\p{Z}|\\p{P}");
 
  public static String split(final String content, final int number) {
    String listOfKeywords = "";
    int count = 0;
    String[] tokens = REGEX_TO_SPLIT_ALONG_WHITESPACES.split(content);
    List<string> strings = Arrays.asList(tokens);
    List<string> allStrings = singleDoubleTripleWords(strings);
    int size = allStrings.size();
    for (String phrase : allStrings) {
      if (count == number) {
        break;
      }
      listOfKeywords += "'" + phrase + "'";
      if (++count < size && count < number) {
        listOfKeywords += ", ";
      }
    }
    return listOfKeywords;
  }
 
  private static List<String> singleDoubleTripleWords(final List<string> strings) {
    List<string> allStrings = new ArrayList<string>();
    int numWords = strings.size();
 
    if (hasEnoughWords(numWords) == false) {
      return allStrings;
    }
 
    // Extracting single words. Total size of words == numWords
 
    // Extracting single-word phrases.
    for (int i = 0; i < numWords; ++i) {
      allStrings.add(strings.get(i));
    }
 
    // Extracting double-word phrases
    for (int i = 0; i < numWords - 1; ++i) {
      allStrings.add(strings.get(i) + " " + strings.get(i + 1));
    }
 
    // Extracting triple-word phrases
    for (int i = 0; i < numWords - 2; ++i) {
      allStrings.add(strings.get(i) + " " + strings.get(i + 1) + " " + strings.get(i + 2));
    }
    return allStrings;
  }
 
  private static boolean hasEnoughWords(final int numWords) {
    if (numWords < 3) {
      return false;
    }
    return true;
  }
}
  • http://kerrybuckley.org/ Kerry Buckley

    Here’s my attempt at refactoring it in Ruby.

  • http://kerrybuckley.org/ Kerry Buckley

    Here’s my attempt at refactoring it in Ruby.


    Licensed under
Creative Commons License