code smells, slice-based metrics and plenty of deodorant
DESCRIPTION
Code smells, slice-based metrics and plenty of deodorant. Steve Counsell Brunel University Alessandro Murgia University of Cagliari. Introduction. Code smells are areas of code which “scream out” to be refactored In theory, a code smell would be indicative of a decaying class - PowerPoint PPT PresentationTRANSCRIPT
Code smells, slice-based Code smells, slice-based metrics and plenty of metrics and plenty of
deodorantdeodorant
Steve Counsell Steve Counsell Brunel UniversityBrunel University
Alessandro Murgia Alessandro Murgia University of Cagliari University of Cagliari
Introduction
Code smells are areas of code which “scream out” to be refactored
In theory, a code smell would be indicative of a decaying class Methods becoming longerClasses becoming biggerCoupling becoming greater Cohesion deteriorating
Commonly-studied smells (cont.)
Feature Envy “A classic [code] smell is a method that seems more
interested in a class other than the one it is in. The most common focus of the envy is the data” (Fowler)
God Class (aka Key Class) A class that is deemed to have grown too large
Long Method A method that is deemed to have grown too large
Long Parameter List Method is deemed to have too many parameters
Commonly-studied smells (cont.)
Feature Envy Remedied (deodorised) by ‘moving’ the method to the
class where it is most needed God Class
No obvious remedy Extract class?
Long Method Remedied by splitting the method into at least two
Long Parameter List
What we did
Premise Smell-based classes will have low cohesion
We used Eclipse Jdeodorant
Allows smell extraction from Java systems
Slice-based metrics plug-in (Tsantalis et al.): Overlap Tightness Coverage (omitted from this analysis for clarity –
honestly!)
Overlap and Tightness (function F)
)(
|| int
Flength
SL
|Vo|
1
|Vo |
1i |SLi |
|| intSL
Tightness(F) =
Overlap(F) =
Program function SLsmallest SLlargest SLint Main() { int i; int smallest; int largest; int A[10]; for (i=0; i <10; i++) { int num; scanf(“%d”, &num); A[i] = num; } smallest = A[0]; largest=smallest; i=1; while (i <10) { if (smallest > A[i]) smallest = A[i]; if (largest < A[i]) largest = A[i]; i = i +1; } printf(“%d \n”, smallest); printf(”%d \n”, largest); }
| | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
| | | | | | | | | | |
length =19 14 16 11
Tightness = 19
11 = 0.58
2
114
11
16
11Overlap = ( +
) = 0.74
Study 1 – Evolizer
Evolizer tool
A tool for studying the evolution of OO systems Developed at the University of Zurich
300 classes/interfaces We looked at the following smells:
God class Long method Feature envy
God class
The JDeodorant tool found 18 occurrences of the God class
For each of the God classes We extracted the two slice-based metrics for
all methods in those classes: Overlap Tightness
Abstract classes/interfaces an issue
God class - problems
Constructors were often the largest methods in a God class (metrics n/a)
Single line methods with no local variables were a frequent occurrence (metrics n/a)Get and set methods
Single variable methods Use of ‘super’ to access a superclass
0
0.2
0.4
0.6
0.8
1
1.2
1 3 5 7 9 11 13 15 17 19 21 23 25 27
Method
Overlap
Tightness
Low cohesionvalues are not apparent
Tightness and Overlap (Counsell et al 2010)
N Mean Max SD Median
Tightness (fault-prone)
372 0.32 0.99 0.32 0.21
Tightness(fault-free)
150 0.38 1.00 0.37 0.28
Overlap(fault-prone)
372 0.59 1.00 0.33 0.63
Overlap (fault-free)
150 0.63 1.00 0.38 0.72
Long method
The JDeodorant tool found 9 occurrences of Long Method
For each of these methods: We extracted the same two slice-based
metrics for all methods in those classes Overlap Tightness
Long method - problems
Constructors were often the largest methods (metrics n/a)
Single line methods with no local variables were a frequent occurrence (metrics n/a)Get and set methods
Single variable methods Use of ‘super’
0
0.2
0.4
0.6
0.8
1
1.2
1 2 3 4 5 6 7 8 9
Method
Overlap
Tightness
Low cohesion Values not apparent
Feature envy
The JDeodorant tool found 11 occurrences of Feature envy
For each of these methods: We extracted the two slice-based metrics for
all methods in those classes
Feature envy - problems
………same problems as with the other two smells
0
0.2
0.4
0.6
0.8
1
1.2
1 2 3 4 5 6 7 8 9 10 11
Method
Overlap
Tightness
A hypothesis
In terms of cohesion, we would expect: Long Method to contain the most un-cohesive
methodsGod class to contain the next most un-
cohesive methodsFeature envy to be the most cohesive
A hypothesis – result mean values For Overlap:
God class most cohesiveFeature envy Long method least cohesive
For Tightness:Long method most cohesiveFeature envyGod class least cohesive
Most values of Overlap and Tightness > 0.5
Study 2 – Proprietary System
Background to Study 2
C# sub-system for a web-based, loans system providing quotes and financial information for on-line customers
We examined two versions of one of its sub-systems: an early version, comprised 401 classes later version (version n) had been the subject of a
significant refactoring effort to amalgamate, minimize as well as optimize classes
Comprised 101 classes only
Smell analysis
We focused on three smells which, arguably, should be easily identifiable from the source code: God Class Long Method Lazy Class. A class is not doing enough to justify its
existence, identified by a small number of methods and/or executable statements; it should be merged with the nearest, related class
God Class
We found many god classes to be architectural pattern-based class (Page Controller and Data Transfer Objects) Should be left alone, irrespective of the cohesion
value They also had relatively large amounts of coupling
So eradicating this smell would not only be unwarranted, but difficult (because of the coupling)
Long Method
Class ComparisonEngine.cs contained the method with the highest number of statements. Inspection of the code revealed this method to contain one large
switch statement comprising 340 statements Deodorising this method would be a major undertaking
Often ‘long’ methods are a necessary part of the implementation of an architectural pattern Leave them alone
Found evidence of these features in both start and end versions
Conclusions
Many problems with extracting the Weiser set of metrics and interpretation in OO
Use of parameters might be a better bet Use of variables in any cohesion metric is subject
to various problems Decision on eradication of smells (deodorant)
is a problemMight explain the difficulty of capturing cohesion
Thanks for listening!