refactoring, a first example

19
Refactoring, a First Refactoring, a First Example Example Vorleak Chy Vorleak Chy ([email protected]) ([email protected]) Software Engineer Software Engineer Yoolk Inc Yoolk Inc http://groups.google.com/group/khm

Upload: vorleak-chy

Post on 09-May-2015

1.100 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Refactoring, A First Example

Refactoring, a First Refactoring, a First ExampleExample

Vorleak Chy ([email protected])Vorleak Chy ([email protected])

Software EngineerSoftware Engineer

Yoolk IncYoolk Inchttp://groups.google.com/group/khmertech/

Page 2: Refactoring, A First Example

Before RefactoringBefore Refactoring

Unreadable codeUnreadable code

Duplicated codeDuplicated code

Complex codeComplex code

Hard to modifyHard to modify

Page 3: Refactoring, A First Example

After RefactoringAfter Refactoring

Easier to understandEasier to understand

Cheaper to modifyCheaper to modify

Clean codeClean code

Better codeBetter code

Page 4: Refactoring, A First Example

IntroductionIntroduction

What is Refactoring?What is Refactoring?

Why Refactor?Why Refactor?

Page 5: Refactoring, A First Example

What is Refactoring?What is Refactoring?

The process of changing a software The process of changing a software system in such a way that it does not system in such a way that it does not alter the alter the external behaviour external behaviour of the of the code, yet improves its code, yet improves its internal internal structurestructure..

Fowler, et al., Refactoring, 1999.Fowler, et al., Refactoring, 1999.

Page 6: Refactoring, A First Example

Why Refactor?Why Refactor?

The realityThe realityExtremely difficult to get the design Extremely difficult to get the design “right” the first time“right” the first time

Hard to fully understand the problem Hard to fully understand the problem domaindomain

Hard to understand user requirements, Hard to understand user requirements, even if the user does!even if the user does!

Hard to know how the system will evolve Hard to know how the system will evolve in five yearsin five years

Original design is often inadequateOriginal design is often inadequate

System becomes brittle over time, and System becomes brittle over time, and more difficult to changemore difficult to change

Page 7: Refactoring, A First Example

Why Refactor? (Cont.)Why Refactor? (Cont.)

Refactoring helps you toRefactoring helps you toManipulate code in a safe environment Manipulate code in a safe environment (behavior preserving)(behavior preserving)

Recreate a situation where evolution is Recreate a situation where evolution is possiblepossible

Understand existing codeUnderstand existing code

Page 8: Refactoring, A First Example

Look in exampleLook in example

So what?So what?

Try to understand in the real Try to understand in the real exampleexample

Page 9: Refactoring, A First Example

The Starting PointThe Starting Point

Page 10: Refactoring, A First Example

Comments on the Comments on the Starting ProgramStarting Program

When you find you have to add a When you find you have to add a feature to a program, and the feature to a program, and the program's code is not structured program's code is not structured in a convenient way to add the in a convenient way to add the feature, first refactor the feature, first refactor the program to make it easy to add program to make it easy to add the feature, then add the the feature, then add the feature.feature.

Page 11: Refactoring, A First Example

The First Step in The First Step in RefactoringRefactoring

Before you start refactoring, Before you start refactoring, check that you have a solid suite check that you have a solid suite of tests. These tests must be of tests. These tests must be self-checking.self-checking.

Page 12: Refactoring, A First Example

Decomposing and Decomposing and Redistributing the Redistributing the Statement Method Statement Method (Extract Method)(Extract Method)Refactoring changes the Refactoring changes the

programs in small steps. If you programs in small steps. If you make a mistake, it is easy to find make a mistake, it is easy to find the bug.the bug.

Any fool can write code that a Any fool can write code that a computer can understand. Good computer can understand. Good programmers write code that programmers write code that humans can understand.humans can understand.

Page 13: Refactoring, A First Example

Moving the Amount Moving the Amount CalculationCalculation

Move MethodMove Method

Replace Temp with QueryReplace Temp with Query

Page 14: Refactoring, A First Example

Extracting Frequent Extracting Frequent Renter PointsRenter Points

Extract MethodExtract Method

Page 15: Refactoring, A First Example

Removing TempsRemoving Temps

Replace Temp with QueryReplace Temp with Query

Page 16: Refactoring, A First Example

Replacing the Replacing the Conditional Logic on Conditional Logic on Price Code with Price Code with PolymorphismPolymorphismMoving methods to movieMoving methods to movie

Page 17: Refactoring, A First Example

At last … InheritanceAt last … Inheritance

Page 18: Refactoring, A First Example
Page 19: Refactoring, A First Example

Thank-you for attendingThank-you for attending

KhmerTech

Feel free to join athttp://groups.google.com/group/khmertech/