modern software development using c#.net chapter 6: refactoring

11
Modern Software Development Using C# .NET Chapter 6: Refactoring

Upload: franklin-strickland

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET

Chapter 6: Refactoring

Page 2: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 2

Overview

• Refactoring is redesign aimed at improving existing software without adding new functionality.

• Like any design activity, it is a process that is guided by basic principles, experience, and creativity.

Page 3: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 3

Overview continued

• The goal of refactoring is to make the resulting software system easier to understand and maintain as future changes are made in response to new requirements.

• Through a disciplined process of rebuilding classes, the architecture of a software system is refined and enhanced during the process of refactoring without changing the observable behavior of the system.

• Only the internal structure of the system is improved.

Page 4: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 4

Overview continued

• Many organizations are reluctant to encourage or support refactoring activity because of the fear that defects (bugs) may be introduced during the process of modifying and rebuilding classes.

• They believe that the expenditures involved are not commensurate with the benefits, since in the end, no new functionality is added to a refactored system.

• In some cases such a view is short-sighted because the long-term benefits of having a system that is easier to understand because its parts fit more logically together may lower the amortized cost of development.

Page 5: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 5

Overview continued

• Refactoring is typically performed incrementally. A series of modifications from minor to major is undertaken.

• These modifications include: • renaming an entity such as a class, method, or parameter• moving a field from one class to another• changing the number of parameters in a method• pulling code out of a method to extract a new method• moving a method up or down an inheritance hierarchy• extracting a super class in order to be able to factor

common behavior (fields or methods)• replacing a branching construct with polymorphism

Page 6: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 6

Overview continued

• At each stage of refactoring, careful testing must be done to assure that the external behavior of the system is unchanged.

• Unit testing (see http://junit.org/index.htm) has become a widely used activity in conjunction with refactoring.

• It is difficult to know when refactoring is needed and when it is completed.

• As a complex software system evolves, refactoring is often performed at various times during the development cycle.

Page 7: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 7

Principles used in refactoring (Long Method)

• Comments and white-space delimiters often provide a clue about where to extract a private method with the goal of shortening and simplifying the method that the code is embedded in.

• The extracted method should provide a simple and well-defined mission.

• The mission of the extracted code is what is important, not its length.

• One should never arbitrarily extract methods from a given method only to shorten the original method.

Page 8: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 8

Principles used in refactoring continued (Long Method)

• In the process of creating extracted private methods that support a given method, duplication or near duplication of functionality may be discovered.

• Using parameter modification, two or more such extracted methods might be combined into a single supporting method.

Page 9: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 9

Principles used in refactoring continued (Long Method)

• The goal of method extraction is to manage complexity through functional decomposition.

• The overhead of additional method calls should generally not be a consideration.

• Intellectual overhead, which is a measure of the programmer’s intellectual cycles (thinking time) that must be expended in understanding and maintaining the software, is typically much more expensive than the often immeasurable extra machine cycles that extracted methods may entail.

Page 10: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 10

Principles used in refactoring (Long Parameter List)

• Replace one or more parameters with a parameter object.

• The parameter list may be shortened by sending in an object of the new class to replace the individual parameters in the original parameter list.

Page 11: Modern Software Development Using C#.NET Chapter 6: Refactoring

Modern Software Development Using C# .NET by Richard Wiener 11

Case Study

A detailed case study is presented in Chapter 6 that walks the reader through the process of refactoring an initially poor design. This case study involves too much code to be reproduced using PowerPoint slides.