tech talks#6: code refactoring
TRANSCRIPT
CODE REFACTORING
Phạm Anh Đới – [email protected] Nguyễn Việt Khoa – [email protected]
Hanoi, 11/2011
TechTalks #6
2
Objectives
• What’s Code Refactoring?
• Why do we Refactor?
• When should we Refactor?
• How do we Refactor?
• Refactoring Techniques
• Code Refactoring Tools
• Refactoring Dojo
Code Refactoring – TechTalks #6
3
What’s Code Refactoring?
“A series of small steps, each of which changes
the program’s internal structure without
changing its external behavior “
Martin Fowler
Code Refactoring – TechTalks #6
4
What’s Code Refactoring?
• Code reorganization
o Implies equivalence
o Change the structure, not the behavior
• Cleans up “code-smell”
• Does NOT fix bugs
Code Refactoring – TechTalks #6
5
Why do we Refactor?
• Helps us deliver more business value faster
• Improves the design of our software
• Minimizes technical debt
• Keep development at speed
• To make the software easier to understand
• To help find bugs
• To “Fix broken windows”
Code Refactoring – TechTalks #6
6
Example
Which code segment is easier to read?
Sample 1:
if (markT>=0 && markT<=25 && markL>=0 && markL<=25){
float markAvg = (markT + markL)/2;
System.out.println("Your mark: " + markAvg);
}
Sample 2:
if (isValid(markT) && isValid(markL)){
float markAvg = (markT + markL)/2;
System.out.println("Your mark: " + mark);
}
Code Refactoring – TechTalks #6
7
When should we Refactor?
• To add new functionalityo refactor existing code until you understand ito refactor the design to make it simple to add
• To find bugso refactor to understand the code
• For code reviewso immediate effect of code reviewo allows for higher level suggestions
Code Refactoring – TechTalks #6
8
How do we Refactor?
• Manual Refactoringo Code Smells
• Automated/Assisted Refactoringo Refactoring by hand is time consuming and prone to erroro Tools (IDE)
• In either case, test your changes
Code Refactoring – TechTalks #6
9
Code Smell
•Duplicated code
• Feature Envy
• Inappropriate Intimacy
• Comments
• Long Method
• Long Parameter List
• Switch Statements
• Improper Naming
Code Refactoring – TechTalks #6
10
Code Smell examples (1)
Code Refactoring – TechTalks #6
public void display(String[] names) { System.out.println(“--------------"); for(int i=0; i<names.length; i++){
System.out.println(“ + " + names[i]); }System.out.println(“--------------");
}
public void listMember(String[] names) { System.out.println(“List all member: ”);System.out.println(“--------------"); for(int i=0; i<names.length; i++){
System.out.println(“ + " + names[i]); }System.out.println(“--------------");
}
Duplicated code
11
Code Smell examples (2)
Code Refactoring – TechTalks #6
public int getSum() { Scanner input = new Scanner(System.in); int[] list = new int[100]; //Input System.out.println("count:"); int count= input.nextInt(); for (int i= 0; i < count; i++) { list[i] = input.nextInt(); }
//Get sum int sum=0; for (int i= 0; i < count; i++) { sum+=list[i]; } return sum; }
Long Method
12
Code Smell examples (3)
Code Refactoring – TechTalks #6
public String formatStudent( int id, String name, Date dob,String
province, String address, String phone ){
//TODO:return null;
}
Long Parameter List
13
Refactoring Techniques
• for more abstraction
• for breaking code apart
• for improving code standard
Code Refactoring – TechTalks #6
14
For more abstraction
• Encapsulate Field – force code to access the field with getter and setter methods
• Generalize Type – create more general types to allow for more code sharing
• Replace type-checking code with State/Strategy
• Replace conditional with polymorphism
Code Refactoring – TechTalks #6
15
Example
public class Book{ String title;
public static void main(String[] args) { Book book = new Book();
String title = new Scanner(System.in).nextLine();
if(title!=null){title = title.trim();
if(!title.isEmpty()){book.title = title;System.out.println("My book: " +
book.title); }
}}
}
public class Book{ private String title;
public void setTitle(String title){ if(title!=null){
title = title.trim();if(!title.isEmpty()){
this.title = title; }
} }
public String getTitle(){return title;
}
public static void main(String[] args) { Book book = new Book(); String title = new Scanner(System.in).nextLine();book.setTitle(title);System.out.println("My book: " + book.getTitle());
} }
Code Refactoring – TechTalks #6
16
For breaking code apart
• Extract Method, to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions.
• Extract Class moves part of the code from an existing class into a new class.
Code Refactoring – TechTalks #6
17
public void showInfor(){showUser();
System.out.println(“Email: “ + email);System.out.println(“Phone: “ + phone);System.out.println(“Address: “ + address);
}
Examplepublic void showInfor(){
showUser();
showContact();}
public void showContact(){System.out.println(“Email: “ + email);System.out.println(“Phone: “ + phone);System.out.println(“Address: “ + address);
}
Code Refactoring – TechTalks #6
18
For improving code standard
• Move Method or Move Field – move to a more appropriate Class or source file
• Rename Method or Rename Field – changing the name into a new one that better reveals its purpose
• Pull Up – in OOP, move to a superclass
• Push Down – in OOP, move to a subclass
Code Refactoring – TechTalks #6
19
Example
Code Refactoring – TechTalks #6
20
Tools for Code Refactoring
Supported by IDE
•For Java:o Netbeans (www.netbeans.org)o Eclipse (www.eclipse.org)
o IntelliJ IDEA (www.jetbrains.com)
•For .NET:o Visual Studio (msdn.microsoft.com)o .NET Refactoring (www.dotnetrefactoring.com)
o JustCode, ReSharper, Visual Assist, … (addon for Visual Studio)
Code Refactoring – TechTalks #6
21
Refactoring Dojo
Code Refactoring – TechTalks #6
22
Refactoring and TDD
Code Refactoring – TechTalks #6
TDD Rhythm - Test, Code, Refactor
23
Summary
• Improving the design of existing code
• Refactoring does not affect behavior
• The system is also kept fully working after each small refactoring
• Two general categories of benefits to the activity of refactoring: Maintainability and Extensibility
• 3 techniques for refactoring
• Using tools for Code Refactoring
• Code Refactoring are often used to improve quality and enhance project agility.
Code Refactoring – TechTalks #6
24
Q & A
Code Refactoring – TechTalks #6
25
References
•Book: Improving the Design of Existing Code (by Martin Fowler)
•Sites:
•http://refactoring.com
•http://en.wikipedia.org/wiki/Refactoring
•http://wiki.netbeans.org/Refactoring
•http://msdn.microsoft.com/en-us/library/ms379618(v=vs.80).aspx
Code Refactoring – TechTalks #6
26
Thank you
Code Refactoring – TechTalks #6