Click here to Skip to main content
15,112,304 members
Articles / Programming Languages / C#
Article
Posted 19 May 2003

Stats

73.9K views
1.3K downloads
26 bookmarked

Fowler Refactoring Example

Rate me:
Please Sign up or sign in to vote.
2.92/5 (8 votes)
19 May 2003CPOL2 min read
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 Pin
Member 18056562-Aug-10 8:14
MemberMember 18056562-Aug-10 8:14 
GeneralThanks for the initiative! Pin
Gary Lutchansky23-Feb-10 11:39
MemberGary Lutchansky23-Feb-10 11:39 
GeneralSome comments Pin
Anthony_Yio3-Jun-03 16:27
MemberAnthony_Yio3-Jun-03 16:27 
GeneralRefactoring Pin
Marc Clifton21-May-03 4:12
mvaMarc Clifton21-May-03 4:12 
GeneralRe: Refactoring Pin
Blake Coverett21-May-03 8:39
MemberBlake Coverett21-May-03 8:39 
GeneralRe: Refactoring Pin
David Veeneman24-May-03 11:33
MemberDavid Veeneman24-May-03 11:33 

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

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