Main | Kata Twenty One: Simple Lists »

January 28, 2007

Code Kata

Background

How do you get to be a great musician? It helps to know the theory, and to understand the mechanics of your instrument. It helps to have talent. But ultimately, greatness comes from practicing; applying the theory over and over again, using feedback to get better every time.

How do you get to be an All-Star sports person? Obviously fitness and talent help. But the great athletes spend hours and hours every day, practicing.

But in the software industry we take developers trained in the theory and throw them straight in to the deep-end, working on a project. It’s like taking a group of fit kids and telling them that they have four quarters to beat the Redskins (hey, we manage by objectives, right?). In software we do our practicing on the job, and that’s why we make mistakes on the job. We need to find ways of splitting the practice from the profession. We need practice sessions.

CodeKata:
A description of how this all started
MoreKata:
Sometimes ‘kata’ isn’t quite the right word; karate uses other techniques to teach too.

The Kata

What makes a good practice session? You need time without interruptions, and a simple thing you want to try. You need to try it as many times as it takes, and be comfortable making mistakes. You need to look for feedback each time so you can work to improve. There needs to be no pressure: this is why it is hard to practice in a project environment. it helps to keep it fun: make small steps forward when you can. Finally, you’ll recognize a good practice session because you’ll came out of it knowing more than when you went in.

Code Kata is an attempt to bring this element of practice to software development. A kata is an exercise in karate where you repeat a form many, many times, making little improvements in each. The intent behind code kata is similar. Each is a short exercise (perhaps 30 minutes to an hour long). Some involve programming, and can be coded in many different ways. Some are open ended, and involve thinking about the issues behind programming. These are unlikely to have a single correct answer. I add a new kata every week or so. Invest some time in your craft and try them.

If you want to discuss kata, there’s a mailing list here, and a wiki here. However, remember that the point of the kata is not arriving at a correct answer. The point is the stuff you learn along the way.

KataOne: Supermarket pricing. Pricing looks easy, but scratch the surface and there are some interesting issues to consider.
KataTwo: Karate Chop. A binary chop algorithm is fairly boring. Until you have to implement it using five totally different techniques.
KataThree: How Big, How Fast? Quick estimation is invaluable when it comes to making design and implementation decisions. Here are some questions to make you turn over the envelope.
KataFour: Data Munging. Implement two simple data extraction routines, and see how much they have in common.
KataFive: Bloom Filters. Implement a simple hash-based lookup mechanism and explore its characteristics.
KataSix: Anagrams. Find all the anagram combinations in a dictionary.
KataSeven: Reviewing. What does our code look like through critical eyes, and how can we make our eyes more critical?
KataEight: Objectives. What effects do our objectives have on the way we write code?
KataNine: Checkout. Back to the supermarket. This week, we’ll implement the code for a checkout system that handles pricing schemes such as "apples cost 50 cents, three apples cost $1.30."
KataTen: Hash vs. Class. Is it always correct to use (for example) classes and objects to structure complex business objects, or couple simpler structures (hash as Hashes) do the job?
KataEleven: Sorting it Out. Just because we need to sort something doesn’t necessarily mean we need to use a conventional sorting algorithm.
KataTwelve: Best Sellers. Consider the implementation of a top-ten best sellers list for a high volume web store.
KataThirteen: Counting Lines. Counting lines of code in Java source is not quite as simple as it seems.
KataFourteen: Trigrams. Generating text using trigram analysis lets us experiment with different heuristics.
KataFifteen: Playing with bits. A diversion to discover the pattern in some bit sequences.
KataSixteen: Business Rules. How can you tame a wild (and changing) set of business rules?
KataSeventeen: More Business Rules. The rules that specify the overall processing of an order can be complex too, particularly as they often involve waiting around for things to happen.
KataEighteen: Dependencies. Let’s write some code that calculates how dependencies propagate between things such as classes in a program.
KataNineteen: Word chains. Write a program that solves word chain puzzles (cat -> cot -> dot -> dog).
KataTwenty: Klondike. Experiment with various heuristics for playing the game Klondike.
KataTwentyOne: Simple Lists. Play with different implementations of a simple list.

There are places (apart from the comments in this blog) where you can discuss Code Kata.

The first is a YahooGroups mailing list, the second an index page on the PragProg wiki.

I have to admit that I’m nervous doing this. My hope is that folks will work on the kata for a while before discussing them; much of the benefit comes from the little "a-ha!" moments along the way. So, it’ll be interesting to see how (and if) the discussion develops.

Posted at 09:03 AM | Permalink

TrackBack

TrackBack URL for this entry:
www.typepad.com/services/trackback/6a00d83451c41c69e200d834de8bcd53ef

