October 15, 2009 16

Number to LCD

By Corey Haines in Katas

My inaugural code kata is the venerable Ruby Quiz #14: LCD Numbers. This is a fun exercise, as it exists it two distinct parts: single-digit and multi-digit. I appreciate the natural move between them, and I often find myself focusing on one side or the other, starting from scratch and working until I have single-digit support, or starting with single-digit already working and moving to multi-digit.

You’ll notice that I have the examples already written, and I am simply re-enabling them as I go, as I’ve found that the actual writing of these particular examples adds little to my goals for the kata.

The music I selected for this kata is the 1812 Overture by Tchaikovsky. I like the ebb and flow of the piece, and I think it fits nicely with the different parts of the kata. One of my favorite parts is when I am finishing the single-digit support, and the music picks up a bit, as if to keep the energy high while filling in the arrays of line segments for the numbers.

Much thanks to Techsmith for sponsoring the series with licenses for Camtasia. It was actually pretty damn easy to record and edit this. And I have barely watched their tutorials, even. :)

Here is the git repository to do it on your own.

Best enjoyed full screen, of course. :)

Number to LCD from Corey Haines on Vimeo.

And, of course, if you have any thoughts, feel free to either comment below, write a blog post or do your own katacast. Fun for the whole family.

And, naturally, if you are going to write a critique, which I appreciate, please don’t do it anonymously; it makes it difficult to take you seriously if you aren’t willing to use your real name.

