8/11/2019 Code Smells II
1/17
SOEN 691C
Software Refactoring
Code Smells II
Long MethodLack of Polymorphism
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
2/17
2. Long method
The longera method is, the more difficultitis to understand.
Larger modulesare associated with highererror-pronenessand change-proneness.
Extractingcode fragments having a distinct
functionality into new methods can improvethe understandability.
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
3/17
What is a Long Method?
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
4/17
Slice-based cohesion
Ott and Thuss proposed slice-basedmetrics as
a means to measure the level of cohesion
within a module(method or function).
The defined cohesion metrics are computed
based on slice profiles.
L. M. Ott, and J. J. Thuss, "The Relationship between Slices and Module Cohesion," 11th
International Conference on Software Engineering(ICSE'89), pp. 198-204, 1989.
L. M. Ott, and J. J. Thuss, "Slice-based metrics for estimating cohesion," First International
Software Metrics Symposium(METRICS'93), pp. 71-81, 1993.
4
8/11/2019 Code Smells II
5/17
Slice profile
5
1 publicvoidpaintComponent(Graphics g) {
2 booleanscale = false;
3 drawWidth = available.getWidth();
4 drawHeight = available.getHeight();
5 this.scaleX= 1.0;
6 this.scaleY= 1.0;
7 if(drawWidth < this.minimumDrawWidth) {
8 this.scaleX= drawWidth / this.minimumDrawWidth;9 drawWidth = this.minimumDrawWidth;
10 scale = true;
}
11 elseif(drawWidth > this.maximumDrawWidth) {
12 this.scaleX= drawWidth / this.maximumDrawWidth;
13 drawWidth = this.maximumDrawWidth;
14 scale = true;
}
15 Rectangle2D chartArea = newRectangle2D.Double(
0.0, 0.0, drawWidth, drawHeight);
}
drawWidth scaleX scale
|
| | |
|
| | |
||
|
| | |
|
|
|
The "|" symbol in position (x, y) of the slice profile table indicates that the statement
in row xis required for the computation of the variable in column y.
8/11/2019 Code Smells II
6/17
Slice-based cohesion metrics
Tightness
6
drawWidth scaleX scale
1
2 |
3 | | |
4
5 |
67 | | |
8 |
9 |
10 |
11 | | |
12 |
13 |
14 |
15
Let SLibe the slice obtained forvariable viVO
SLintbe the intersection of SLiover allviVO
()
In this example, Tightness = 3/15 =0.2
It expresses the ratio of the numberof statements which are common toall slices over the module length
8/11/2019 Code Smells II
7/17
Slice-based cohesion metrics
Overlap
7
drawWidth scaleX scale
1
2 |
3 | | |
4
5 |
67 | | |
8 |
9 |
10 |
11 | | |
12 |
13 |
14 |
15
1
=1
In this example,
Overlap = 1/3*(3/5 + 3/6 + 3/6) = 0.53 It expresses the average ratioof
the number of statements which
are common to all slices to the size
of each slice.
8/11/2019 Code Smells II
8/17
Slice-based cohesion metrics
Coverage
8
drawWidth scaleX scale
1
2 |
3 | | |
4
5 |
67 | | |
8 |
9 |
10 |
11 | | |
12 |
13 |
14 |
15
1
()
=1
In this example,
Coverage = 1/3*(5/15 + 6/15 + 6/15)= 0.38
It expresses the average slice size
over the module length.
A high value of coverage is achieved
when the slices extend over a large
portion of the module
8/11/2019 Code Smells II
9/17
Long MethodCase Studies
Apache Ant 1.9.0 Jar::grabManifests(ResourceCollection[] rcs)
JFreeChart 1.0.14
SamplingXYLineRenderer::drawItem()
XYPolygonAnnotation::draw()
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
ExtractMethod
8/11/2019 Code Smells II
10/17
Slice profile for grabManifests()
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
11/17
3. Instanceof code smell
From Effective C++, by Scott Meyers:
Anytime you find yourself writing code of the form "if
the object is of type T1, then do something, but if it's of
type T2, then do something else" slap yourself!
In Java, it comes in the form of if/else ifchains:
If (referenceinstanceof Type)
If (reference.getClass()== Type.class)
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
12/17
InstanceofCase study
Violet 0.16
CallEdge::getPoints()
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
Replace
Conditional logicwith polymorphism
8/11/2019 Code Smells II
13/17
4. State checking
Conditional logic that simulatespolymorphism
Usually, comes in the form of if/else ifchains or
switch cases
Affects negatively the flexibilityand extensibility
Adding a new case requires to findand update
all conditionals scattered in the system.
Inconsistent updates may lead to errorsin the
program.
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
14/17
Polymorphism Alternative
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
Context
+ method() {state.method();
}
-type : int-STATE_A: int = 1
-STATE_B: int = 2
State
+method()
StateB
+method() {
}
StateA
+method() {
}
Context
+ method() {
}
-state : int-STATE_A: int = 1
-STATE_B: int = 2
doStateA();
ifstate== STATE_A
elseif
state== STATE_BdoStateB();
state
8/11/2019 Code Smells II
15/17
State checkingCase study
Violet 0.16
GraphPanel::paintComponent()
GraphPanel::mouseDragged()
GraphPanel::mouseReleased()
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
Introduce
State pattern
8/11/2019 Code Smells II
16/17
References (1)
Robert C. Martin,Agile Software Development, Principles,
Patterns, and Practices, Prentice Hall, 2002.
Nikolaos Tsantalis, and Alexander Chatzigeorgiou,
"Identification of Extract Method Refactoring Opportunities for
the Decomposition of Methods," Journal of Systems and
Software, vol. 84, no. 10, pp. 17571782, October 2011.
Danilo Silva, Ricardo Terra, and Marco Tulio Valente,
"Recommending Automated Extract Method Refactorings,"
22nd International Conference on Program Comprehension(ICPC'14), Hyderabad, India, June 2-3, 2014.
Computer Science and Software Engineering 2013 Nikolaos Tsantalis
8/11/2019 Code Smells II
17/17
References (2)
Nikolaos Tsantalis, and Alexander Chatzigeorgiou,
"Identification of Refactoring Opportunities Introducing
Polymorphism," Journal of Systems and Software, vol. 83, no.
3, pp. 391-404, March 2010.
Aikaterini Christopoulou, E.A. Giakoumakis, Vassilis E. Zafeiris,
and Vasiliki Soukara, "Automated Refactoring to the Strategy
Design Pattern," Information and Software Technology, vol. 54,
no. 11, pp. 1202-1214, November 2012.
Computer Science and Software Engineering 2013 Nikolaos Tsantalis