Many software developers use coding katas to get better at writing efficient and high-quality code. This post dives into the details of code kata exercises and how they can help enhance your coding.
What is a code kata?
Coding katas are software development exercises that help developers improve their skills through practice and sheer repetition. The goal of a kata is not to pose difficult problems for the developer to solve. Instead, katas aim to help develop successful routines and to build the “muscle memory” necessary to be able to apply these practices in real-life development scenarios.
The term “kata” comes from the area of Japanese martial arts, where it refers to a choreographed pattern of movements that may be practiced either alone or in groups. The term was first used in the context of software development by Dave Thomas, a founding father of Agile and co-author of the book The Pragmatic Programmer.
Why practice katas in programming?
The goal of code katas is to equip the developer with the skills to use certain programming techniques as second nature: without hesitation and executed perfectly, every time. Code katas are all about learning by doing: whether practiced alone or with a partner, they are based on repetition, executing a kata again and again.
Note that there’s no mentoring needed to perform katas – that said, it helps to review your implementation with other team members (or that of other developers on the internet) retrospectively so as to arrive at better solutions and build those into your subsequent kata practice sessions.
Katas are useful every time you need to learn a new programming technique or even a new language. For instance, it’s popular with devs looking to adopt Test-Driven Development (TDD) and Behavior-Driven Development (BDD).
How to exercise a code kata excercise?
Katas generally take about 30-60 minutes to complete but can stretch to be even longer in some cases (and that time obviously also depends on your experience). When performing a kata, you’ll be given a software problem, and will have to come up with your own implementation to solve that problem. Note that not all katas include coding! Some katas focus on the thinking process necessary to solve a problem.
That said, most katas do actually start out with some (often messy) legacy code that works. There will also be some requirements and constraints provided. The kata will have you refactor and extend the code per the requirements, and your job will be to find the best way to do that. Besides those basic elements, a kata is an exercise that gives a good deal of freedom to the developer. Once you understand the problem, you’ll just start working on it your own way.
Performing a kata is meant to be a practice exercise, so make sure you are not bound by deadlines, don’t have any kind of pressure to “get it right”, and have no consequences if your first solution to the problem isn’t the best one out there. The basic idea behind katas is that practice makes perfect, so take your time. And it’s not like once you’ve tackled a kata, you’re done! Instead, you’ll go back to the same kata over and over again, finding the best solution and then practicing using that technique again and again.
A word of advice: make sure you have plenty of uninterrupted “problem solving” time when practicing a kata! Set aside some time and make sure you have a “playground” sandbox environment to practice in where you can’t do any damage. Start with a simple exercise, and move on to more difficult katas as you gain experience.
With TDD katas, the idea is to practice designing tests and running them often to improve code in iterations. In other words, you’re not expected to rewrite all of the code from scratch, but rather to practice the iterative workflow of TDD. This helps you get into the swing of applying the TDD approach in a safe environment before you start using it in production.
The benefits of doing code katas for software developers
Software developers exercise katas to further improve their programming expertise by learning (and building up the muscle memory to put into practice) new techniques and best practices to solve problems. Learning coding techniques this way can remove stress and anxiety and make you a more confident programmer. It can also help you stick to established best practices consistently, each time you use that technique.
Katas help you improve efficiency because you’ll have to spend less time thinking about solutions and how to apply them when confronted with a new programming problem. They are also useful when it comes to experimenting with new frameworks, languages, or language features. A kata may have you use various functions, classes, or libraries. It can have you create entire applications, or improve the design of existing ones – all of that is stuff you may need to do in your real-life software projects.
Overall, besides learning specific techniques, practicing katas also helps you get better at getting productive in a new and unfamiliar environment as fast as possible.
Commonly used katas
You’ll find a vast range of katas you can practice out there. To get started, you’ll find a curated list of katas here and here. In addition, the Kata-Log is a great website that offers a wide range of katas in a variety of topics for both beginners and experienced kata practitioners.
Below, we’re giving brief descriptions of some of the most popular code katas that help you get into the swing of things as you start exercising katas. SPOILER ALERT: Check out the videos below only if you are not planning on doing these katas any time soon but are interested to see them in action!
The Gilded Rose refactoring kata
The Gilded Rose is considered a standard kata for Test-Driven Development that was created by Terry Hughes. In this kata, you start out with some legacy code that is somewhat messy, some text explaining what the code does, and a few requirements and constraints. The point is then to refactor the code in increments without breaking the functionality.
Find the starting code for the Gilded Rose refactoring kata here (in many programming languages).
Conway’s Game of Life refactoring kata
Another refactoring classic is the Game of Life kata presented by Emmanuel Gaillot at XP2005. Conway’s Game of Life is a popular example of a zero-player game where your contribution is configuring the initial state – then, you’ll just observe as the game evolves on its own. This kata has you calculate the next generation in the Game of Life from any starting position.
Find the code for the Game of Life kata over at Emily Bache’s GitHub.
FizzBuzz
Another classic: FizzBuzz is a popular code kata in which you’ll write code to print numbers from 1 to 100. However, there are two more requirements: if the number can be divided by 3, the program should print “Fizz”, and if it can be divided by 5, the program should print “Buzz”. If both conditions apply (e.g. the number is divisible by both 3 and 5), “FizzBuzz” should be printed. Use this kata to practice writing simple unit tests for basic functionality – a good way to get started with unit testing in general.
Find the code for the FizzBuzz kata here.
Mars Rover
If you’re ready to step up your kata game, the Mars Rover involves another level of complexity and is best used to practice object-oriented design approaches. This kata has you write a program to control a Mars rover based on a set of commands. Use this kata to practice writing unit tests for complex stateful programs.
Find the starting points for Mars Rover in a variety of languages over at the Kata-Log website.
While katas are all about repetition, repeating laborious and no-value-added tasks like setting up the basic structure for your unit tests doesn’t make a ton of sense. That’s why Symflower offers unit test templates that take care of all the necessary imports and boilerplate, enabling you to focus on the actual test scenario. Experience Symflower’s power in action! Try the Gilded Rose or FizzBuzz kata with Symflower’s support to see how much faster you can work, and let us know if you have any feedback!
Make sure you don’t miss any of our insightful posts by signing up for Symflower’s newsletter and by following us on Twitter, LinkedIn or Facebook!