23
1 Predicting Classes in Need of R Predicting Classes in Need of R efactoring – An Application of efactoring – An Application of Static Metrics Static Metrics Liming Zhao Jane Hayes Liming Zhao Jane Hayes [email protected] [email protected] [email protected] [email protected] 23 September 2006 23 September 2006 2006 International PROMISE Workshop 2006 International PROMISE Workshop

1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes 23 September 2006

Embed Size (px)

DESCRIPTION

3 Motivation Cost of software maintenance Cost of software maintenance Refactoring Refactoring Importance of refactoring planning Importance of refactoring planning Need for improving multiple classes Need for improving multiple classes Deadlines, human resources, budgets Deadlines, human resources, budgets

Citation preview

Page 1: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

11

Predicting Classes in Need of RefactorPredicting Classes in Need of Refactoring – An Application of Static Metricsing – An Application of Static MetricsLiming Zhao Jane HayesLiming Zhao Jane [email protected]@uky.edu [email protected]@cs.uky.edu

23 September 2006 23 September 2006 2006 International PROMISE Workshop 2006 International PROMISE Workshop

Page 2: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

22

Predicting Classes in Need of Predicting Classes in Need of Refactoring: Refactoring:

An Application of Static MetricsAn Application of Static Metrics Motivation Motivation Related workRelated work Design of the toolDesign of the tool Study designStudy design Study resultsStudy results Conclusions and future workConclusions and future work

Page 3: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

33

Motivation Motivation Cost of software maintenanceCost of software maintenance RefactoringRefactoring Importance of refactoring planningImportance of refactoring planning

Need for improving multiple classesNeed for improving multiple classes Deadlines, human resources, budgetsDeadlines, human resources, budgets

Page 4: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

44

Refactoring stepsRefactoring steps Identify code segments Identify code segments Evaluate possible costs and benefits Evaluate possible costs and benefits Develop refactoring plan Develop refactoring plan Apply the refactoringsApply the refactorings

Page 5: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

55

Why use a class-based Why use a class-based approach? approach?

Common programmer(s)Common programmer(s) Cohesive codeCohesive code Learning effect Learning effect Comprehension overheadComprehension overhead

Page 6: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

66

Code Repository Analysis

Complexity Size Coupling History

Maintainability Prediction Model

Maintainability Measurement & Prediction Component (MMPC)

Cost-Benefits Estimation

Candidate Classes

Refactoring Planning

Cost & Benefit Estimation Component (CBEC)

Proposed approachProposed approach

Visualizer

Prioritized Class List

Managers

Cost Benefits

Page 7: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

77

Design of the toolDesign of the tool

Size

Maintainability Prediction Model

Complexity

Coupling

Refactoring Planning

Candidate Classes

History

Code Repository Analysis

Page 8: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

88

Related work: Maintainability Related work: Maintainability assessmentassessment

OO metrics – Chidamber and Kemerer [1OO metrics – Chidamber and Kemerer [1994]994] MI – Welker [1995] MI – Welker [1995] PM and MP – Hayes et al. [2004] PM and MP – Hayes et al. [2004] Assessment of UML artifacts - Hassan et Assessment of UML artifacts - Hassan et al. [2005] al. [2005] RDC ratio – Hayes and Zhao [2005]RDC ratio – Hayes and Zhao [2005]

Page 9: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

99

Related work: RefactoringRelated work: Refactoring Preconditions – Opdyke [1992] Preconditions – Opdyke [1992] Bad smells – Fowler et al. [1999] Bad smells – Fowler et al. [1999] Detecting bad smells – Mens et al. [2003] Detecting bad smells – Mens et al. [2003] Member similarity – Simon et al. [2001] Member similarity – Simon et al. [2001]

Page 10: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1010

Metrics examinedMetrics examined Halstead metricsHalstead metrics Cyclomatic complexity Cyclomatic complexity Weighted method per classWeighted method per class Maintainability indexMaintainability index

Page 11: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1111

Class-based rankClass-based rank Cost concernCost concern Priority class listPriority class list Individual rankIndividual rank Comprehensive rankComprehensive rank

Page 12: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1212

Code repository analysis and Code repository analysis and metrics collectionmetrics collection

Code repository analyzerCode repository analyzer Java front endJava front end

Abstract syntax tree Abstract syntax tree MetricsMetrics

ComplexityComplexity Size Size Other Other

Weighted maintainability rank (WMR)Weighted maintainability rank (WMR)

Page 13: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1313

Initial validation on student Initial validation on student projectsprojects

Objective of the studyObjective of the study Measures examinedMeasures examined

Tool’s performanceTool’s performance Programmers’ decisionsProgrammers’ decisions Tool vs. programmerTool vs. programmer Time spentTime spent

Page 14: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1414

Study designStudy design Participants: Graduate students in Participants: Graduate students in

