8 - Refactoring
8 - Refactoring
Assurance
Part 8
Refactoring
1
Objectives
To introduce refactoring
2
What is Refactoring?
Refactoring is about cleaning up the code
after it has been written
3
Why Refactoring?
To improve the software design
Makes the program easier to change
4
Should We Refactor?
Some argue that good design does not lead to code
needing refactoring but:
7
Extract Method
8
Replace Temp with Query
9
Introduce Explaining Variable
10
Decompose Conditional
11
Replace Data Value with
Object
12
Replace Array with Object
13
Replace Magic Number with
Symbolic Constant
14
Replace Subclass with Fields
15
Consolidate Duplicate Conditional
Fragments
16
Rename Method
17
Separate Query from Modifier
18
Parameterize Method
19
Preserve Whole Object
20
Introduce Parameter Object
21
Replace Inheritance with
Delegation
22
Replace Delegation with
Inheritance
23
Some Obstacles to Refactoring
Complexity
Changing design is hard
Understanding code is hard
Possibility to introduce errors
Run tests if possible
Build tests
Cultural Issues
“We pay you to add new features, not to improve
the code!”
Performance issue
Refactoring may slow down the execution
Development is always under time pressure
Refactoring takes time
24
Refactoring better after delivery
Why Are Developers Reluctant to
Refactor Their Programs?
The programmers might not understand how
to refactor.
25
Summary
“The process of changing a software system in such a
way that it does not alter the external behavior of the
code, yet improves its internal structure” [Fowler]
Refactor to:
Improve the software design
Make the software easier to understand
Help find bugs
26