35
Software Quality Software Quality Improvements from Improvements from Removing Code Smells Removing Code Smells Wes J. Lloyd Wes J. Lloyd July 15, 2008 July 15, 2008 Computer Science Department Computer Science Department Colorado State University Colorado State University Dr. Sudipto Ghosh, co-advisor Dr. Sudipto Ghosh, co-advisor Dr. James Bieman, co-advisor Dr. James Bieman, co-advisor

Software Quality Improvements from Removing Code Smells Wes J. Lloyd July 15, 2008 Computer Science Department Colorado State University Dr. Sudipto Ghosh,

Embed Size (px)

Citation preview

Software Quality Software Quality Improvements from Improvements from

Removing Code SmellsRemoving Code Smells

Wes J. LloydWes J. LloydJuly 15, 2008July 15, 2008

Computer Science DepartmentComputer Science DepartmentColorado State UniversityColorado State University

Dr. Sudipto Ghosh, co-advisorDr. Sudipto Ghosh, co-advisorDr. James Bieman, co-advisorDr. James Bieman, co-advisor

2

BackgroundBackground

Object Modelling System (OMS) Object Modelling System (OMS) FrameworkFramework• Java OO framework for scientific Java OO framework for scientific

modellingmodelling• Models disaggregated into componentsModels disaggregated into components

How can we help developers How can we help developers improve component reusability?improve component reusability?

Reuse components across multiple modelsReuse components across multiple models Support plug-and-play to try out different Support plug-and-play to try out different

modelling approaches, scientific equations, modelling approaches, scientific equations, etc.etc.

3

EvapotranspirationEvapotranspiration

Harmon equation

Penman-Monteith equation

Blaney-Criddle equation

Makkink equation

Hargreaves formula

eddy covariance technique

Potential evapotranspiration (PET)

4

Code InspectionCode Inspection

How can we help developers How can we help developers improve component reusability?improve component reusability?• OMS Code Inspection plug-in toolOMS Code Inspection plug-in tool

Inspect “quality” of components Inspect “quality” of components Encourage design best practicesEncourage design best practices

• But what should the tool look for?But what should the tool look for? What sort of problems?What sort of problems? How to quantify reusability?How to quantify reusability? Many static analysis tools already existMany static analysis tools already exist

5

Code SmellsCode Smells A symptom in source code that is A symptom in source code that is

indicative of a problemindicative of a problem• Poor software designPoor software design• Abuse of OO language featuresAbuse of OO language features• Misunderstanding of OO language Misunderstanding of OO language

featuresfeatures Code smells suggest areas to refactorCode smells suggest areas to refactor Twenty-two common smells identified by Twenty-two common smells identified by

Martin Fowler in Martin Fowler in Refactoring: Improving Refactoring: Improving the Design of Existing Codethe Design of Existing Code 22

6

Detecting Code SmellsDetecting Code Smells Most smells conform to one of three types Most smells conform to one of three types

of smell detectionof smell detection•Type 0 – Manual InspectionType 0 – Manual Inspection•Type 1 – Tool supported manual Type 1 – Tool supported manual

inspectioninspection Tool finds area of interest in codeTool finds area of interest in code

•Type 2 – Tool supported detection Type 2 – Tool supported detection by threshold by threshold

7

Detecting Code Smells - 2Detecting Code Smells - 2

Type 0 - Type 0 - manual code inspection manual code inspection • Divergent ChangeDivergent Change• Shotgun SurgeryShotgun Surgery• Incomplete Class LibraryIncomplete Class Library• Alternate Classes with Different Alternate Classes with Different

InterfacesInterfaces

8

Detecting Code Smells - 3Detecting Code Smells - 3 Type 1 - Tools suggest locationsType 1 - Tools suggest locations

• based on various thresholds/criteriabased on various thresholds/criteria• Programmer’s intent can not be Programmer’s intent can not be

automatically determinedautomatically determined• Detection is subjectiveDetection is subjective

(too many) Comments(too many) Comments Refused BequestRefused Bequest Temporary FieldTemporary Field Speculative GeneralitySpeculative Generality

9

Detecting Code Smells - 4Detecting Code Smells - 4

Type 2 - smells detectable by Type 2 - smells detectable by thresholdthreshold• Long MethodLong Method• Large ClassLarge Class• Long Parameter ListLong Parameter List• Lazy ClassLazy Class• Duplicate CodeDuplicate Code• Others...Others...

10

Research QuestionsResearch Questions

Is the presence of code smells Is the presence of code smells indicative of poor reusability?indicative of poor reusability?

Which code smells indicate poor Which code smells indicate poor reusability?reusability?

11

Reusability AssessmentReusability Assessment

