0% found this document useful (0 votes)
12 views26 pages

8 - Refactoring

Refactoring is the process of cleaning up code to improve software design, making it easier to understand and debug. It is essential in agile development and involves various techniques such as Extract Method and Replace Temp with Query. However, obstacles like complexity, cultural issues, and time pressure can make developers reluctant to refactor their code.

Uploaded by

cyytpqrfgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views26 pages

8 - Refactoring

Refactoring is the process of cleaning up code to improve software design, making it easier to understand and debug. It is essential in agile development and involves various techniques such as Extract Method and Replace Temp with Query. However, obstacles like complexity, cultural issues, and time pressure can make developers reluctant to refactor their code.

Uploaded by

cyytpqrfgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Software Testing and Quality

Assurance
Part 8
Refactoring

1
Objectives
 To introduce refactoring

 To understand the importance of


refactoring

 To give some refactoring examples

2
What is Refactoring?
Refactoring is about cleaning up the code
after it has been written

Refactoring targets potential problem areas


(“bad smells in code”) in the code design
so far

Refactoring integrates very well with the


agile development, but it can (and should)
be used with other processes

3
Why Refactoring?
 To improve the software design
 Makes the program easier to change

 To make the software easier to understand


 Write for people, not the compiler
 Understand unfamiliar code

 To help find bugs


 Refactor while debugging to clarify the
code

4
Should We Refactor?
 Some argue that good design does not lead to code
needing refactoring but:

 Extremely difficult to get the design right the


first time
 You cannot fully understand the problem
domain
 You cannot understand user requirements,
regardless whether or not the user does!
 You cannot really plan how the system will
evolve in five years
 Original design is often inadequate
5
Refactoring Examples
 Extract Method
 Replace Temp with Query
 Introduce Explaining Variable
 Decompose Conditional
 Replace Data Value with Object
 Replace Array with Object
 Replace Magic Number with Symbolic
Constant
 Replace Subclass with Fields
 Consolidate Duplicate Conditional Fragments
6
Refactoring Examples (Cont’d)
Rename Method
Separate Query from Modifier
Parameterize Method
Preserve Whole Object
Introduce Parameter Object
Replace Inheritance with Delegation
Replace Delegation with Inheritance
…

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.

If the benefits are long-term, why exert the


effort now? In the long term, you might not be
with the project to reap the benefits.

Refactoring code is an overhead activity;


programmers are paid to write new features.

Refactoring might break the existing program.

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

 A catalog of refactoring exists: Extract Method, Replace


Temp with Query, Decompose Conditional, etc…

 Refactoring has some obstacles

26

You might also like