Chapter 51. Learn to Kata and Kata to Learn

Donald Raab

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 that 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.

Here’s 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 the 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 version control system (VCS).

  6. Open source the code to share with others.

Now I’ll demonstrate how to create a small kata by following the first four steps:

  1. Topic: Learn how to join strings in a List.

  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);
    }
       
  3. Refactor the code to use StringJoiner in Java 8. Rerun 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. Rerun the test:

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

    Refactor the code to use String.join. Rerun the test:

    String joined = String.join(", ", names);
  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’ll 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset