20
May. 2011 Jerry Hsu Refactoring (ch 12, 13)

重構—改善既有程式的設計(chapter 12,13)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 重構—改善既有程式的設計(chapter 12,13)

May. 2011 Jerry Hsu

Refactoring (ch 12, 13)

Page 2: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.2Classification

Chapter 12. Big Refactorings

• 本質– 沒有準確的重構步驟– 時間

Page 3: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.3Classification

Chapter 12. Big Refactorings

• 梳理並分解繼承體系• 將程序式設計轉化為物件設計• 將領域和表述 /顯示分離• 提煉繼承體系

Page 4: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.4Classification

12.1 Tease Apart Inheritance

• 梳理並分解繼承體系– 混亂的繼承體系

• 重複的程式碼

• 某個繼承體系承擔了兩項不同的責任– 交易種類– 顯示風格

Page 5: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.5Classification

Tease Apart Inheritance

1. Extract class

Page 6: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.6Classification

12.1 Tease Apart Inheritance

2. Create sub class

3. Move method

Page 7: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.7Classification

12.1 Tease Apart Inheritance

Page 8: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.8Classification

12.1 Tease Apart Inheritance

Page 9: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.9Classification

12.2 Convert Procedural Design to Objects

• 將程序式設計轉化為物件設計

Page 10: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.10Classification

12.2 Convert Procedural Design to Objects

• 作法 : 看課本• Ex: Page 5 Page 51

Page 11: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.11Classification

12.3 Separate Domain from Presentation

• 將領域和表述 /顯示分離

Page 12: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.12Classification

12.3 Separate Domain from Presentation

• MVC– Model Domain– View Presentation– Control Logic

• one business logic multiple views

Page 13: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.13Classification

12.3 Separate Domain from Presentation

Table entity Business entityTransform

Database

Invoker

Page 14: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.14Classification

12.4 Extract Hierarchy

• 提煉繼承體系• 某個 class做了太多工作,其中一部分的工作是以大量條件式完成的

Page 15: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.15Classification

12.4 Extract Hierarchy

Page 16: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.16Classification

Chapter 13. Refactroing, Reuse and Reality• 為什麼開發者不願意重構他們的程式

1. 你不知道如何重構2. 重構的效益要長時間才能展現出來3. 重構是一項額外工作,老闆付錢給你是要你寫新功能4. 重構會破壞現有程式

Page 17: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.17Classification

Chapter 13. Refactroing, Reuse and Reality• 重構的效益要長時間才能展現出來– 短期利益

• 程式碼總量變少• 重複的程式碼有錯誤只需要修改一個地方• Trace, debug

– 中期利益• 重構的抽象層對於定義後續檔案系統很有幫助

– 長期利益• 我們的身體健康

Page 18: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.18Classification

Chapter 13. Refactroing, Reuse and Reality• 降低重構帶來的額外開銷• Reduce the overhead of refactoring

– 工具– 雖然重構需要額外開銷,但是可以從”在程式開發期間其他階段

降低所需心力及滯怠時間”而獲得補償

Page 19: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.19Classification

Chapter 13. Refactroing, Reuse and Reality• 安全的進行重構

– How• 相信自己的功力• 相信 compiler• 相信 test suite• 相信 code review

Page 20: 重構—改善既有程式的設計(chapter 12,13)

Copyright 2007 - Trend Micro Inc.20Classification

Thank you