Refactoring, A First Example - Presentation Transcript
Refactoring, a First Example Vorleak Chy (vorleak.chy@gmail.com) Software Engineer Yoolk Inc http://groups.google.com/group/khmertech/
Before Refactoring
Unreadable code
Duplicated code
Complex code
Hard to modify
After Refactoring
Easier to understand
Cheaper to modify
Clean code
Better code
Introduction
What is Refactoring?
Why Refactor?
What is Refactoring?
The process of changing a software system in such a way that it does not alter the external behaviour of the code, yet improves its internal structure .
Fowler, et al., Refactoring, 1999.
Why Refactor?
The reality
Extremely difficult to get the design “right” the first time
Hard to fully understand the problem domain
Hard to understand user requirements, even if the user does!
Hard to know how the system will evolve in five years
Original design is often inadequate
System becomes brittle over time, and more difficult to change
Why Refactor? (Cont.)
Refactoring helps you to
Manipulate code in a safe environment (behavior preserving)
Recreate a situation where evolution is possible
Understand existing code
Look in example
So what?
Try to understand in the real example
The Starting Point
Comments on the Starting Program
When you find you have to add a feature to a program, and the program's code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.
The First Step in Refactoring
Before you start refactoring, check that you have a solid suite of tests. These tests must be self-checking.
Decomposing and Redistributing the Statement Method (Extract Method)
Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Moving the Amount Calculation
Move Method
Replace Temp with Query
Extracting Frequent Renter Points
Extract Method
Removing Temps
Replace Temp with Query
Replacing the Conditional Logic on Price Code with Polymorphism
Moving methods to movie
At last … Inheritance
Thank-you for attending KhmerTech Feel free to join at http://groups.google.com/group/khmertech/
0 comments
Post a comment