Introduction

The idea of coding katas comes from a common practice found in martial arts.

Kata in Japanese means "form" and refers to the solo drills that practitioners perform during their training sessions. Kata gif.

Katas are meant to be done slowly and deliberately, much more slowly than during a real fighting situation. These patterns are honed through practice and repetition so that when a situation arises that the student has to fight, they can instinctually refer back them.

This also frees their mind to focus on other essential things, such as what their opponent is doing.

Martial artists believe that constant practice is required to master a technique and achieve greatness.

The same idea is true for the code katas. We practice specific aspects of programming dozens of times so that we don't have to struggle with it during our actual work.

Normally, a teacher will make the students take their time with a kata until they are comfortable taking on challenges of a similar difficulty level.

Kata's are also a common way of testing candidates' problem-solving skills and ability during an interview process. I've put together my list of essential steps I take in solving them.

  1. Identify the problem
  2. Break it down
  3. Identify variables
  4. Tackle your subproblems
  5. Troubleshooting
  6. Refactor

1. Identify the problem

  • You can't solve a problem you don't understand.
  • Read through the problem at least three times, or however many times you need. You don't want to find out halfway through that you misunderstood a key detail.
  • Be careful not to assume you understand the problem after reading the first few lines, especially if you think it's something you've covered in the past. Once I had to code a simple Tic-tac-toe game but didn't realize my task was actually to code a variant of it, numerical tic-tac-toe.
  • Sometimes, if conditions permit, I'll even try to describe the problem to a friend and see if their understanding matches my own. (It's not as cliché as it sounds! It plays out in the real world when you break down problems with your work colleagues).
  • Personally, I like to summarize some key points as comments

data/admin/2021/3/desc-to-comments.jpg

2. Break it down

  • Do not try to solve the problem as a whole. You will cry.
  • Instead, try to break it down into easier to solve step-by-step subproblems.
  • I comment these steps into my code:
public class TopWords
{
    public static List Top3(string s)
    {
       // Step 1 trim punctuation from s
    
        // Step 2 - iterate string and put into a dictionary, 
        // Key = word, Value = count 
        //  if (check if NullOrWhiteSpace) {
        //    add to new dictionary
        //    }
        //      else
        //        increase count
        //  }
      
        // Step 3 - Order new dictionary by value by descending   
      
        // Step 4 - Take top 3
      
        // Step 5 return result
      }
}
  • You might prefer to write these steps down on a piece of paper/word doc.

3. Identify variables

Think about the data and then consider -

  • If it's a collection, do you need an array, dictionary, list?
  • Give the variables meaningful names and place them at the start of your function.
public class TopWords
{
    public static List Top3(string s)
    {   
        Dictionary frequencies = new Dictionary();
      
        var punctuation = s.Where(Char.IsPunctuation).Distinct().ToArray();
        var words = s.Split(new char[] {' ',',', ';', '/', ':', '?', '.', '_','-','!' }).Select(x => x.Trim(punctuation)).ToList();
        words = words.ConvertAll(d => d.ToLower());
}
  • Don't spend too long on this. The purpose of this step is to help promote some ideas ahead of tackling the actual problem. You'll likely come back to change these variables.

4. Tackle your subproblems

  • Treat each subproblem as stand-alone, like functions within a function, and try to achieve each step's desired output before moving forward.
  • If available to you, print each step's output to the console and test your function with the unit tests sometimes provided on the kata platform you're on. You'll then know if your step is working and better set you up for the next step.
public class TopWords
{
    public static List Top3(string s)
    {   
        Dictionary frequencies = new Dictionary();
      
        // Variables
        var punctuation = s.Where(Char.IsPunctuation).Distinct().ToArray();
        
        // Step 1 trim punctuation from s
        var words = s.Split(new char[] {' ',',', ';', '/', ':', '?', '.', '_','-','!' }).Select(x => x.Trim(punctuation)).ToList();
        words = words.ConvertAll(d => d.ToLower());
        
        // Print result
       foreach (var word in words)
       {
          Console.WriteLine("word");
       } 

        // Step 2 - iterate string and put into a dictionary, 
        // Key = word, Value = count 
        foreach (var word in words) {
          
            if (!String.IsNullOrWhiteSpace(word))
            {
              if (!frequencies.ContainsKey(word))
              { 
                frequencies.Add(word, 0);
                frequencies[word] += 1;
              }
              else
              {
                  frequencies[word] += 1;
              }
             }
           }
           
        // Iterate the dictionary and print result
        foreach (KeyValuePair kvp in frequencies)
        {
          Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        } 
        
        // Step 3
        // TODO         
}
  • If you run into a problem you can't solve, don't spend too much time on it. Move onto the next step and then come back to it - that's the benefit of encapsulating aspects of your problem.

5. Troubleshooting

  • Think carefully about how to word your question and what you need to search on Google.
  • Prepare a list of your favorite resources to fall back on ahead of approaching your kata, such as:
    • Stackoverflow.com
    • Github.com
    • Draw.io – for diagramming
    • trello.com – for managing your task

6. Refactor

  • Once you've completed the kata, you can start to find improvements and optimizations in your code.
  • It's unrealistic that anyone writes final code to solve a problem first time, there will always be some changes that can be made. Make them if you have the luxury of time.