Learn to Kata and Kata to Learn

Donald Raab
Nov 28 · 3 min read

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.

97 Things

Tap into the wisdom of experts to learn what every great practitioner should know, no matter what technology or techniques you use. With the 97 short and extremely useful tips, you’ll expand your skills by adopting new approaches while learning best practices.

Donald Raab

Written by

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

97 Things

97 Things

Tap into the wisdom of experts to learn what every great practitioner should know, no matter what technology or techniques you use. With the 97 short and extremely useful tips, you’ll expand your skills by adopting new approaches while learning best practices.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade