Code Kata == Practice (to me at least)

I have been reading some about Code Katas and what I take from it is that to be a professional and great programmer, I have to practice.   This seems pretty obvious, but I think I take things for granted and get comfortable with the norm and the way things are going.  This really struck me tonight.  I asked myself if I really understood Object Oriented Design principles, and the answer was I still have a lot to learn.   As Uncle Bob, of Object Mentor, has said, “All too often today’s programmers are unaware of the principles that are the foundation of the disciplines that their languages were derived around.”  I find myself in this category, so I decided to practice.

I have been doing a lot of reading (Clean Code, Head First Design Patterns) to increase my knowledge and skills, but after my realization tonight, I decided to practice more, to really implement the concepts and improve my skills by actually doing.

First, I created an imaginary scenario in which a customer has asked me to create a program that finds word duplicates in a string.  A simple and seemingly pointless program; The point, however, is to practice, and simple seems a worthy place to begin.  Basically, when this program is run, the user supplies a string and an array of substrings in the provided string that were used more than once is returned.

Wanting to practice Test Driven Design (TDD), I started with writing a test.  First, I thought of the requirement (see above) and then wrote a sentence stating the test:

Given the String “Mary had a little lamb.” the program should return an empty array.

Seems logical enough to me, and it seems to meet the requirements for the program.  Here’s the test:

package stever.chad.kata.duplicatefinder;

import org.junit.Test;

import junit.framework.TestCase;

public class DuplicateFinderTest extends TestCase {

	@Test
	public void testNoDuplicates() {
		String seed = "Mary had a little lamb.";
		int expectedNumberOfDuplicates = 0;
		DuplicateFinder finder = new DuplicateFinder();
		String[] duplicates = finder.findDuplicates(seed);
		assertEquals(expectedNumberOfDuplicates, duplicates.length);
	}
}

After I had my test, I wrote the implementation of DuplicateFinder to satisfy the compiler.

package stever.chad.kata.duplicatefinder;

import java.util.*;

public class DuplicateFinder {

	public String[] findDuplicates(String seed) {
            return null;
	}
}

Now it was time to get the test to pass; in fact running this test would result in a NullPointerException being thrown, because finder.findDuplicates(seed) returns a null object; so making this test pass and not throw any exceptions seems easy enough: just return an empty String array.

public String[] findDuplicates(String seed) {
		return new String[0];
}

Now the test passes. Great! But what if I actually have a string with duplicates? Enter the next test:

Given the String “Mary had a little lamb named Mary.” the program should return an array with one value and that value should be “Mary”.

Here’s the test in Java:

@Test
public void testOneDuplicate() {
	String seed = "Mary had a little lamb named Mary.";
	String[] expectedDuplicates = { "Mary" };
	int expectedNumberOfDuplicates = expectedDuplicates.length;
	DuplicateFinder finder = new DuplicateFinder();
	String[] duplicates = finder.findDuplicates(seed);
	assertEquals(expectedNumberOfDuplicates, duplicates.length);
	assertEquals(expectedDuplicates[0], duplicates[0]);
}

Now I needed a way to actually look at the words in the supplied string and compare them. Having recently read about the Single Responsibility Principle [SRP] and keeping my functions small (Clean Code p.34), I thought I should create another class that would have the responsibility to extract the words from the supplied string: WordFinder.  More on that at a later time.

Remember, the point of this exercise is just that: to exercise; To “work out”  my skills.  To practice.  I obviously don’t claim to be perfect or that my technique is correct or that I am using the most efficient methods.  That’s why I am practicing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s