Coding Katas for practicing the refactoring of legacy code

16,154

Solution 1

I don't know of a site that catalogs them directly, but one strategy that I've used on occasion is this:

  1. Find an old, small, unmaintained open source project on sourceforge
  2. Download it, get it to compile/build/run
  3. Read the documentation, get a feel for the code
  4. Use the techniques in Working Effectively with Legacy Code to get a piece of it under test
  5. Refactor that piece, perhaps fixing bugs and adding features along the way
  6. Repeat steps 4 through 6

When you find a part that was especially challenging, throw away your work and repeat it a couple times to reinforce your skills.

This doesn't just practice refactoring, but other skills like code reading, testing, and dealing with build processes.

The hardest problem is finding a project that you're interested enough in to keep working in. The last one I worked on was a python library for genetic programming, and the current one I'm working on is a IRC library for Java.

Solution 2

I feel like necromancer replying to such an old thread, but there is one thing that would make for a worthy addition - Legacy Code Retreat.

Idea is to have a Code Retreat with legacy code and try to practice the very techniques for dealing with such, but I can't see anything that would ban you from simply using the code prepared and practicing with it by yourself. Just using it for creating a Golden Master makes for an hour of work, and there's a lot more you can do. If your kata usually last around 2 hours, I'd say just by splitting what usually happens on LCR into kata gives you four different things to work on.

There's a GitHub repository by idea's author, J.B. Rainsberger, that contains a simple legacy system that you are to work with, Trivia Game.

From my experience as organizer/participant, folks really liked this and it was illuminating to see what can be a problem in a legacy code and where your refactoring can lead you astray (and how!). Here's yet another account of how it looks like, by Andreas Leidig.

Solution 3

Emily Bache has a github repository with some refactoring katas: Emily Bache's Refactoring Kata Repo. There are variants of KataYahtzee and KataTennis to refactor. Also, she has a variant of the Gilded Rose Kata, which was designed as a refactoring kata.

Also, she has the Racing Car Katas in her repo: Racing Car Kata. The Race Car Katas also include good exercises for refactoring.

Those kata have the code in multiple langauages:

  • C++
  • C#
  • Java
  • Javascript
  • Python
  • Ruby
Share:
16,154
user3953201
Author by

user3953201

A C# .NET Developer with a passion for coding best practices and Test Driven Development

Updated on June 18, 2022

Comments

  • user3953201
    user3953201 about 2 years

    I've gotten quite interested in coding katas in recent months. I believe they are a great way to hone my programming skills and improve the quality of the code I write on the job.

    There are numerous places where Katas can be found. like..

    http://codekata.pragprog.com/

    http://schuchert.wikispaces.com/Katas

    http://www.codingdojo.org/

    I've found these to be excellent repositories of Katas... my attempts at some of them have been been immensely rewarding.

    However, I feel that all the Kata's I've seen so far have one short coming. None of them seem to allow me to practice refactoring bad code. It's great learning how to write clean code the first time around...but in my current job, I don't have too many opportunities to write new code. Rather I'm often battling against legacy code and trying to figure out how to refactor modules, eliminate dependencies, and reduce coupling.

    As such, I'm on the look out for a couple Katas that I can use to hone my skills of refactoring legacy code and turning it into clean code.

    Does anyone know of any that already exist? I know I get a lot of practice at it while I'm at work...but I'd like to hone my skills to the point where I'm able to quickly see how to break apart dependencies and separate concerns in classes that do far too much.

  • user3953201
    user3953201 almost 15 years
    +1 very interesting suggestion. Like you said, the tricky thing is finding an appropriate project. I'll give this one a lot of consideration. Perhaps if I, or others, find some that are very useful it could be saved and documented somewhere as a Kata...
  • TrueWill
    TrueWill over 14 years
    "Dealing with Legacy Code" - do you mean "Working Effectively with Legacy Code"? amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/…
  • Ryan
    Ryan over 14 years
    Yes, I mean "Working Effectively with Legacy Code". Thanks for the catch!
  • Christian Maslen
    Christian Maslen over 9 years
    Emily has also published the book The Coding Dojo Handbook that lists the Katas and describes how to start and run your own coding dojos. I highly recommend the book: leanpub.com/codingdojohandbook
  • Steve Clanton
    Steve Clanton over 9 years
    I have used her book to run dojos, and I agree that it is an excellent resource. Emily has a related course at pluralsight.com/courses/the-coding-dojo, as well.