Your SlideShare is downloading. ×
0
CODE REFACTORING           Phạm Anh Đới – doipa@fpt.com.vn      Nguyễn Việt Khoa – khoanv4@fpt.com.vn                     ...
Code Refactoring – TechTalks #6   2Objectives •     What’s Code Refactoring? •     Why do we Refactor? •     When should w...
Code Refactoring – TechTalks #6                3What’s Code Refactoring? “A series of small steps, each of which changes t...
Code Refactoring – TechTalks #6               4What’s Code Refactoring?• Code reorganization   o Implies equivalence   o C...
Code Refactoring – TechTalks #6                    5Why do we Refactor?•    Helps us deliver more business value faster•  ...
Code Refactoring – TechTalks #6                             6ExampleWhich code segment is easier to read? Sample 1: if (ma...
Code Refactoring – TechTalks #6                        7When should we Refactor?   • To add new functionality    o refacto...
Code Refactoring – TechTalks #6                                 8How do we Refactor?• Manual Refactoring   o Code Smells• ...
Code Refactoring – TechTalks #6                           9Code Smell•Duplicated code                  • Long Method• Feat...
Code Refactoring – TechTalks #6                10Code Smell examples (1)public void display(String[] names) {   System.out...
Code Refactoring – TechTalks #6                      11Code Smell examples (2)      public int getSum() {           Scanne...
Code Refactoring – TechTalks #6             12Code Smell examples (3)public String formatStudent( int id,                 ...
Code Refactoring – TechTalks #6   13Refactoring Techniques• for more abstraction• for breaking code apart• for improving c...
Code Refactoring – TechTalks #6                  14For more abstraction• Encapsulate Field – force code to access the     ...
Code Refactoring – TechTalks #6                                      15Examplepublic class Book{    private String title;p...
Code Refactoring – TechTalks #6                      16For breaking code apart• Extract Method, to turn part of a larger m...
Code Refactoring – TechTalks #6                    17Examplepublic void showInfor(){     showUser();       showUser();    ...
Code Refactoring – TechTalks #6                        18For improving code standard• Move Method or Move Field – move to ...
Code Refactoring – TechTalks #6   19Example
Code Refactoring – TechTalks #6                      20Tools for Code Refactoring   Supported by IDE    • For Java:       ...
Code Refactoring – TechTalks #6   21Refactoring Dojo
Code Refactoring – TechTalks #6                       22Refactoring and TDD                  TDD Rhythm - Test, Code, Refa...
Code Refactoring – TechTalks #6                            23Summary• Improving the design of existing code• Refactoring d...
Code Refactoring – TechTalks #6   24Q&A
Code Refactoring – TechTalks #6                 25References•Book: Improving the Design of Existing  Code (by Martin Fowle...
Code Refactoring – TechTalks #6                                   26                      Thank you                       ...
Upcoming SlideShare
Loading in...5
×

Tech talks#6: Code Refactoring

