Code Katas
A code kata is a small programming challenge aimed at improving your programming and problem solving skills. The idea is you regularly spend 15 to 30 minutes solving a small problem.
There has been some debate over whether these are actually useful, especially for more experienced developers. My view is that if you approach them correctly, then they can be a valuable method of improving both your knowledge of a programming language and your problem solving skills.
Generally, the argument against katas is that doing the same thing over and over again will not really improve you as a developer, and I mostly agree with that. This is why I think it is important to be smart about how you approach katas and to make sure you keep mixing things up. The rest of this article covers the approaches I take to try and get the most out of katas.
Test Driven Development
Explaining the ins and outs of TDD is outside the scope of this article, but the general idea is that you should never write any code without first writing a test. TDD in PHP is covered in more detail in this Laracasts episode or in this TutsPlus article.
TDD forces you to consider the design of the code you’re going to write before you write it. You’ll also know whether changes to your code breaks anything as you continue on with the kata.
Generally I use PHPUnit for TDD, but I often find PHPSpec is a lot of fun for the more complex katas.
Vary your challenges
Doing the same kata over and over again probably won’t challenge you as much as if you were to mix up the programming puzzles you do. There are lots of places to find new challenges (see the end of the article for a list). That doesn’t mean you should never do a kata twice, you should just make sure you space them out.
Follow an iterative process
Some katas like this one from Roy Osherove is purposely split into steps. You should always start with the simplest solution to the first step and gradually incorporate more of the solution into your application.
As you add each new requirement, think about which parts of your code cause you problems as you add the new features. Could you have written the code a different way initially to mitigate these issues? This sort of knowledge and experience could well help you in larger projects down the line.
Some katas (such as the bowling game kata) might not be purposely split into sections, but it is easy to start with the simplest score and gradually add to your program.
Refactor the code
Don’t just stop when all of your tests are passing. Read through your code, could you improve it? See if you can change the code to make it:
- Faster
- Shorter (less lines of code)
- More readable
Challenge yourself
After you’ve completed the kata once, rather than doing the exact same thing again, could you:
- Do it without using conditionals?
- Do it without using loops?
- If there’s a function you dont often use (the array functions such as
array_map
,array_walk
andarray_filter
are normally good candidates), can you rewrite your solution to use them?
Try it in a new language
If you’ve been trying out a new language, katas are a good way to get into writing slightly more complex code. Plus you get to try out the testing frameworks for your new languge.
Generally the slight changes in native functionality between languages will force you to approach the problem in a different way.
Finding code katas
- String calculator kata - A nice simple kata to get you started.
- Laracasts Kata Series - Work along with Jeffrey Way to solve many of the most popular katas with PHPSpec.
- Advent of Code - Launched just before Christmas, this is a number of coding challenges which must be completed in order.
- Reddit Daily Programmer - Daily programming challenges and solutions.
- Brett Schuchert Katas - A few more challenging katas.
- CodeKata - Lots of information on katas and 21 to try.
- Code Wars - CodeWars allows you to sign up and complete coding challenges to earn points. They only support a few languages, but it is easy to rewrite the tests in another language if you want to complete it locally.
- Coding Dojo - A collection of coding challenges.