Listed below are links to weblogs that reference Code Kata:

» Cold Hashes from Pawprints of the Mind
So I've been reading about practicing programming, and in particular, Dave Thomas' Code Kata. In Kata Ten, he asks about a design question: hashes versus classes. Can hashes and classes be interchanged? Do you really want to? There's even a specific co [Read More]

Tracked on July 13, 2007 at 08:05 PM

Comments

spacer

I'm a Mac nerd and a Unix sysadmin by profession and there's a couple of things I'd like to say here.

Firstly, pretty much every command I have seen in the cheatsheets I looked at will work on a Mac. Mac OS X has a very pretty GUI but the power and precision is there hiding underneath as soon as you need it.

Secondly, you don't need reams of cheatsheets to perform simple tasks. The tasks that most of the people I work with use would fit on half a sheet of paper and you wouldn't have to use the sheet after an hour's usage. Complex tasks, on the other hand, are a different matter. I was trying to think of something you can't do in the GUI and, thanks to spotlight, there really isn't very much left.

The two things I came up with highlight the main two reasons for using the command line: precision and power.

With spotlight, I could find references to a particular image in all the html files in my website and open them up for editing. With BBEdit, I could do the same thing but automatically change the image name to its new name. But with a command line I can find all references to this image that are not already wrapped in a link and change both the image to be its thumbnail version and the link to point to the full-sized version or I can add a new link all in one command.

The second is a task that I had to perform last week and would have taken me all week in a GUI. Someone (or a piece of malfunctioning software) had deleted a few of the original sized images from a gallery but left the mid-sized images, the thumbnails and all the references to the missing files in place. I wrote a command that would print out a list of all the missing filenames and I can't think of a way of doing that in a GUI that would be quicker than several hours.

The greatest power of the command line is in chaining commands together. Practically every day I end up analysing an Apache log file, looking for some pattern that could explain what is happening. Sometimes I want to find the IP addressses with the most requests (cut -d' ' -f1 access_log | sort | uniq -c | sort -n) or I want to find all of the pages that returned 304 response codes (awk '$9 ~ /304/ {print $7}' access_log | sort | uniq) or I want to add up all of the bandwidth used by various different pages and list the top twenty (for PAGE in `cut -d' ' -f7 access_log | sort | uniq`; do awk -v PAGE=$PAGE 'BEGIN {SUM=0} $7 ~ /PAGE/ {SUM=SUM $10} END {PRINT SUM,PAGE}'; done | sort -n | head -20)

Posted by: ToLGaDrifT | January 18, 2011 at 02:14 AM

spacer

Hi!

Thanks for the excersises!

You compare programmers to musicians and that makes me think about Hanon excersises. I think that coding Katas are pretty similar to these.

The Hanon excersises are a set of 60 excersises for the piano to train some difficult fingering techniques. They are to be repeated daily to retain technique. They were created by Charles-Louis Hanon.

Hanon excersises have received quite a lot of criticism. It includes "doing drills repeatedly will make your attitude towards music mechanical", "it's just boring", "even when you've mastered all 60 you still haven't performed one actual piece of music" etc.

Now I'm not saying that's automatically true for code Katas but it would be interesting to hear your comments on some questions that come to mind:

Why is it important to do these drills? What skills do they add to your repertoar? After doing all your Katas, will you be able to write good programs? Is coding Katas anything like Hanon excersises do you think?

You say that musicians repeatedly practice but you should now that successful musicians also take great care practicing the right things. There is no end to the amount of time you can spend practicing things mindlessly. More on that in this book: www.pianofundamentals.com/ which has some interesting ideas about how to practice the piano. Wikipedia has an article about the Hanon excersises: en.wikipedia.org/wiki/The_Virtuoso_Pianist_in_60_Exercises

I very much like to hear you elaborate on the Katas and how to use them properly, I find the subject very interesting.

Posted by: Erik Nilsson | December 29, 2010 at 02:48 PM

spacer

I am knowing kata now~thanks!

Posted by: Chen Shuiyang | December 24, 2010 at 02:47 AM

spacer

Thanks

Posted by: Tekno_Loji | December 19, 2010 at 04:12 AM

spacer

Thnks;
This should be a training for programmers...

Posted by: travesti | August 26, 2010 at 11:51 AM

spacer

Todd: Thanks! Fixed.

Posted by: Dave Thomas | December 18, 2009 at 09:07 AM

spacer

You're missing the word "from" in the fourth sentence: "But ultimately, greatness comes [from] practicing;"

I realize the post is now nearly three years old, but it's never too late to improve quality, right? =)

