You are on page 1of 10

SOEN 691C

Software Refactoring
Code Smells
Duplicated Code aka
Software Clones
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Ive seen code
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Code smells
Indications of aesthetically revolting code
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Violations of design principles and best practices
1. Duplicated Code
Is believed to be the worst code smell.
In case of a bug or a new feature, all duplicate
instances in the source code have to be found
and updated.
Inconsistent updates may lead to errors in
the program.
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Clone Detection
The clone detection community defined 4 types of clones:
Type I: code fragments are identical except for variations
in white space, layout, and comments.
Type II: code fragments are structurally and syntactically
identical except for variations in identifiers, literals,
types, in addition to Type I differences.
Type III: code fragments are copies with further
modifications. Statements can be changed, added, or
removed in addition to Type II differences.
Type IV: two or more code fragments perform the same
computation, but are implemented through different
syntactic variants.
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Clone Detection Tools
CCFinder
Google CodePro Analytix
NiCad
Deckard
CloneDr
Cpdetector
Simian
SimScan
JPlag
iClones
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Refactoring Duplicated code
1. If the duplicated code is in the same class, we
can apply Extract Method refactoring.
2. If the duplicated code is in different subclasses
of the same superclass, we can apply Extract &
Pull Up Method refactorings.
3. If the duplicated code is in different unrelated
classes
And there are several duplicated instance methods,
we can move them to a new common superclass.
If the duplicated methods are static, we can move
them to a Utility class.
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Duplicated Code - Case study I
JFreeChart 1.0.14
DateAxis::refreshTicksHorizontal()
DateAxis::refreshTicksVertical()
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Extract
Method
Duplicated Code - Case study II
JFreeChart 1.0.14
LineAndShapeRenderer::getLegendItem()
ScatterRenderer::getLegendItem()
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Extract & Pull
Up Method
Duplicated Code - Case study III
Apache Ant 1.9.0
WeblogicDeploymentTool::isRebuildRequired()
WebsphereDeploymentTool::isRebuildRequired()
Computer Science and Software Engineering
2013 Nikolaos Tsantalis
Extract & Pull
Up Method

You might also like