Learn to Kata and Kata to Learn

Donald Raab
97 Things
Published in
3 min readNov 28, 2019

Every Java developer needs to learn new skills and keep their existing skills sharp. The Java ecosystem is enormous and continues to evolve. With so much to learn, the prospect of keeping up may seem daunting. We can help each other keep up in this rapidly changing space if we work together as a community, sharing knowledge and practice. Taking, creating, and sharing code katas is one of the ways we can do this.

A code kata is a hands-on programming exercise that helps you hone specific skills through practice. Some code katas will provide you structure to validate a skill has been acquired by getting unit tests to pass. Code katas are a great way for developers to share practice exercises with their future selves and other developers to learn from.

How to create your first code kata:

  1. Select a topic you want to learn.
  2. Write a passing unit test that demonstrates some piece of knowledge.
  3. Refactor the code repeatedly until you are satisfied with a final solution. Make sure the test passes after each refactoring.
  4. Delete the solution in the exercise and leave a failing test.
  5. Commit the failing test with supporting code and build artifacts to a VCS.
  6. Open source the code to share with others.

Now I will follow the first four steps to create a small kata.

Step 1

Topic: Learn how to join strings in a List.

Step 2

Write a passing JUnit test that shows how to join strings in a List.

@Test
public void joinStrings()
{
List<String> names = Arrays.asList("Sally", "Ted", "Mary");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < names.size(); i++)
{
if (i > 0)
{
builder.append(", ");
}
builder.append(names.get(i));
}
String joined = builder.toString();
Assert.assertEquals("Sally, Ted, Mary", joined);
}

Step 3

Refactor the code to use StringJoiner in Java 8. Re-run the test.

StringJoiner joiner = new StringJoiner(", ");
for (String name : names)
{
joiner.add(name);
}
String joined = joiner.toString();

Refactor the code to use Java 8 Streams. Re-run the test.

String joined = names.stream().collect(Collectors.joining(", "));

Refactor the code to use String.join. Re-run the test.

String joined = String.join(", ", names);

Step 4

Delete the solution and leave a failing test with a comment.

@Test
public void joinStrings()
{
List<String> names = Arrays.asList("Sally", "Ted", "Mary");
// Join the names and separate them by ", "
String joined = null;
Assert.assertEquals("Sally, Ted, Mary", joined);
}

Pay it forward — I will leave steps 5 and 6 as an exercise for the reader.

This example should be simple enough to illustrate how to create your own katas of varying complexity, leveraging unit tests to provide the structure necessary to build confidence and understanding.

Value your own learning and knowledge. When you learn something useful, write it down. Saving practice exercises to recall how things work can be quite helpful. Capture your knowledge and exploration in code katas. Katas you have used to sharpen your own skills may also be valuable to others.

We all have things to learn and that we can teach. When we share what we learn with others, we improve the whole Java community. This is vitally important to helping ourselves and our fellow Java developers collectively improve our coding skills.

--

--

Donald Raab
97 Things

Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse/eclipse-collections). Inspired by Smalltalk. Opinions are my own.