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/
Before RefactoringBefore Refactoring
Unreadable codeUnreadable code
Duplicated codeDuplicated code
Complex codeComplex code
Hard to modifyHard to modify
After RefactoringAfter Refactoring
Easier to understandEasier to understand
Cheaper to modifyCheaper to modify
Clean codeClean code
Better codeBetter code
IntroductionIntroduction
What is Refactoring?What is Refactoring?
Why Refactor?Why Refactor?
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.
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
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
Look in exampleLook in example
So what?So what?
Try to understand in the real Try to understand in the real exampleexample
The Starting PointThe Starting Point
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.
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.
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.
Moving the Amount Moving the Amount CalculationCalculation
Move MethodMove Method
Replace Temp with QueryReplace Temp with Query
Extracting Frequent Extracting Frequent Renter PointsRenter Points
Extract MethodExtract Method
Removing TempsRemoving Temps
Replace Temp with QueryReplace Temp with Query
Replacing the Replacing the Conditional Logic on Conditional Logic on Price Code with Price Code with PolymorphismPolymorphismMoving methods to movieMoving methods to movie
At last … InheritanceAt last … Inheritance
Thank-you for attendingThank-you for attending
KhmerTech
Feel free to join athttp://groups.google.com/group/khmertech/