Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty...

34
Code smells, slice-based Code smells, slice-based metrics and plenty of metrics and plenty of deodorant deodorant Steve Counsell Steve Counsell Brunel University Brunel University Alessandro Murgia Alessandro Murgia University of University of Cagliari Cagliari

Transcript of Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty...

Page 1: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 2: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 3: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 4: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 5: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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!)

Page 6: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Overlap and Tightness (function F)

)(

|| int

Flength

SL

|Vo|

1

|Vo |

1i |SLi |

|| intSL

Tightness(F) =

Overlap(F) =

Page 7: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 8: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Tightness = 19

11 = 0.58

2

114

11

16

11Overlap = ( +

) = 0.74

Page 9: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Study 1 – Evolizer

Page 10: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 11: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 12: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 13: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 14: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 15: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.
Page 16: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.
Page 17: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.
Page 18: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 19: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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’

Page 20: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 21: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.
Page 22: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 23: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Feature envy - problems

………same problems as with the other two smells

Page 24: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 25: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.
Page 26: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 27: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 28: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Study 2 – Proprietary System

Page 29: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 30: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 31: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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)

Page 32: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 33: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

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

Page 34: Code smells, slice-based metrics and plenty of deodorant Code smells, slice-based metrics and plenty of deodorant Steve Counsell Brunel University Alessandro.

Thanks for listening!