Download - Refactoring
![Page 1: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/1.jpg)
RefactoringImproving The Design of Existing Code
Reporter:Jiayun Zhou <[email protected]>
Date :2011/07/13
![Page 2: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/2.jpg)
Outline
Introduction
Bad Smells & Refactorings
Conclusion
Copyright by IISI. All rights reserved 2
![Page 3: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/3.jpg)
Introduction
![Page 4: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/4.jpg)
Eclipse 1.0 – Nov 2001
Copyright by IISI. All rights reserved 4
![Page 5: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/5.jpg)
Eclipse 2.0 – Jun 2002
Copyright by IISI. All rights reserved 5
![Page 6: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/6.jpg)
Eclipse 2.1 – Mar 2003
Copyright by IISI. All rights reserved 6
![Page 7: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/7.jpg)
Eclipse 3.7 – Jun 2011
Copyright by IISI. All rights reserved 7
![Page 8: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/8.jpg)
Copyright by IISI. All rights reserved 8
![Page 9: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/9.jpg)
Martin Fowler
Copyright by IISI. All rights reserved 9
![Page 10: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/10.jpg)
Kent Beck
Copyright by IISI. All rights reserved 10
![Page 11: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/11.jpg)
Bad Smells & Refactorings
![Page 12: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/12.jpg)
Bad Smell
If it stinks, change it.
-Grandma Beck,discussing child-rearing philosophy
Copyright by IISI. All rights reserved 12
![Page 13: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/13.jpg)
WTF
Copyright by IISI. All rights reserved 13
![Page 14: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/14.jpg)
What the Fxxk
Copyright by IISI. All rights reserved 14
![Page 15: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/15.jpg)
Copyright by IISI. All rights reserved 15
![Page 16: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/16.jpg)
Good Programmer
• 30% Good Habits
• 30% English
• 40% Passion, Talent, Practice, and Everything Else
Copyright by IISI. All rights reserved 16
![Page 17: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/17.jpg)
Duplicated Code (Bad Smell)
DRY
(Don't Repeat Yourself)
Copyright by IISI. All rights reserved 17
![Page 18: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/18.jpg)
Duplicated Code (Bad Smell)
DRY
(Don't Repeat Yourself)
vs.
WET
(We Enjoy Typing)
Copyright by IISI. All rights reserved 18
![Page 19: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/19.jpg)
Pull Up Method(Refactoring)
Copyright by IISI. All rights reserved 19
![Page 20: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/20.jpg)
Pull Up – 1 (Eclipse)
Copyright by IISI. All rights reserved 20
![Page 21: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/21.jpg)
Pull Up – 2 (Eclipse)
Copyright by IISI. All rights reserved 21
![Page 22: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/22.jpg)
Pull Up – 3 (Eclipse)
Copyright by IISI. All rights reserved 22
![Page 23: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/23.jpg)
Pull Up – 4 (Eclipse)
Copyright by IISI. All rights reserved 23
![Page 24: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/24.jpg)
Pull Up – 5 (Eclipse)
Copyright by IISI. All rights reserved 24
![Page 25: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/25.jpg)
Long Method (Bad Smell)
• Short methods are good
• Whenever we feel the need to comment something, we write a method instead
• Method name explains the intention
Copyright by IISI. All rights reserved 25
![Page 26: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/26.jpg)
Extract Method(Refactoring)
Copyright by IISI. All rights reserved 26
![Page 27: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/27.jpg)
Extract Method – 1 (Eclipse)
Copyright by IISI. All rights reserved 27
![Page 28: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/28.jpg)
Extract Method – 2 (Eclipse)
Copyright by IISI. All rights reserved 28
![Page 29: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/29.jpg)
Extract Method – 3 (Eclipse)
Copyright by IISI. All rights reserved 29
![Page 30: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/30.jpg)
Conditionals and loops also give signs for extractions.
Use Decompose Conditional to deal with conditional expressions.
With loops, extract the loop and the code within the loop into its own method.
Copyright by IISI. All rights reserved 30
![Page 31: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/31.jpg)
Large Class (Bad Smell)
• Trying to do too much
• Shows up as too many instance variables
• Duplicated code cannot be far behind
Copyright by IISI. All rights reserved 31
![Page 32: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/32.jpg)
Extract Class(Refactoring)
Copyright by IISI. All rights reserved 32
![Page 33: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/33.jpg)
Extract Class – 1 (Eclipse)
Copyright by IISI. All rights reserved 33
![Page 34: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/34.jpg)
Extract Class – 2 (Eclipse)
Copyright by IISI. All rights reserved 34
![Page 35: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/35.jpg)
Extract Class – 3 (Eclipse)
Copyright by IISI. All rights reserved 35
![Page 36: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/36.jpg)
Long Parameter List(Bad Smell)
• Hard to understand
• Become inconsistent and difficult to use
• Forever changing them as you need more data
Copyright by IISI. All rights reserved 36
![Page 37: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/37.jpg)
Replace Parameter with Method (Refactoring)
Copyright by IISI. All rights reserved 37
![Page 38: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/38.jpg)
Preserve Whole Object(Refactoring)
Copyright by IISI. All rights reserved 38
![Page 39: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/39.jpg)
Introduce Parameter Object(Refactoring)
Copyright by IISI. All rights reserved 39
![Page 40: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/40.jpg)
Introduce Parameter Object –01 (Eclipse)
Copyright by IISI. All rights reserved 40
![Page 41: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/41.jpg)
Introduce Parameter Object –02 (Eclipse)
Copyright by IISI. All rights reserved 41
![Page 42: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/42.jpg)
Introduce Parameter Object –03 (Eclipse)
Copyright by IISI. All rights reserved 42
![Page 43: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/43.jpg)
Comments (Bad Smell)
• They are sweet smell
• Don't use as a deodorant
Copyright by IISI. All rights reserved 43
![Page 44: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/44.jpg)
Conclusion
![Page 45: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/45.jpg)
How to refactor safely?
Copyright by IISI. All rights reserved 45
![Page 46: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/46.jpg)
Unit Test
Copyright by IISI. All rights reserved 46
![Page 47: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/47.jpg)
Copyright by IISI. All rights reserved 47
![Page 48: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/48.jpg)
How about the translation?
Copyright by IISI. All rights reserved 48
![Page 49: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/49.jpg)
中譯
你可以選擇每個 class 和函式的名字,這給了你一個解釋自己意圖的機會。class 或函式內部則解釋實現這個意圖的作法。如果class 和函式內部又以「更小單元的意圖」來編寫,你所寫的程式碼就可以「與其結構中的大部分重要資訊溝通」。 (p.61)
Copyright by IISI. All rights reserved 49
![Page 50: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/50.jpg)
原文
Choosing the name of each class and the name of each method gives you an opportunity to explain what you intend. The internals of the class or method explain how the intention is realized. If the internals also are written in terms of intention in yet smaller pieces, you can write code that communicates most of the important information about its own structure.
Copyright by IISI. All rights reserved 50
![Page 51: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/51.jpg)
Copyright by IISI. All rights reserved 51
![Page 52: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/52.jpg)
我的翻譯
你可以選擇每個 class 和函式的名字,這給了你一個解釋自己意圖的機會。class 或函式內部則解釋實現這個意圖的作法。如果class 和函式內部又以「更小單元的意圖」來編寫,你所寫的程式碼就可以「表達出它自己結構的大部分重要資訊」。
Copyright by IISI. All rights reserved 52
![Page 53: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/53.jpg)
盡信書不如無書
Copyright by IISI. All rights reserved 53
![Page 54: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/54.jpg)
Copyright by IISI. All rights reserved 54
![Page 55: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/55.jpg)
琴鍵有限,琴藝無限
Copyright by IISI. All rights reserved 55
![Page 56: Refactoring](https://reader034.vdocuments.site/reader034/viewer/2022050920/549a556ab4795979508b4649/html5/thumbnails/56.jpg)
Headquarter
6F., No.7, Sec. 2, Xianmin Blvd., Banqiao Dist., New Taipei City 22041, Taiwan(R.O.C.)
TEL : +886-2-8969-1969 FAX : +886-2-8969-3359
- Thank You -