2,185

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,185
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
89
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Nguồn tham khảo: Java Refactoring Fest (presentation) - Naresh Jain - naresh@agilefaqs.comhttp://refactoring.comRefactoring workbook - William C. WakeRefactoring to Patterns - Joshua KerievskyRefactoring (presentation) - Jason Lee
  • Improves the design of our softwareLoại bỏ những code tồiGiúp code dễ hiểu và dễ bảo trìTạo điều kiện thuận lợi cho các thay đổiTăng tính linh hoạtTăng khả năng tái sử dụng
  • Cầncóvídụ
  • Mỗikỹthuậtcầncómột demo
  • Giới thiệu về một thiết kế (tồi) biểu diễn với UML trênĐới + Khoa (5 phút): thiết kế lại (đồng thời)So sánh 2 bản thiết kế mới của Đới và Khoa với bản ban đầuCùng sinh viên thảo luận, đánh giáDùng Netbeans triển khai theo thiết kế ban đầu và dùng các kỹ thuật Refactor có sẵn của Netbeans để thực hiện Refactor theo 2 bản thiết kế mới
  • Ảnh từ: Slide của Naresh Jain
  • Transcript of "Tech talks#6: Code Refactoring"

    1. 1. CODE REFACTORING Phạm Anh Đới – doipa@fpt.com.vn Nguyễn Việt Khoa – khoanv4@fpt.com.vn Hanoi, 11/2011
    2. 2. Code Refactoring – TechTalks #6 2Objectives • What’s Code Refactoring? • Why do we Refactor? • When should we Refactor? • How do we Refactor? • Refactoring Techniques • Code Refactoring Tools • Refactoring Dojo
    3. 3. Code Refactoring – TechTalks #6 3What’s Code Refactoring? “A series of small steps, each of which changes the program’s internalstructure without changing its external behavior “ Martin Fowler
    4. 4. Code Refactoring – TechTalks #6 4What’s Code Refactoring?• Code reorganization o Implies equivalence o Change the structure, not the behavior• Cleans up “code-smell”• Does NOT fix bugs
    5. 5. Code Refactoring – TechTalks #6 5Why 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”
    6. 6. Code Refactoring – TechTalks #6 6ExampleWhich 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); }
    7. 7. Code Refactoring – TechTalks #6 7When should we Refactor? • To add new functionality o refactor existing code until you understand it o refactor the design to make it simple to add • To find bugs o refactor to understand the code • For code reviews o immediate effect of code review o allows for higher level suggestions
    8. 8. Code Refactoring – TechTalks #6 8How do we Refactor?• Manual Refactoring o Code Smells• Automated/Assisted Refactoring o Refactoring by hand is time consuming and prone to error o Tools (IDE)• In either case, test your changes
    9. 9. Code Refactoring – TechTalks #6 9Code Smell•Duplicated code • Long Method• Feature Envy • Long Parameter List• Inappropriate Intimacy • Switch Statements• Comments • Improper Naming
    10. 10. Code Refactoring – TechTalks #6 10Code Smell examples (1)public void display(String[] names) { System.out.println(“--------------"); for(int i=0; i<names.length; i++){ System.out.println(“ + " + names[i]); } System.out.println(“--------------");} Duplicated codepublic 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(“--------------");}
    11. 11. Code Refactoring – TechTalks #6 11Code Smell examples (2) 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++) { Long Method list[i] = input.nextInt(); } //Get sum int sum=0; for (int i= 0; i < count; i++) { sum+=list[i]; } return sum; }
    12. 12. Code Refactoring – TechTalks #6 12Code Smell examples (3)public String formatStudent( int id, String name, Date dob, String province, String address, Long Parameter List String phone ){ //TODO: return null;}
    13. 13. Code Refactoring – TechTalks #6 13Refactoring Techniques• for more abstraction• for breaking code apart• for improving code standard
    14. 14. Code Refactoring – TechTalks #6 14For 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
    15. 15. Code Refactoring – TechTalks #6 15Examplepublic class Book{ private String title;public class Book{ String title; public void setTitle(String title){ if(title!=null){ public title = void main(String[] args) { static title.trim(); if(!title.isEmpty()){ Book book = new Book(); this.title = title; } String title = new Scanner(System.in).nextLine(); } } if(title!=null){ title = title.trim(); public String getTitle(){ if(!title.isEmpty()){ return title; } book.title = title; System.out.println("My book: " + book.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()); }}
    16. 16. Code Refactoring – TechTalks #6 16For 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.
    17. 17. Code Refactoring – TechTalks #6 17Examplepublic void showInfor(){ showUser(); showUser(); System.out.println(“Email: “ + showContact(); email);} System.out.println(“Phone: “ + phone); System.out.println(“Address: “ + address);public void showContact(){} System.out.println(“Email: “ + email); System.out.println(“Phone: “ + phone); System.out.println(“Address: “ + address);}
    18. 18. Code Refactoring – TechTalks #6 18For 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
    19. 19. Code Refactoring – TechTalks #6 19Example
    20. 20. Code Refactoring – TechTalks #6 20Tools 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)
    21. 21. Code Refactoring – TechTalks #6 21Refactoring Dojo
    22. 22. Code Refactoring – TechTalks #6 22Refactoring and TDD TDD Rhythm - Test, Code, Refactor
    23. 23. Code Refactoring – TechTalks #6 23Summary• 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.
    24. 24. Code Refactoring – TechTalks #6 24Q&A
    25. 25. Code Refactoring – TechTalks #6 25References•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
    26. 26. Code Refactoring – TechTalks #6 26 Thank you doipa@fpt.com.vn – khoanv4@fpt.com.vn
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×