45
Code Refactoring Presented by Steven Dec. 2016

Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Code RefactoringPresented by Steven

Dec. 2016

Page 2: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Why

2

Page 3: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

3

Page 4: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

4

Page 5: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

FastRunnable

5

Page 6: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

FastRunnable

6

Page 7: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Spaghetti Code

● program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled.

● complex and confusing control structures that are barely understandable

7

Page 8: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

8

Page 9: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

What

9

Page 10: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

● Code refactoring is the process of restructuring existing computer code without changing its external behaviour.

Code Refactoring

10

Page 11: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

在不改變軟體的外在行為下,改善既有軟體的內部設計。

11

Page 12: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

2 Advantages

6 Techniques

12

Page 13: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Advantages

13

Page 14: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Expressiveness

14

Page 15: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Expressiveness

Complexity

15

Page 16: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Expressiveness

Complexity

Readability

16

Page 17: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

17

Page 18: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Expressiveness

Complexity

Readability

18

Page 19: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Maintainability

Expressiveness

Complexity

Readability

19

Page 20: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Maintainability

Expressiveness

Complexity

Readability

Extensibility

20

Page 21: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Maintainability Extensibility

21

Page 22: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Techniques

22

Page 23: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenaming

23

Page 24: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenamingcommon: private / (default) / protected / public

24

Page 25: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenamingcreate more general types, e.g., super-classes

25

Page 26: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenamingextract {classes, methods, funcions}fewer methods → clearer purpose and functionality

26

Page 27: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenaming

rename {methods, fields}

27

Page 28: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenaming

moving methods between parent/child classes

28

Page 29: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization ExtractionEncapsulation

Pulling-up Pushing-downRenaming

http://refactoring.com/catalog/index.html29

Page 30: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

How

30

Page 31: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Encapsulation 31

Page 32: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Encapsulation 32

Page 33: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

data / field → private

Setter & Getter

Encapsulation 33

Page 34: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization 34

Page 35: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization 35

Page 36: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization 36

Page 37: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization 37

Page 38: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Generalization 38

Page 39: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Renaming 39

Page 40: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Renaming 40

Page 41: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

Conclusion

41

Page 42: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

● what

42

Page 43: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

● whatMaintainability

Extensibility43

Page 44: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

44

Page 45: Code Refactoring · 2018. 3. 4. · Spaghetti Code program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. complex and confusing control structures that are

45