Refactoring

…is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

Its heart is a series of small behavior preserving transformations. Each transformation (called a “refactoring”) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.

Learning More

To learn more about Refactoring, the natural starting point is the Refactoring Book. Although I wrote this book back in 2000, it focuses on a basic programming technique that doesn’t change over time. The examples are in Java, but are easily applicable to other programming languages.

Although the original Refactoring book is worthwhile for other languages, if you program in Ruby you’ll probably prefer the Ruby edition, which was adapted by Jay Fields and Shane Harvie. As well as using Ruby examples, this book includes several refactorings that are more applicable to Ruby.

These refactoring books provide the basic explanation and catalog of example refactorings, but many readers enjoy examples to work from. Bill Wake answered this need with his Refactoring Workbook. Coincidentally the original workbook uses java, but there is also a Ruby version.

Beyond Simple Code

The base books and workbooks look at the fundamentals of refactoring software. They are where to start, but refactoring applies in more places and contexts, and several books explore these further aspects.

There is a close relationship between refactoring and patterns. Often the best way to use patterns is to gradually refactor your code to use the pattern once you realize it’s needed. Joshua Kerievsky’s Refactoring to Patterns explores this topic, making this a great topic to learn about once you’ve got the basic refactorings under your belt.

Many software development projects involve relational databases and they need refactoring just as much as code does. Refactoring Databases is somewhat more involved, since you have to not just change the code and schema, you also have to migrate the data. However doing this well is essential to using an evolutionary design approach in a database-intensive environment.

Although it’s an excessive stretch to refer to HTML as a programming language, the concept of refactoring applies very well to HTML. Poorly structured HTML can be a significant barrier to keeping a web site fresh with regular updates. Refactoring HTML allows you to keep sites nimble and makes it easier to take advantage of changes in web technology.

Catalog

This online catalog of refactorings includes the refactorings described in the base Refactoring book and its Ruby edition. For each refactoring I include the problem/solution pair, the sketch, and any notes, corrections, or clarifications I’ve made since the books were published. If you have a safari books online copy of these books, you should find the deep link into the book handy - allowing you to go directly from the catalog to the relevant page in the book.

I may extend this catalog to include refactorings from other books, or purely online refactorings, later on.