Introduction

In this series, I’ll start looking at re-factoring and testing existing code. This project will use sorting algorithms and starts with working, but otherwise not so great code.

When you see how simple the starting code is, I know you’ll be asking “why bother to automate the testing?” and in general, you’d be right. However, when trying to understand how to do something, you should use a simple context so that what you are doing is not obscured by what you are doing it to.

The next instalment will begin the process by adding a basic unit test and doing some very simple re-factoring. Before moving on to the next step, make sure that you can build the code and that you understand it.

Tools

The tools I use for this project are:

  • Intellij IDEA which is the best Java IDE I’ve found. Any IDE should be OK to use but you should really give the IDEA Community Edition a try.
  • Maven, used to manage project dependencies and build.
  • JUnit, the Java unit testing framework.
  • Git for version control, along with github for on-line hosting of the code.

Digression into the sorting algorithm

The algorithm used in this stage of the project is the ripple sort. Also known as the cocktail shaker sort, it ‘s a variation on the bubble sort which works by making bi-directional passes through the data moving the highest and lowest remaining values into place.

The ripple sort is not a particularly efficient sort but it is quite simple which makes it ideal for our purpose.

The initial code

The following code is the starting point for this project. It is severely flawed.

package com.thefifthcontinent;

public class Main {

    public static void main(String[] args) {

        int d[] = {14, 12, 19, 7, 4, 1};
        boolean s;

        System.out.println("Before: ");
        for (int i=0; i<d.length; i++) {
            System.out.print(d[i] + " ");
        }
        System.out.println();

        do {

            s = false;

            for (int i=0; i<d.length -2; i++) {

                if (d[i] > d[i+1]) {
                    int temp = d[i];
                    d[i] = d[i+1];
                    d[i+1] = temp;
                    s = true;
                }

            }

            if (!s) {
                break;
            }

            s = false;

            for (int i=d.length -2; i>-0; i--) {

                if (d[i] > d[i+1]) {
                    int temp = d[i];
                    d[i] = d[i+1];
                    d[i+1] = temp;
                    s = true;
                }

            }

        } while(s);

        System.out.println("After: ");
        for (int i=0; i<d.length; i++) {
            System.out.print(d[i] + " ");
        }
        System.out.println();

    }
}

What’s wrong with this code?

This code meets the first requirement of good software, it compiles, runs and produces the correct output on the happy path. Sadly, I’ve worked in places where that would be production quality code whereas it obviously isn’t. So what’s wrong with it?

  • It’s all in one big method.
  • It’s all in the main method.
  • Code is repeated.
  • Variable names.
  • It’s not easy to modify – what if we want a quick sort instead?
  • It’s not optimised.
  • It’s not properly testable.
  • Immutability? What’s Immutability?

Links

IntelliJ IDEA
Apache Maven
JUnit
Git
Github
Project Repository