Click here to Skip to main content
Click here to Skip to main content
Go to top

Fowler Refactoring Example

, 19 May 2003
Rate this:
Please Sign up or sign in to vote.
A C# translation of the 'Starting Point' example in Chapter 1 of "Refactoring - Improving the Design of Existing Code", by Martin Fowler.

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)

Share

About the Author

David Veeneman
Software Developer (Senior) Foresight Systems
United States United States
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.

Comments and Discussions

 
GeneralSwitch statment not the same PinmemberMember 18056562-Aug-10 7:14 
GeneralThanks for the initiative! PinmemberGary Lutchansky23-Feb-10 10:39 
GeneralSome comments PinmemberAnthony_Yio3-Jun-03 15:27 
GeneralRefactoring PinsubeditorMarc Clifton21-May-03 3:12 
GeneralRe: Refactoring PinmemberBlake Coverett21-May-03 7:39 
GeneralRe: Refactoring PinmemberDave Veeneman24-May-03 10:33 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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