computer sciencecomputer science Subjects: Java source code (20 Subjects: Java source code (20

classes) from software engineering classes) from software engineering class projectclass project

Pre-reading: Questionnaire about Pre-reading: Questionnaire about background, smell-list, etc.background, smell-list, etc.

Page 15: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1515

Study design: procedureStudy design: procedure Procedure instructed: Procedure instructed:

Fill in the questionnaire and read about the smell lFill in the questionnaire and read about the smell listist Read the code and look for “bad smells”; select Read the code and look for “bad smells”; select a smell from a provided list or fill in a problem obsa smell from a provided list or fill in a problem observed that was not in the listerved that was not in the list Find the class with the most serious problems that Find the class with the most serious problems that should be refactored first should be refactored first Record the time spent reviewing/reading each claRecord the time spent reviewing/reading each classss

Page 16: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1616

Study Result: Compare Study Result: Compare reviewers’ selection with tool’s reviewers’ selection with tool’s

selection selection PrioritPriorityy

rv1rv1 rv2rv2 rv3rv3 rv4rv4 rv5rv5 rv6rv6 TooTooll

#1#1 I1I1 AA GG AA AA GG AA#2#2 FF WW TT P P EE H H LOLO#3#3 WW LOLO AA SS GG HH#4#4 GG VV LL WW HH PP#5#5 PP SS M M LOLO FF#6#6 PP LL Legend of class name abbreviations: A - AnimationScreen, F-FileOutPut, Legend of class name abbreviations: A - AnimationScreen, F-FileOutPut,

L-LoginScreen, P- PitchAnalyzer, S- SessionReport, E- ErrorWindow, G- L-LoginScreen, P- PitchAnalyzer, S- SessionReport, E- ErrorWindow, G-Graphic, LO - LoudnessAnalyzer, R- RegistrationScreen, T - TestScreen, Graphic, LO - LoudnessAnalyzer, R- RegistrationScreen, T - TestScreen, F- FileInputHandler, H - HistoryReport, M - MenuScreen, W- Welcome, I1 F- FileInputHandler, H - HistoryReport, M - MenuScreen, W- Welcome, I1 – ImagePanel1, I2 – ImagePanel2, L – LoginScreen – ImagePanel1, I2 – ImagePanel2, L – LoginScreen

Page 17: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1717

Class AnimationClass Animation Noted by 67% of the reviewers and the tNoted by 67% of the reviewers and the tool (ranked 1ool (ranked 1stst by 50% of the reviewers a by 50% of the reviewers and the tool)nd the tool) Largest halstead_effort and smallest MILargest halstead_effort and smallest MI Long method per class (LMC) and ComplLong method per class (LMC) and Complex method per class (CMC) above averagex method per class (CMC) above average (Slide 43) e (Slide 43)

Page 18: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1818

Metrics of class animationMetrics of class animationHalstead-Halstead-LL

Halstead-Halstead-vv

Halstead-Halstead-EE

MIMI LMCLMC CMCCMC

AnimatioAnimationn

491491 191191 74762 74762 (max)(max)

37.437.499

55 22

AverageAverage 203.15 203.15 1894.7 1894.7 19353.3 19353.3 62.662.633

3.753.75 0.650.65

Page 19: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

1919

Study resultsStudy results Tool and half of reviewers noted Tool and half of reviewers noted

class Animation as worstclass Animation as worst Reviewers spent a significant amount Reviewers spent a significant amount

of time (1-3 hours)of time (1-3 hours) More “easy” problems found (90%)More “easy” problems found (90%) Reviewers looked for different smells Reviewers looked for different smells

(28% not in list) (28% not in list)

Page 20: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

2020

ConclusionsConclusions Complexity and size are among the Complexity and size are among the

major factors making comprehension major factors making comprehension hardhard

Reviewers are looking for different Reviewers are looking for different problems problems

Code reviewing is time-consuming Code reviewing is time-consuming Automation can help consistency, Automation can help consistency,

efficiency, and effectivenessefficiency, and effectiveness

Page 21: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

2121

Future workFuture work Predict the possible cost of the Predict the possible cost of the

refactoring and its impact on code refactoring and its impact on code maintainabilitymaintainability

Use metrics from evolution historyUse metrics from evolution history Use metrics reflecting inter-class Use metrics reflecting inter-class

relationshipsrelationships

Page 22: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

2222

AcknowledgementsAcknowledgements Thanks to Edison Design Group for Thanks to Edison Design Group for

providing JFEproviding JFE Thanks to the graduate student Thanks to the graduate student

volunteers for participating in the volunteers for participating in the study study

Page 23: 1 Predicting Classes in Need of Refactoring – An Application of Static Metrics Liming Zhao Jane Hayes  23 September 2006

2323

Questions?Questions?