Assessment of any software quality Assessment of any software quality attribute is highly subjectiveattribute is highly subjective

Many different reusability Many different reusability assessment approaches assessment approaches 4, 9, 10, 11, 124, 9, 10, 11, 12

12

Reusability AssessmentReusability Assessment

Validate metrics withValidate metrics with• human evaluators assessmentshuman evaluators assessments• classes designed with specific classes designed with specific

reusability attributesreusability attributes• highly reused library classes (Java SDK, highly reused library classes (Java SDK,

etc.) etc.) • large organizational codebases with large organizational codebases with

abundant reuse instancesabundant reuse instances• empirical study. Provide class library to empirical study. Provide class library to

developers with programming tasks. developers with programming tasks. Investigate attributes of reused classesInvestigate attributes of reused classes

13

Initial InvestigationInitial Investigation

How are traditional OO metrics How are traditional OO metrics affected by presence of code smells?affected by presence of code smells?• Chidamber and Kemerer OO metrics Chidamber and Kemerer OO metrics 11

Does smell removal indicate quality Does smell removal indicate quality improvements?improvements?

14

OO MetricsOO Metrics

Coupling between object classes Coupling between object classes (CBO)(CBO)

Response for a class (RFC)Response for a class (RFC) Lack of Cohesion in Methods Lack of Cohesion in Methods

(LCOM)(LCOM) McCabe’s Cyclomatic Complexity McCabe’s Cyclomatic Complexity

(CC)(CC)

15

OO Java ModelsOO Java Models OMS Framework ModelsOMS Framework Models

• Precipitation-Runoff Modeling System Precipitation-Runoff Modeling System (PRMS)(PRMS)

Hydrology ModelHydrology Model 19 classes, 1012 methods, 4370 NCSS19 classes, 1012 methods, 4370 NCSS

• CEAP/J2000 CEAP/J2000 Hydrology ModelHydrology Model 51 classes, 216 methods, 3519 NCSS51 classes, 216 methods, 3519 NCSS

Java ModelJava Model• ACRU2000ACRU2000

Univ. of Florida Hydrology ModelUniv. of Florida Hydrology Model 668 classes, 3088 methods, 20708 NCSS668 classes, 3088 methods, 20708 NCSS

16

Static Analysis ToolsStatic Analysis Tools

JavaNCSSJavaNCSS• http://www.kclee.de/clemens/java/javancss/http://www.kclee.de/clemens/java/javancss/

CheckstyleCheckstyle• http://checkstyle.sourceforge.net/http://checkstyle.sourceforge.net/

Checkstyle Simian pluginCheckstyle Simian plugin• http://www.redhillconsulting.com.au/products/simian/http://www.redhillconsulting.com.au/products/simian/

JHawkJHawk• http://www.virtualmachinery.com/jhawkprod.htmhttp://www.virtualmachinery.com/jhawkprod.htm

CKJMCKJM• http://www.spinellis.gr/sw/ckjm/http://www.spinellis.gr/sw/ckjm/

17

Smell DetectionSmell Detection

Long Parameter ListLong Parameter List• # parameters >= 6# parameters >= 6

Large ClassesLarge Classes• Class size > avg + 2 stdev NCSS (Non-Class size > avg + 2 stdev NCSS (Non-

Comment Source Statements)Comment Source Statements)• # Methods > avg + 2 stdev# Methods > avg + 2 stdev

Long MethodsLong Methods• Length > avg + 2 stdev NCSSLength > avg + 2 stdev NCSS• Cyclomatic Complexity > avg + 2 stdevCyclomatic Complexity > avg + 2 stdev

18

Smell Detection - 2Smell Detection - 2

Duplicate CodeDuplicate Code• > 10 duplicate LOC> 10 duplicate LOC

Lazy ClassesLazy Classes• Class size < 8 NCSSClass size < 8 NCSS

19

Smell Detection - 3Smell Detection - 3

34160Lazy Classes

1332810Duplicate Code

63165Long Methods

2631Large Classes

27811Long Parameter List

ACRU2000CEAP/J2000PRMSSmell

20

ACRU2000 - Lazy ClassesACRU2000 - Lazy Classes

• ACRU2000- 341– 51% of classes < 8 NCSS (341 classes)– 54% of classes < 4 methods (364 classes)– 61% of classes

w/ avg method length <= 2 NCSS (409 classes)

21

Lazy Class RefactoringLazy Class Refactoring

Does the presence of the Lazy Class Does the presence of the Lazy Class Smell in ACRU2000 affect OO Smell in ACRU2000 affect OO metrics?metrics?

Refactored smallest classes firstRefactored smallest classes first• 2, 3, 4, 5, 6, 7 NCSS2, 3, 4, 5, 6, 7 NCSS

Model produces same output with Model produces same output with test data set after each refactoringtest data set after each refactoring

Refactored versions saved in Refactored versions saved in SubversionSubversion• Enable future analysisEnable future analysis

22

Lazy Class Refactoring - 2Lazy Class Refactoring - 2

181732230113337 line classes removed

1822622542363446 line classes removed

191832963165805 line classes removed

192252980325964 line classes removed

192723044306283 line classes removed

201083078106582 line classes removed

2013830880668Original Code

StatementsMethodsClasses Removed

# ClassesRefactoring

23

Lazy Class CausesLazy Class Causes

13Unused Class

3Class with little code

4Empty child class (no code)

7Non-abstract class /w only abstract methods

8Empty class used for tracking type

10Abstract class with little/no code

16Data class: not used

27Exception Class with no functionality

249Data class: extension instead of instantiation

24

Summary of ResultsSummary of Results # of classes reduced ~ 50% # of classes reduced ~ 50% # of methods decreased ~ 28%# of methods decreased ~ 28% # of statements decreases ~ 10%# of statements decreases ~ 10% Refactored code becomes functionally Refactored code becomes functionally

more densemore dense• Class average cyclomatic complexity increasesClass average cyclomatic complexity increases• Class Average response for a class increasesClass Average response for a class increases

Coupling/CohesionCoupling/Cohesion• Class average coupling decreasesClass average coupling decreases• Class average LCOM no significant changeClass average LCOM no significant change

25

Coupling (CBO)Coupling (CBO)

3.393.397 line classes removed

3.023.346 line classes removed

3.021.985 line classes removed

3.001.924 line classes removed

2.991.823 line classes removed

3.652.422 line classes removed

3.652.38Original Code

Average CBO w/o lazy classes

Average CBOall classes

Refactoring

26

Coupling (CBO) - 2Coupling (CBO) - 2

5.58202 (60.66%)7 line classes removed

5.39213 (61.92%)6 line classes removed

5.39213 (36.72%)5 line classes removed

5.32215 (36.07%)4 line classes removed

5.33214 (34.08%)3 line classes removed

5.96267 (40.58%)2 line classes removed

5.96267 (39.97%)Original Code

Average CBO coupled classes

# classes w/ couplingRefactoring

27

Lack of Cohesion (LCOM)Lack of Cohesion (LCOM)

0.300.37 line classes removed

0.300.296 line classes removed

0.300.185 line classes removed

0.290.174 line classes removed

0.290.163 line classes removed

0.290.152 line classes removed

0.290.15Original Code

Average LCOM w/o lazy classes

Average LCOMall classes

Refactoring

28

Total Cyclomatic Complexity (TCC)Total Cyclomatic Complexity (TCC)

13.9213.927 line classes removed

13.1413.536 line classes removed

13.149.255 line classes removed

13.079.034 line classes removed

13.058.663 line classes removed

13.058.312 line classes removed

13.058.21Original Code

Average TCC w/o lazy classes

Average TCCall classes

Refactoring

29

Response for a class (RFC)Response for a class (RFC)

19.419.47 line classes removed

18.7418.876 line classes removed

18.7412.425 line classes removed

18.6112.094 line classes removed

18.5811.563 line classes removed

18.6311.12 line classes removed

18.6310.95Original Code

Average RFC w/o lazy classes

Average RFCall classes

Refactoring

30

LimitationsLimitations

““Data” and “process” objects Data” and “process” objects instantiated dynamically at runtime instantiated dynamically at runtime based on input datafilebased on input datafile• These class uses not counted by static These class uses not counted by static

analysis toolsanalysis tools Only (1) system analyzedOnly (1) system analyzed Only (1) smell refactoredOnly (1) smell refactored Only (1) model test dataset Only (1) model test dataset

31

Future WorkFuture Work Data analysisData analysis

• Box plots, distribution plotsBox plots, distribution plots Investigate other smells in ACRU2000Investigate other smells in ACRU2000

• Long Method, Large Class, Duplicate Code, Long Method, Large Class, Duplicate Code, Long Parameter ListLong Parameter List

Investigate lazy class smell in other Investigate lazy class smell in other systemssystems

Investigate other smells in other systemsInvestigate other smells in other systems What hypotheses can be stated from What hypotheses can be stated from

exploratory studies?exploratory studies?• What relationships exist?What relationships exist?

32

QuestionsQuestions

33

ReferencesReferences1)1) Chidamber, S., Kemerer, C., A metrics suite for object oriented Chidamber, S., Kemerer, C., A metrics suite for object oriented

design. IEEE Transactions on Software Engineering, 20(6); design. IEEE Transactions on Software Engineering, 20(6); 476-493, 1994.476-493, 1994.

2)2) Fowler, M., et al., Refactoring: Improving the Design of Fowler, M., et al., Refactoring: Improving the Design of Existing Code: Addison-Wesley, 1999.Existing Code: Addison-Wesley, 1999.

3)3) Emden, E., Moonen, L., Java Quality Assurance by Detecting Emden, E., Moonen, L., Java Quality Assurance by Detecting Code Smells, in Proc. of the 9th Working Conference on Code Smells, in Proc. of the 9th Working Conference on Reverse Engineering (WCRE '02), 97-106, 2002.Reverse Engineering (WCRE '02), 97-106, 2002.

4)4) Moser, R., Sillitti, A., Abrahamsson, P., Succi, G., Does Moser, R., Sillitti, A., Abrahamsson, P., Succi, G., Does refactoring improve reusability?, in Proc. 9th International refactoring improve reusability?, in Proc. 9th International Conference on Software Reuse (ICSR 2006), Lecture Notes in Conference on Software Reuse (ICSR 2006), Lecture Notes in Computer Science, 4039: 287-297, 2006.Computer Science, 4039: 287-297, 2006.

5)5) Price, M., Needham, D., Demurjian, S., Producing Reusable Price, M., Needham, D., Demurjian, S., Producing Reusable Object-Oriented Components: A Domain-and-Organization-Object-Oriented Components: A Domain-and-Organization-Specific Perspective, in Proc. ACM Symposium on Software Specific Perspective, in Proc. ACM Symposium on Software Reusability (SSR 2001), 41-50, 2001.Reusability (SSR 2001), 41-50, 2001.

6)6) Mens, T., Deursen, A. V., Refactoring: Emerging Trends and Mens, T., Deursen, A. V., Refactoring: Emerging Trends and Open Problems, in Proc. 1st International Workshop on Open Problems, in Proc. 1st International Workshop on Refactoring: Achievements, Challenges and Effects (REFACE Refactoring: Achievements, Challenges and Effects (REFACE 2003), 1-4, 2003.2003), 1-4, 2003.

34

References - 2References - 21)1) Munro, M., Product Metrics for Automatic Identification of Bad Munro, M., Product Metrics for Automatic Identification of Bad

Smell Design Problems in Java Source-Code, in Proc. 11th IEEE Smell Design Problems in Java Source-Code, in Proc. 11th IEEE International Software Metrics Symposium (METRICS 2005), 15-International Software Metrics Symposium (METRICS 2005), 15-23, 2005.23, 2005.

2)2) Mantyla, M., Vanhanen, J., Lassenius, C., in Proc. 20th IEEE Mantyla, M., Vanhanen, J., Lassenius, C., in Proc. 20th IEEE International Conference on Software Maintenance (ICSM '04), International Conference on Software Maintenance (ICSM '04), “Bad Smells – Humans as Code Critics”, 399-408, 2004.“Bad Smells – Humans as Code Critics”, 399-408, 2004.

3)3) Washizaki, H., Yamamoto, H., Fukazawa, Y., A Metrics Suite for Washizaki, H., Yamamoto, H., Fukazawa, Y., A Metrics Suite for Measuring Reusability of Software Components, in Proc. of the Measuring Reusability of Software Components, in Proc. of the 9th International Software Metrics Symposum (METRICS '03), 9th International Software Metrics Symposum (METRICS '03), 2003.2003.

4)4) Etzkorn, L. H., Hughes, W. E., Davis, C. G., Automated Etzkorn, L. H., Hughes, W. E., Davis, C. G., Automated reusability quality analysis of OO legacy software, Information reusability quality analysis of OO legacy software, Information and Software Technology, Vol. 43, No. 5, Elsevier, 2001, pp. and Software Technology, Vol. 43, No. 5, Elsevier, 2001, pp. 295-308.295-308.

5)5) Barnard, J., A new reusability metric for object-oriented Barnard, J., A new reusability metric for object-oriented software, Software Quality Journal, Vol. 7, pp. 33-50, 1998.software, Software Quality Journal, Vol. 7, pp. 33-50, 1998.

6)6) Dandashi, F., Rine, D., A Method for Assessing the Reusability of Dandashi, F., Rine, D., A Method for Assessing the Reusability of Object-Oriented Code Using a Validated Set of Automated Object-Oriented Code Using a Validated Set of Automated Measurements, in Proc. of the ACM Symposium on Applied Measurements, in Proc. of the ACM Symposium on Applied Computing (SAC 2002), 2002.Computing (SAC 2002), 2002.

35

QuestionsQuestions