Today I decided to start my own code kata and hopefully I can be consistent enough to keep it going.
To give a little context, code kata is way for programmer to practice their programming skill. Sometimes the same problem is done multiple times and the purpose is to find a better approach to solve the problem.
This is also a good way to learn new technology, programming language, or even new methodology.
This time I’m going to start slow by solving the Word Count problem and I’m going to write it in python.
Imagine that I have this sentences:
Hello world! Today I’m going to learn new skill and that is programming.
I don’t know whether I’m going to make it or not, but I’ll try as hard as possible.
What I want to do is put the sentences into a program and the program will produce this output:
I saved it into a folder called tests. And later, the folder structure would be like this:
Next, I ran the test file with nose. The full command to run the test is shown by this command:
This was the result that I got:
As I expected, the test failed and it was the time to write the production code. Here’s the code that I wrote:
I saved the file and named it with word_count.py. Hence, the folder structure was modified to be like this:
Again, I ran the test to ensure what I did was correct according to the test. Here’s the result of the test:
Yeay! It passed!
Wait hold on! The job is not over yet. One task in 1 TDD cycle is to do refactoring to the code. So, I looked back to the code and thought about possible way to refactor the code.
I found it and then I made some changes to the code, so it looked like this:
Apparently the changes reduced a significant number of line of code. The counter logic has been handled by Counter that I imported from collections.
Looking at the tests, apparently there was one more test case that I should add to the test file. One thing that I realised was I hadn’t handled punctuations.
That will lead to a case where hello is different with hello!, but it shouldn’t. So, here’s the additional test case that I added to the test file.
When I ran the test, I got this message:
Awesome! That was expected. So I proceeded to the production code and changed it. This is the result of the changes.
I just added a regex search to replace any unwanted punctuations and also stripped all the trailing whitespaces.
When I re-ran the test, the result was like this.
OK! Is it done? At that time, I didn’t think it was done. Remember, I should spend some time to look back at the code and see for any possibility of refactoring.
My thought about this was to give a clear job description that this class suppose to have. In general, this class should have these tasks:
Clean up the sentences
Count the words
Therefore, I thought I should write the code better so the task clean up the sentences can be expressed more obvious.
Here’s the test case that I added to the test file to fulfill my design.
As usual, I ran the test first and got 1 failed test which is the test that I just added.
This is the production code looked alike after I made some changes to fulfill the test case.
This time when I re-ran the test I got 3 tests passed!
And that was it, the WordCount class that I should have to fulfill my requirement.
Next, I just needed to add some codes to print out the result to the console, and this is the additional code.
When I ran the whole code, here’s what I got:
Setting up tests
It is easy to use nose because the setup is very simple and the guidelines in its website is very clear and helpful.
So, if you need to have tests in python, I highly suggest this tool.
When I did this code kata, that was the first time I knew about Counter class.
All the logic that I implemented was easily replaced with this class. Very handy!
Wrap it up
Apparently, it’s fun to have a code kata like this. I know this is way too easy for most people, but I do think this is a very good case to try for the first code kata.
I will certainly do this again in the future with various problem complexity. Hopefully, you guys too!