Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Article

Fowler Refactoring Example

Rate me:
Please Sign up or sign in to vote.
2.92/5 (8 votes)
19 May 2003CPOL2 min read 76.3K   1.3K   26   6
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)


Written By
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 7:14
Member 18056562-Aug-10 7:14 
GeneralThanks for the initiative! Pin
Gary Lutchansky23-Feb-10 10:39
Gary Lutchansky23-Feb-10 10:39 
GeneralSome comments Pin
Anthony_Yio3-Jun-03 15:27
Anthony_Yio3-Jun-03 15:27 
GeneralRefactoring Pin
Marc Clifton21-May-03 3:12
mvaMarc Clifton21-May-03 3:12 
GeneralRe: Refactoring Pin
Blake Coverett21-May-03 7:39
Blake Coverett21-May-03 7:39 
GeneralRe: Refactoring Pin
David Veeneman24-May-03 10:33
David Veeneman24-May-03 10: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.