Skip to main content
blog title image

3 minute read - Programming Counterstrings

Testing Related Code Katas

Mar 4, 2019

What would be suitable Code Katas for people wanting to learn how to code to support their testing?

Code Katas are a pretty popular concept for programming:

I don’t do Code Katas. John Somnez writes about why he doesn’t do code katas here and here is another take on Code Katas from Micah Martin

Why I don’t do Code Katas

  • Time
    • I don’t really have time to dedicate to code Katas
  • Usefulness
    • When I’m learning a language I really want the code to be useful, because that is what motivates me to learn more.
  • I don’t want to keep doing the same thing

But I realised recently that I might well do Code Katas, but I haven’t called them that.

My Code Katas

I have written CounterString implementations in:

I have approached the writing of CounterString differently each time I created a new implementation.

And I have revisited my implementation as I learned more about the programming language.

An Exercise

If you want to use CounterStrings as a way to practice your programming, and potentially consider it a Code Kata.

Here are my suggestions.

  1. write code to generate a String starting with *, and then 3*, etc. to the length of your CounterString, e.g this would create a ten character CounterString like *3*5*7*9*1. I describe this as Forward CounterString Generation
  2. write code to generate a CounterString that matches how JamesBach describes it where the string ends with * and is of the required length and the number before each * is the position of the * in the string, so a 10 character CounterString would read *3*5*7*10*
  3. instead of writing out your CounterString to the console, write it to a file
  4. instead of writing out your CounterString to the console, write it to the clipboard
  5. instead of writing out your CounterString to the clipboard, make it type it into a field
  6. find a different implementation approach e.g. if you used recursion change it to do something else, if you didn’t use recursion try that, if you were reversing strings try doing it without reversing strings
  7. create automated scripts that execute your implementation and compare the output with a different implementation e.g. James Bach’s perl clip this might initially be a pre-generated file or responses, but create one that calls PerlClip dynamically
  8. for the ‘ultimate’ challenge, try writing an implementation that generates CounterStrings the way James Bach describes it, but does it dynamically so that you could stream the generation. I describe this as Predictive Forward CounterString Generation

With each of these you will create something useful, that supports you in your work. And you’ll learn something.

Other Suggestions

Other code that I seem to keep writing:

  • create a verb/noun text adventure game
  • write a link checker
  • write a calculator that takes a string e.g. “2+4+7” and returns the result
  • write a test data generator using generative grammar

All of these can vary in complexity but provide useful learning tools. You’ll find examples of all of the above in my Github repos (but you might have to search a bit).

How do you practice your programming and what exercises do you use to learn new languages?

Free Video Inside

Slides