Practicing Refactoring

[article]
Summary:
As programmers, we're constantly working to improve and evolve our designs. Refactoring helps us take an evolving code base and make it look like the code was designed for today's problems right from the start. And hey, it can actually be fun! It can be difficult to make small steps in a tricky bit of code, hard to figure out how to fix a particular code smell, and tricky to know when you're done.

As programmers, we're constantly working to improve and evolve our designs. Refactoring helps us take an evolving code base and make it look like the code was designed for today's problems right from the start. And hey, it can actually be fun!

But it's not easy. It can be difficult to make small steps in a tricky bit of code, hard to figure out how to fix a particular code smell, and tricky to know when you're done.

Practice helps us build our skills and recognize patterns in code. We practice to get better, to build good habits, and to learn new techniques. Here I'm thinking of practice as something we do outside of work; something we can do in an environment without the stress and other commotions of our day jobs. 

What are some ways to practice refactoring? Naresh Jain has posted a few puzzles on his blog (he calls them "Refactoring Teasers") along with his solutions to the problems. I like these because they start out small but offer enough material to get in and try a few approaches.

William Wake and Kevin Rutherford have released the Refactoring in Ruby book, with plenty of exercises and solutions. The authors suggest a few ways to practice, including a technique of applying and unwinding the refactoring to help you to identify when you're creating a code smell.

The Industrial Logic albums offer online courseware with exercises and suggested solutions. In particular, they have a few courses on code smells and refactoring.

You can use tools like Reek for Ruby or Findbugs in Java to find code with potential problems or code smells. An automated tool might not always identify real problems, but they provide an emotionless opinion in the way only machines can.  I tried using Reek on one of my pet Rails projects and it identified a few areas that seemed suspicious that I hadn't noticed on my own (like a robot pair?). 

Plus there's a wealth of opportunities in open source software - you can grab some source and experiment. This is a great way to get some practice navigating code that's not familiar to you.

About the author

AgileConnection is a TechWell community.

Through conferences, training, consulting, and online resources, TechWell helps you develop and deliver great software every day.