January 6, 2010 16

String Calculator – Python

By Corey Haines in Katas, Software Craftsmanship

Continuing our series of ‘Same Kata, Different Languages,’ we have a very special performance in Python by Gary Bernhardt:

Gary Bernhardt is an independent software contractor in Seattle specializing in the dynamic languages Python and Ruby. A brutal minimalist, he is bent on the destruction of all existing software except Emacs and Vim, the two One True Editors.

Gary’s a fantastic developer with some amazing thoughts on software development. Here’s what he has to say about doing this kata:

I’ve made dozens of runs through this kata, but I chose this one to post even though it has two accidental red bars. They’re nice red bars, though, because they show you just how fast test feedback can be. At one point, I run the tests and they fail unexpectedly. I see the bug, fix it, and get a green bar about two seconds after I fired off the original failing test run. That’s run-fail-analyze-fix-run-pass in two seconds. I think that we should strive for this fluidity in all of our software development and I love doing what I can to help others achieve it.
There’s one section of the kata that I’d like to highlight. Around 3:30, when I begin to make the code reject negative numbers, I come to a point where it says “if there’s a minus sign at all, throw an exception.” Now I’m stuck – I can’t make that check more specific by talking about negative rejection. Instead, I write a test on the other side of the problem – I check for the case when the minus sign is actually a delimiter. The test fails, but the easiest way to fix it is a refactor from the code’s previous state. So, I comment the failing test out, do the refactor, uncomment the test, and now it passes. This is something that I stumbled over while learning TDD – I’d try to plow ahead and fix the failing test case directly, which can be a slow, frustrating process.

Knowing Gary, when he says he did this dozens of time, he isn’t lying. Gary breaks from the norm of using classical music, but I think he’s chosen a great soundtrack that goes great with his style.

If you’d like to contact Gary:

blog: http://blog.extracheese.org/

twitter: http://www.twitter.com/garybernhardt

Feel free to comment. If you are inspired to do your own kata, please let me know. Watch and enjoy,  you are about to see an amazing performance.

String Calculator Kata In Python from Gary Bernhardt on Vimeo.

16 Responses to “String Calculator – Python”

  1. Steve Howell says:

    Who’s the dude with the beard at the end of the screencast?

    Nice work, Gary!

  2. Josh Walsh says:

    Nice job Gary. A bit speedy for me to follow at times, but I know it’s way slower than you normally work. :)

  3. Dale says:

    So fast, so very fast! I hit pause at one point just to catch up on the refactoring in my head (and slow my heart rate). I’ve never even looked at Python before, but the code is very readable.

  4. Steve, that’s John McCarthy, inventor of Lisp (but I see on Twitter that you already figured that out. :)

    Josh and Dale, I’m sorry that it was still too fast. :( I slowed it down based on Corey’s feedback, but I guess it wasn’t enough. Editing speed is important to me and I’ve gone so fast for so long that I have a hard time turning it off.

    It’s much shorter than most katas, so you could always watch it a couple times to pick up the pieces you miss. :)

  5. This is really, really good. Watched it twice so far, will watch it more I’m sure.

  6. Al Tenhundfeld says:

    Bravo!

  7. James Martin says:

    Wow. That was incredibly fast. I had to watch it three times and pause a lot to keep up with the refactoring. Great to watch; a real performance!

    Could you give me a list of the vim plugins you were using?

  8. Jan Ernsting says:

    Nice cast :)

    I’d like to see the list of vim plugins, too ;)

  9. Martin says:

    Super cool. Only downside is that the problem is so easily solved in Python :-) . Could you describe your setup? it is obviously Vim, but what else?

  10. Yoshi Carroll says:

    The normalizing delimiters part was a great idea.

    And I’d love to see a full speed version. :)

  11. Calvin Bottoms says:

    Beautiful work. +1 on the vim plugins.

  12. I use python.vim for better Python support in Vim, but that might actually not even be needed nowadays. The method extractions were done with Bicycle Repair Man, a Python refactoring tool. I think those are the only actual plugins used in the cast. The red/green test running magic is all custom, and sadly lives directly in my .vimrc. You can find it and all of my other dotfiles on BitBucket, though: http://bitbucket.org/garybernhardt/dotfiles

  13. DocOnDev says:

    Gary – I am always impressed at your speed. I’ll need to look into Bicycle Repair Man. Thanks for a great video.

    I’ve been working this Kata in Python for about two weeks; maybe every other day for an hour or so. I’ve been using it as a way to teach my wife Python. I blogged about the experience and show one version of the code.

    http://docondev.blogspot.com/2010/02/python-string-calculator-kata-step-by.html

  14. This is fabulous, thanks for taking the time to share it.

    I have one dumb question: Where does your test function ‘raises’ come from? I don’t believe it’s a builtin? Unless it’s new in 3.x and my Google-fu is sorely lacking. Is it imported somewhere that we can’t see?

  15. Scott Radden says:

    Man this guy codes fast.
    I’m a C# guy by really enjoyed this kata

  16. Jonathan,

    The “raises” function was part of Mote, the test runner I was using. I wrote it and it’s very experimental – in fact, the current version doesn’t even have the “raises” function any more. :)

Leave a Reply