Posted by: Todd Fiske | December 17, 2009 at 01:11 PM

spacer

Been a programmer (in one form or another) since days with TI/40A and Apple IIe. But the Prag folks and, now, your blog is helping to revive the joy in this craft. Thanks!

Posted by: ajmCafe | November 23, 2009 at 09:15 AM

spacer

I agree with Ivan Dolvich and I checked out the PragProg wiki for help about discussed answers but I found nothing. Could you please point me in the right direction so I can extend to beyond the basics and learn from the honored sensei's of this discipline.

Thanks.

Posted by: L Pedersen | October 26, 2009 at 09:03 AM

spacer

If you want the ultimate code kata then do the exercises in SICP ... all of them 'nuff said.

Posted by: MMA | July 15, 2009 at 04:08 AM

spacer

Hey man, thanks for the katas, it's a really good idea! Congratulations!

Posted by: Wiliam | September 07, 2008 at 08:33 AM

spacer

i love karatedo. Phil.

Posted by: jiejie | January 14, 2008 at 07:20 PM

spacer

i love karatedo. Phil.

Posted by: jiejie | January 14, 2008 at 07:19 PM

spacer

Dave, I've been looking for exercises like these for a long time, and am happy to find your site. But why are there no answers like on RubyQuiz? I think one can benefit very much from learning from the others. Without other solutions to compare there are several disadvantages:

* solving problems "alone" is no fun
* you don't know how good your solution is until you compare it
* you don't get different ideas

This should be a training for programmers. When I were a white belt in BJJ, some fellow blue belts showed me so many things. I think this would add much value to this site.

Posted by: Ivan Dolvich | January 06, 2008 at 07:38 AM

spacer

haha, i forgot exam rule #1 - always read to the end of the question before starting to answer.

Posted by: Max Williams | June 27, 2007 at 05:08 AM

spacer

Max:

Have a look at the last three paragraphs of this blog entry....

Posted by: Dave Thomas | June 26, 2007 at 09:17 AM

spacer

Hi Dave, thanks for the kata site and the great books.

It feels like there should be a forum for sharing, comparison and discussion of solutions to these and other kata. Do you know of such a site? (my simple google didn't throw anything useful up)

Posted by: Max Williams | June 26, 2007 at 09:01 AM

spacer

I have to disagree with the first part of your sentence: the Kata are most definitely not taken from another source, except where credited. If there is overlap, it is a coincidence—some problems are fairly universal.

I agree with the second part, though—Programming Pearls and it's successor are indeed good books.

Posted by: Dave Thomas | April 09, 2007 at 07:51 AM

spacer

Most if not all of the katas are from the excellent book
"Programming Pearls" by John Bentley
www.cs.bell-labs.com/cm/cs/pearls/

Posted by: Dimitar Dimitrov | April 09, 2007 at 05:39 AM

spacer

Thanks--got it fixed.

Posted by: Dave Thomas | January 30, 2007 at 10:49 PM

spacer

Your link to MoreKata (pragdave.blogs.pragprog.com/Practices/MoreKata.rdoc) throws a 404. And thanks for the resource.

Posted by: George | January 30, 2007 at 02:02 PM

spacer

Thanks for pointing that out. It's there now.

Posted by: Dave Thomas | January 30, 2007 at 08:05 AM

spacer

Hi,

It seems that "15. Playing With Bits" was not transferred across. When you click on the link it returns page not found and the navigation from kata to kata skips over it as well (14 to 16).

Thanks.

James....

Posted by: James | January 30, 2007 at 03:51 AM

The comments to this entry are closed.

Subscribe to this blog's feed
spacer

Other links

  • My Blog
    Dave's blog, which stuff on Ruby, Rails, general programming, careers, and other stuff.
  • The Pragmatic Programmers
    Where it all started! Have a look at the books we're publishing on all sorts of programming topics. If you liked the kata, you might also like The Best of Ruby Quiz.

Archives

  • January 2007

The Kata

  • 1. Supermarket Pricing
  • 2. Karate Chop
  • 3. How Big, How Fast?
  • 4. Data Munging
  • 5. Bloom Filters
  • 6. Anagrams
  • 7. How'd I Do?
  • 8. Conflicting Objectives
  • 9. Back to the CheckOut
  • 10. Hashes vs. Classes
  • 11. Sorting it Out
  • 12. Best Sellers
  • 13, Counting Code Lines
  • 14. Tom Swift Under Milk Wood
  • 15. Playing With Bits
  • 16. Business Rules
  • 17. More Business Processing
  • 18. Transitive Dependencies
  • 19. Word Chains
  • 20. Klondike
  • 21. Simple Lists
Lijit Search
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.