16 Responses to “Number to LCD”

  1. JoshG says:

    Great stuff.

    I do like to hear you talking about what you’re doing while you’re doing it, though. My feeling is the verbalization of the rationale for your actions is important for those trying to learn TDD.

    Also for training purposes, apart from just the kata of doing the solution, mapping from the requirements and acceptance criteria to the specifications in the tests is also super important so people learn why we express tests in such a way. Things like One Assertion Per Test, when to remove duplication, when setups are bad, and so on are very useful for people learning TDD.

    On a meta note, it’d be great to hear your learnings from putting it together, recording it and so on – I saw a few tweets about some frustrations you were having.

    Great music selection! :-)

  2. John Yerhot says:

    I love seeing other work flows. Great screencast.

  3. coreyhaines says:

    Thanks, John. I appreciate you watching it.

    @JoshG – Yeah, there is a definitely different feel without the narrative. I am planning on doing both, as I think there is a beauty in the flow of the code, accentuated by the music. It was amazingly fun picking out the soundtrack, too.

    We are going to be working on more than just simple ‘learn tdd’-style screencasts, I believe, looking at different aspects. My goal, though, is to keep them fairly short, under 15 minutes (hopefully under 10). A full flow from Cucumber -> RSpec -> Ruby might be a bit long for a lot of things, although I can imagine we can find some.

    Thanks again for the comments.

  4. John Keller says:

    I enjoyed this tremendously, thank you! Perfectly timed as I am introducing concepts like TDD to our team now.

  5. coreyhaines says:

    Thanks, John.

    Just wanted to mention that my favorite part is adding the multi-digit support. I’ve done it both with refactoring under green (maintaining single-digit support) and the way I did it in this recording (multi-digit failing until the end). I kept the multi-digit example running in autospec, because I love the end when both digits appear, the 2 under the 1, then the ‘rotate’ method gets created that puts it alongside it and turns everything green! It is a great moment, I think. :)

  6. coreyhaines says:

    John,

    I should warn you that this may not be the greatest example for teaching TDD to your team, though. I violate a few rules that potentially could confuse people and trigger unnecessary conversations. :) I, and the other katacasts participants, will be posting more traditional TDD-oriented katas in the future, as we have a lot planned.

    In the meantime, here are links to three kata that would be better for teaching, I think:
    Uncle Bob – Prime Factors : http://www.viddler.com/explore/unclebobmartin/videos/2/
    Me – String Templating : http://blog.envylabs.com/2009/08/corey-haines-performance-kata/
    Llewellyn Falco – Chop Kata : http://www.screencast.com/users/isidore_us/folders/Camtasia/media/13f8e8e1-8ca0-4b2c-b20a-6c982cdd50c1#

  7. James Martin says:

    Thanks for posting this; it’s a really nice performance.

    I enjoyed the use of Vim (nice colour scheme btw). You might find navigation easier in larger chunks (pg-up/down and centering the view ‘zz’ etc), it cuts down on the Camtasia ‘jjjjjj’, ‘kkkkkk’ key-press overlays.

    I hadn’t seen the LCD kata before, but it’s a nice one so I’m going to give it a try myself!

    The final couple of refactorings go really well with the musical crescendo at the end, however, I do prefer to hear commentary from the performer. Also, it’s nice to see the web-cam picture-in-picture view a-la your string kata – not sure how easy that is with Camtasia.

    Keep up the good work, these are really valuable!

  8. Matthew Williams says:

    Fantastic Kata Corey, I really enjoyed it. Though a found myself pausing a few times to keep up and get a grasp at what you were doing. I have really enjoyed the Katas you’re publishing and I would agree with Josh in regard to hearing your narrative.

    Though the music really adds a neat effect and really keeps your focus!

    Perhaps using a tool like GarageBand to record the narrative while you code with the music as a background track. GarageBand will automatically do the “ducking” (lowering the volume of the background music to give your voice priority and then returning the volume of the music back to normal when you’re not speaking).

    I really hope this is the first of many and I hope to maybe publish one of my own!

    Thanks again,
    -Matt

  9. [...] This post was mentioned on Twitter by unclebobmartin, J.R. Garcia. J.R. Garcia said: RT @unclebobmartin: This is worth watching. http://bit.ly/4I5qO Corey Haines doing the 7-segment kata to the 1812 overture. [...]

  10. Has anybody yet made a “refactoring kata”? In all the katas I’ve seen this far, the program has always been written from scratch. In a refactoring kata there would be an existing (open source) codebase, where some challenging changes would need to be made.

    I would have a couple of examples of big refactorings from my personal projects. Originally those refactorings took 1-2 hours, but now I can do them in 10-20 minutes.

    For example, in one of those refactorings the project originally used java.math.BigInteger as the ID for objects stored in the database. The BigInteger needs to be replaced with an ObjectId class that I wrote myself. The production code is 4400 SLOC, test code is 7300 SLOC, and the BigInteger is used in over 300 places. I’ve outlined the steps it in http://www.cs.helsinki.fi/u/luontola/tdd-2009/kalvot/02-Code-Quality.pdf pages 7-17.

  11. Matt Gumbley says:

    Great kata, Corey, many thanks. Very enjoyable to watch – the first one I’ve seen.

    The task was the right length for the screncast, and your rspec/growl setup was very interesting. Music was just right – and tied in well with the refactoring towards the end.

    As well as making me want to brush up on my Ruby, I was also trying to second-guess your Vim-fu :-) Re James Martin’s comment about reducing use of jjjjjjj – I’d keep using this style of navigation for a screencast, it reduces the amount of ‘magic jumping around’ that would appear otherwise. Similarly for cut-and-paste, e.g. when you refactored the maps and joins earlier – may be easier to follow with Vim’s visual mode, rather than :8,23.. etc.

    Commentary or on-screen ‘though bubbles’ might add to the presentation, but I thought it worked well enough as is – perhaps a longer, more in-depth development/refactoring of existing code, rather than a standalone exercise might require more assistance though.

    (But the existing code refactoring would not be a kata, methinks? A kata is a simple form you polish until it becomes slick? Reminds me of William Burroughs’ The Discipline of Do-Easy… (http://wikilicious.org/index.php?page=TheNow ))

    Looking forward to more!

  12. “A kata is a simple form you polish until it becomes slick?”

    If the refactoring is repeated many times, then it will become slick, right? (The sequence in the PDF that I linked has still room for improvement.) In the beginning the code is in state X and the goal is to refactor it into state Y with as few steps as possible. Isn’t that a kata?

  13. [...] Number to LCD Kata Watch Corey Haines tackle the Number to LCD Kata. [...]

  14. [...] Software Craftsmanship – Katas » Number to LCD [...]

  15. I thought it was a shame that a side effect of writing code to create this fiddly output is having to produce a lot of it by hand in the tests. I was provoked to explore some alternatives and describe my thoughts here:

    http://anthonybailey.net/blog/2009/11/30/testing-with-less-manual-calculation

  16. [...] on Windows that is fast enough to support the TDD/BDD workflow that Corey Haines exhibits in his Number to LCD code kata. On top of that, I wasted 2 days on trying to get Selenium and Cucumber to work together in [...]

Leave a Reply