Quick Index
Problem Statement


For this problem we're going to take our latest test code and simplify it using helper methods.

We'll use the three-step process.

Starting Point


The example "starter code" can be found here

Simplify - Round 1


Identify Redudant Code (Round 1)


Here is a subset of our test code.

Can we identify any redudant code.

We sure can. The call "System.out.println" is repeated many-many times.
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		System.out.println("\n-- testSetDimensions --");
		System.out.println("Set dimensions to 8 and 7");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		System.out.println("\n-- testGrowByWithOneParam --");
		System.out.println("Sent growBy with 5 (after setting dims to 100)");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		System.out.println("\n-- testGrowByWithTwoParams --");
		System.out.println("Sent growBy with (3, 2) after setting dims to 100");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}



Code Helper Method (Round 1)


The simplest helper method simply wraps up the functionality that was redundant.

Because we know that "System.out.println" was the culprit, we simply wrap it in a helper method and call it "prn" (for print).

We could name it whatever we like. We could make the method private if we wanted to.
	public void prn(Object o)  {
		System.out.println(o);
	}



Employ (Call) Helper Method (Round 1)


In this case we can simply search & replace the verbose 'System.out.println' with the concise 'prn'.

We've lightened up the code a bit.
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		prn("\n-- testSetDimensions --");
		prn("Set dimensions to 8 and 7");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		prn("\n-- testGrowByWithOneParam --");
		prn("Sent growBy with 5 (after setting dimensions to 100)");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		prn("\n-- testGrowByWithTwoParams --");
		prn("Sent growBy with 3 and 2 (after setting dimensions to 100)");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}


Simplify - Round 2


This is addicting. Let's do another.

Identify Redudant Code (Round 2)


We note that every method ends with four "prn" statements.

Can we combine these into one line of code that calls a new helper method?

We sure can give it a shot.
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		prn("\n-- testSetDimensions --");
		prn("Set dimensions to 8 and 7");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		prn("\n-- testGrowByWithOneParam --");
		prn("Sent growBy with 5 (after setting dimensions to 100)");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		prn("\n-- testGrowByWithTwoParams --");
		prn("Sent growBy with 3 and 2 (after setting dimensions to 100)");
		prn("Actual Width: " + rec.getWidth());
		prn("Actual Height: " + rec.getHeight());
	}

Code Helper Method (Round 2)


This one takes a little more creativity.

We analyze the group of redudant lines, and note that in each case we are really "summarizing" the final tested object.

So we add a helper method named "summarize" that will print out such a summary for us.
	public void summarize(Object testee, String testTitle, String note)  {
		//testee is the object being tested
		prn("\nCompleted Test: " + testTitle);
		prn(note);
		prn("Tested Object: " + testee);
	}

Employ (Call) Helper Method (Round 2)


In this case we can not just do a search & replace.

We need to take a little more time to code up the calls to the new helper method, but the reward (simpler code) is worth it.
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		summarize(rec,
			"testSetDimensions",
			"Set dimensions to 8 and 7");
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		summarize(rec,
			"testGrowByWithOneParam",
			"Sent growBy with 5 (after setting dims to 100)");
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		summarize(rec,
			"testGrowByWithTwoParams",
			"Sent growBy with 3 and 2 (after setting dims to 100)");
	}

Original Vs Simplified


Our code is significantly lighter.

Left: Original Code
Right: Simplified Code
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		System.out.println("\n-- testSetDimensions --");
		System.out.println("Set dimensions to 8 and 7");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		System.out.println("\n-- testGrowByWithOneParam --");
		System.out.println("Sent growBy with 5 (after setting dims to 100)");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		System.out.println("\n-- testGrowByWithTwoParams --");
		System.out.println("Sent growBy with (3, 2) after setting dims to 100");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		summarize(rec,
			"testSetDimensions",
			"Set dimensions to 8 and 7");
	}

	public void testGrowByWithOneParam()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		summarize(rec,
			"testGrowByWithOneParam",
			"Sent growBy with 5 (after setting dims to 100)");
	}

	public void testGrowByWithTwoParams()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		summarize(rec,
			"testGrowByWithTwoParams",
			"Sent growBy with 3 and 2 (after setting dims to 100)");
	}

The completed code can be downloaded here.

More Ideas


We could keep going. Below are a couple more ideas. Mostly this exercise is so that we we keep our minds open for these "helper methods" and use them liberally in our code to make things simpler!

Construction Helper
We see this construction a lot in the code. We might wrap that in a helper method. Maybe call it "sample" or "testee".
Rectangle rec;
rec = new Rectangle();
Before/After
We might consider a method such as "beforeTest" similar to "summarize" but it might print out a label such as "About to test object" and then info about object.
beforeTest(rec);
//test code
summarize(rec, "testSetDimensions", "Set dimensions to 8 and 7");