Click here to Skip to main content
Click here to Skip to main content

Fowler Refactoring Example

By , 19 May 2003
 

Introduction

Chapter 1 of Fowler, Refactoring: Improving the Design of Existing Code (Addison Wesley 2000) presents an extended example of refactoring, which covers pages 1 to 52 of the book. The example demonstrates the process of refactoring and several specific refactorings that one typically encounters in the process of refactoring code. The example, as presented in the book, is written in Java. This project, and its related projects, translate the example to C#.

How the Example Was Translated

The C# example project follows Fowler's code fairly closely. Changes and additions to Fowler's code are as follows:

  • I used C# properties in place of Fowler's public accessor methods (getter/setter methods).
  • Fowler declares his price codes as Java constants. I have declared them as a C# enum (see Movie class).
  • Unit tests have been added to the example, in a class named Tests.cs. All test classes and methods were created to the specifications of NUnit v. 2.0.

Using the Example

The source code file contains a VS.NET project titled "StartingPoint". This project represents the initial state of the project, before any refactoring has been done. It appears on pages 2 - 5 of the book.

Use this code as your starting point to work through the chapter. As Fowler performs each refactoring in the book, follow along, performing the same refactoring on the C# project. This process will help you gain a better understanding of what Fowler calls the "rhythm of refactoring", as well as the specific refactorings demonstrated in the example.

Testing As You Go

Unit testing is a key element of refactoring. A good suite of unit tests allows you to make a small change, then test. Make another small change, then test again. Once the tests are written, unit testing with a tool such as NUnit or csUnit is nearly automatic and instantaneous.

Fowler mentions his tests in Chapter 1 of Refactoring, but he doesn't show them. So, I added a simple suite of unit tests to the C# project to test the basic elements of the example. I don't claim these tests are complete; merely adequate. They are written for NUnit 2.0, and the project contains a reference to the NUnit framework DLL.

The project requires that you have NUnit installed on your computer. NUnit can be downloaded from NUnit website. The project reference assumes that the DLL can be found in its default location. If it cannot be found, then you should delete the existing reference and add a new one that points to the correct location.

I hope you find the example as useful as I did. It helped me understand why refactoring (and testing) are core disciplines that every programmer should exercise in writing code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

David Veeneman
Software Developer (Senior) Foresight Systems
United States United States
Member
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSwitch statment not the samememberMember 18056562 Aug '10 - 7:14 
Thanks for the code and one question.
 
Just wondering why in the original switch statement for the children movies
 
thisAmount += (each.getDaysRented() - 3) * 1.5;
 
became
 
thisAmount = (each.getDaysRented() - 3) * 1.5;
 
Took me a while to figure out why the test were failing when I used the code from the book with your example. Since the test are coded for 3 not 4.5 doesn't seem to matter unless you do what I did which was copy paste from the book into the refactored price class and then the tests start to fail.
GeneralThanks for the initiative!memberGary Lutchansky23 Feb '10 - 10:39 
David,
 
Glad you put this together. Now rather then reading the Refactoring book, I'm working through the example too. And I realized how simple it is to type in the Java code too.
 
Thanks!
gary
GeneralSome commentsmemberAnthony_Yio3 Jun '03 - 15:27 
Your article is too short. Some extend of elaboration would be great. Smile | :)
GeneralRefactoringsubeditorMarc Clifton21 May '03 - 3:12 
I must admit that I haven't looked at your files yet (mainly because I'm in the middle of too many other things!) but I bookmarked your article.
 
A couple questions--
 
Do you think that refactoring can be avoided by using proper coding practices at the beginning of development?
 
What would you say are the most important coding practices?
 
Some of what I see in terms of refactoring appears to implement various Design Patterns. Personally, I think that a significant amount of refactoring can be avoided by creating a robust framework on which to build an application (yes, my pet project, I know!). What do you think about that?
 
Marc

 
Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

GeneralRe: RefactoringmemberBlake Coverett21 May '03 - 7:39 
This is the standard argument that's dealt with in the introductory chapters of both Fowler's Refactoring book and Beck's Extreme Programming books.
 
There is merit in both the 'big design up front' school and the 'do the simplest thing that could possibly work' school, but discussions of the relative merits is too big a topic for an a few messages attached to one article. Read the books first - they are as important as the GOF Patterns book.
 
regards,
-Blake
GeneralRe: RefactoringmemberDave Veeneman24 May '03 - 10:33 
Marc Clifton wrote:
Do you think that refactoring can be avoided by using proper coding practices at the beginning of development?
 
Theoretically, yes. But I have found myself fretting over proper design that I take forever getting to coding in the first place. One reason I like the idea of refactoring is that it frees me from having to 'get it right' initially. I can 'get it as good as I can', then refactor to make it better. Takes some of the pressure off.
 
I've also concluded that I rarely ever really understand a problem until I've been coding for a while. So, lately I've been coding to get something that works, then refactoring into something more elegant.
 
Marc Clifton wrote:
Personally, I think that a significant amount of refactoring can be avoided by creating a robust framework on which to build an application (yes, my pet project, I know!).
 
I think it's an intruiging idea. I hope you will your pet project as it progresses,

 
Dave Veeneman
www.veeneman.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 20 May 2003
Article Copyright 2003 by David Veeneman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid