Finding the Simplest Possible Solution

Never stop thinking to write the cleanest possible code

Nov. 26, 2014, 6 a.m.

When tasked with creating a new feature we normally instantly start thinking of possible ways to implement it. Generally, we can quickly come up with some sort of an idea that we believe will suffice, and begin to implement our feature. We get right to work and our ideas start coming to life. At this stage we can sometimes keep our head down and churn out more and more code relating to our initial idea without thinking about if this is actually the best way to do it. It is necessary to take frequent, higher-level looks at our tech design and verify each time that no better, simpler way has presented itself.

Recently, I was asked to implement a way to order key-value pairs by the respective key's location in a string. Here is a simple example of one of these strings, which I will refer to as a template:


As you can see, this sample template contains 4 placeholders: {section}, {post}, {number}, and {area}. Essentially, what I needed to do was find a way to take a Map of key-value pairs and output a List of the values in order by where their key, the placeholder, occurred in the template. As I began to think about this I initially had the idea of using regular expressions to find out how many placeholders came before the specific one I was looking for and then put the value into the list at that index. This seemed like a nice, clean solution and I began to code.

As I was implementing my solution I started to run into minor setbacks involving my limited understanding of how to use regular expressions in Java. This turned out to be a blessing in disguise as it forced me to frequently stop coding and look up certain aspects of regular expressions with Java which, in turn, made me really evaluate my chosen solution. I quickly realized that everything I was trying to do with the regular expressions could be simplified by finding the index of a substring, my placeholder, in the template.

// Partially completed method
private List<String> orderValues(Map<String, String> placeholderToValue, String template) {
    for (String placeholder : placeholderToValue.keySet()) {
        int index = template.indexOf(placeholder);
    return null;

I now had access to the index of where my placeholder began in the template; all that was left to do was correctly order the values by that index. Naturally, there are many ways to do this, most of them involving looping through and doing some comparisons, but I wanted to find the simplest way to do this as well. I did a little searching around and found out about the data structure known as the TreeMap. A TreeMap is automatically sorted by the key, which I could use as the index that I had just found. All I had to do now was insert the index into the map as the key and my value as the value.

// Fully implemented method
private List<String> orderValues(Map<String, String> placeholderToValue, String template) {
    Map<Integer, String> positionToValue = new TreeMap<Integer, String>();
    // Place the values in the TreeMap
    for (Map.Entry<String, String> entry : placeholderToValue.entrySet()) {
        int index = template.indexOf(entry.getKey());
        positionToValue.put(index, entry.getValue());

   return new ArrayList<String>(positionToValue.values());

With the use of indexOf and a TreeMap I was able to take my fairly complicated, regular expression solution and boil it down to a fairly trivial loop. It is always useful to step back and evaluate your solution at various steps during implementation as you never know when you will come up with something better!

Comments about Finding the Simplest Possible Solution

  • Anonymous:

  • The last loop is unecessary just write: return new ArrayList<String>(positionToValue.values()); If you want to copy the values list
  • Nov. 26, 2014, 7:33 a.m.

  • Stephen Goeddel:

  • @Anonymous That is a very good point, nice catch! I have made that change.
  • Nov. 26, 2014, 7:43 a.m.