How Do You Practice Refactoring Code?

Refactoring legacy code can be daunting. While trial by fire is a great way to learn most kinds of programming, it doesn't leave a lot of room for getting better at it (at least not quickly). When trying to hit a deadline it is often difficult to take a step back, try different techniques, and learn a few keyboard strokes to automate some of the more mundane tasks. Coding katas are one way we can hone our craft outside the pressure of deadlines and our day jobs. It's even better if your day job encourages spending a little time now and then to improve.

What is a coding kata?

The term "coding kata" comes from one of my favorite programming books: The Pragmatic Programmer. "Kata" is a word borrowed from martial arts which describes a movement, or series of movements, repeated to perfect some technique. Applied to programming, it is an exercise that is repeated to perfect some skill. Typically, these exercises are small enough to be executed in a short amount of time and can be solved easily. Once a problem and its solution are known, the programmer can then focus on technique:

  • learning keyboard shortcuts to increase speed
  • applying design patterns to make code easier to change
  • using test-driven development to reduce regression rates and produce cleaner code
  • etc.


Finding a coding kata to try is just a google search away. Another place you might start is here:  http://codekata.com/

What is special about the Gilded Rose kata?

Most coding katas give you the description of a problem and maybe some other goals to make it more interesting. You choose the tools you like and start practicing. The Gilded Rose, however, starts by giving you some code. It isn't clean code. In fact, if you think that it is good code I would genuinely like to hear your thoughts on what I'm missing. The poor quality of the code is part of the challenge. Your mission, if you choose to accept it, is to clean the code up such that it is easier to understand. Having done that, put your new design to the test by adding a new feature to the code. If it is easy to add the new code, congratulations, you have succeeded. It need not stop at success, though. The next step might be to minimize the time it takes you to complete the exercise. Finding tweaks, like keyboard shortcuts or better design patterns, could build technique that helps you to execute more effectively and efficiently in your day job, when the stakes are higher.

There are countless ways to work this kata. It is my go-to when I'm trying to learn a new language, or refresh myself on a language I haven't used in a while. I also use it in my coaching gigs because it lets me easily demonstrate a number of concepts in just a few hours:

  • Automated testing
    • Pinning tests
    • Test-driven development
    • Code coverage
  • Design patterns
    • Factory method pattern
    • Null Object pattern
    • Strategy pattern
    • Template Method pattern
  • Design principles
    • Single Responsibility Principle
    • Open Closed Principle
  • Refactoring techniques
    • Extract method
    • Extract class
    • Extract interface
  • Pair Programming
  • ... and more

By the way, this kata is also a great way to try out a lot of different agile development techniques. 


How do I get started?

All you need to get started is the Gilded Rose source code in a language of your choice, the kata description/requirements, and your favorite development tools. Emily Bache has a Git repository that contains the code for Gilded Rose in many different languages: https://github.com/emilybache/GildedRose-Refactoring-Kata. It also has a requirements document describing the existing behaviors and desired new functionality.  

  

Join Us Live

We are offering a full day course based around the Gilded Rose Kata June 27th, 2019!

       Learn More      

Teams That Finish Early