Software Quality Improvements from Removing Code Smells Wes J. Lloyd July 15, 2008 Computer Science Department Colorado State University Dr. Sudipto Ghosh, co-advisor Dr. James Bieman, co-advisor
2 Background Object Modelling System (OMS) Framework Object Modelling System (OMS) Framework Java OO framework for scientific modellingJava OO framework for scientific modelling Models disaggregated into componentsModels disaggregated into components How can we help developers improve component reusability? How can we help developers improve component reusability? Reuse components across multiple models Reuse components across multiple models Support plug-and-play to try out different modelling approaches, scientific equations, etc. Support plug-and-play to try out different modelling approaches, scientific equations, etc.
3 Evapotranspiration Harmon equation Penman-Monteith equation Blaney-Criddle equation Makkink equation Hargreaves formula eddy covariance technique Potential evapotranspiration (PET)
4 Code Inspection How can we help developers improve component reusability? How can we help developers 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 practices Encourage 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 exist Many static analysis tools already exist
5 Code Smells A symptom in source code that is indicative of a problem A symptom in source code that is indicative of a problem Poor software designPoor software design Abuse of OO language featuresAbuse of OO language features Misunderstanding of OO language featuresMisunderstanding of OO language features Code smells suggest areas to refactor Code smells suggest areas to refactor Twenty-two common smells identified by Martin Fowler in Refactoring: Improving the Design of Existing Code 2 Twenty-two common smells identified by Martin Fowler in Refactoring: Improving the Design of Existing Code 2
6 Detecting Code Smells Most smells conform to one of three types of smell detection Most smells conform to one of three types of smell detection Type 0 – Manual InspectionType 0 – Manual Inspection Type 1 – Tool supported manual inspectionType 1 – Tool supported manual inspection Tool finds area of interest in code Tool finds area of interest in code Type 2 – Tool supported detection by thresholdType 2 – Tool supported detection by threshold
7 Detecting Code Smells - 2 Type 0 - manual code inspection Type 0 - manual code inspection Divergent ChangeDivergent Change Shotgun SurgeryShotgun Surgery Incomplete Class LibraryIncomplete Class Library Alternate Classes with Different InterfacesAlternate Classes with Different Interfaces
8 Detecting Code Smells - 3 Type 1 - Tools suggest locations Type 1 - Tools suggest locations based on various thresholds/criteriabased on various thresholds/criteria Programmer’s intent can not be automatically determinedProgrammer’s intent can not be automatically determined Detection is subjectiveDetection is subjective (too many) Comments (too many) Comments Refused Bequest Refused Bequest Temporary Field Temporary Field Speculative Generality Speculative Generality
9 Detecting Code Smells - 4 Type 2 - smells detectable by threshold Type 2 - smells detectable by threshold Long MethodLong Method Large ClassLarge Class Long Parameter ListLong Parameter List Lazy ClassLazy Class Duplicate CodeDuplicate Code Others...Others...
10 Research Questions Is the presence of code smells indicative of poor reusability? Is the presence of code smells indicative of poor reusability? Which code smells indicate poor reusability? Which code smells indicate poor reusability?
11 Reusability Assessment Assessment of any software quality attribute is highly subjective Assessment of any software quality attribute is highly subjective Many different reusability assessment approaches 4, 9, 10, 11, 12 Many different reusability assessment approaches 4, 9, 10, 11, 12
12 Reusability Assessment Validate metrics with Validate metrics with human evaluators assessmentshuman evaluators assessments classes designed with specific reusability attributesclasses designed with specific reusability attributes highly reused library classes (Java SDK, etc.)highly reused library classes (Java SDK, etc.) large organizational codebases with abundant reuse instanceslarge organizational codebases with abundant reuse instances empirical study. Provide class library to developers with programming tasks. Investigate attributes of reused classesempirical study. Provide class library to developers with programming tasks. Investigate attributes of reused classes
13 Initial Investigation How are traditional OO metrics affected by presence of code smells? How are traditional OO metrics affected by presence of code smells? Chidamber and Kemerer OO metrics 1Chidamber and Kemerer OO metrics 1 Does smell removal indicate quality improvements? Does smell removal indicate quality improvements?
14 OO Metrics Coupling between object classes (CBO) Coupling between object classes (CBO) Response for a class (RFC) Response for a class (RFC) Lack of Cohesion in Methods (LCOM) Lack of Cohesion in Methods (LCOM) McCabe’s Cyclomatic Complexity (CC) McCabe’s Cyclomatic Complexity (CC)
15 OO Java Models OMS Framework Models OMS Framework Models Precipitation-Runoff Modeling System (PRMS) Precipitation-Runoff Modeling System (PRMS) Hydrology Model Hydrology Model 19 classes, 1012 methods, 4370 NCSS 19 classes, 1012 methods, 4370 NCSS CEAP/J2000CEAP/J2000 Hydrology Model Hydrology Model 51 classes, 216 methods, 3519 NCSS 51 classes, 216 methods, 3519 NCSS Java Model Java Model ACRU2000ACRU2000 Univ. of Florida Hydrology Model Univ. of Florida Hydrology Model 668 classes, 3088 methods, NCSS 668 classes, 3088 methods, NCSS
16 Static Analysis Tools JavaNCSS JavaNCSS Checkstyle Checkstyle Checkstyle Simian plugin Checkstyle Simian plugin JHawk JHawk CKJM CKJM
17 Smell Detection Long Parameter List Long Parameter List # parameters >= 6# parameters >= 6 Large Classes Large Classes Class size > avg + 2 stdev NCSS (Non- Comment Source Statements) Class size > avg + 2 stdev NCSS (Non- Comment Source Statements) # Methods > avg + 2 stdev# Methods > avg + 2 stdev Long Methods Long Methods Length > avg + 2 stdev NCSSLength > avg + 2 stdev NCSS Cyclomatic Complexity > avg + 2 stdevCyclomatic Complexity > avg + 2 stdev
18 Smell Detection - 2 Duplicate Code Duplicate Code > 10 duplicate LOC> 10 duplicate LOC Lazy Classes Lazy Classes Class size < 8 NCSSClass size < 8 NCSS
19 Smell Detection Lazy Classes Duplicate Code Long Methods 2631 Large Classes Long Parameter List ACRU2000CEAP/J2000PRMSSmell
20 ACRU Lazy Classes ACRU – –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 Refactoring Does the presence of the Lazy Class Smell in ACRU2000 affect OO metrics? Does the presence of the Lazy Class Smell in ACRU2000 affect OO metrics? Refactored smallest classes first Refactored smallest classes first 2, 3, 4, 5, 6, 7 NCSS2, 3, 4, 5, 6, 7 NCSS Model produces same output with test data set after each refactoring Model produces same output with test data set after each refactoring Refactored versions saved in Subversion Refactored versions saved in Subversion Enable future analysisEnable future analysis
22 Lazy Class Refactoring line classes removed line classes removed line classes removed line classes removed line classes removed line classes removed Original Code StatementsMethodsClasses Removed # ClassesRefactoring
23 Lazy 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 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 more dense Refactored code becomes functionally more dense Class average cyclomatic complexity increasesClass average cyclomatic complexity increases Class Average response for a class increasesClass Average response for a class increases Coupling/Cohesion Coupling/Cohesion Class average coupling decreasesClass average coupling decreases Class average LCOM no significant changeClass average LCOM no significant change
25 Coupling (CBO) line classes removed line classes removed line classes removed line classes removed line classes removed line classes removed Original Code Average CBO w/o lazy classes Average CBO all classes Refactoring
26 Coupling (CBO) (60.66%) 7 line classes removed (61.92%) 6 line classes removed (36.72%) 5 line classes removed (36.07%) 4 line classes removed (34.08%) 3 line classes removed (40.58%) 2 line classes removed (39.97%) Original Code Average CBO coupled classes # classes w/ coupling Refactoring
27 Lack of Cohesion (LCOM) line classes removed line classes removed line classes removed line classes removed line classes removed line classes removed Original Code Average LCOM w/o lazy classes Average LCOM all classes Refactoring
28 Total Cyclomatic Complexity (TCC) line classes removed line classes removed line classes removed line classes removed line classes removed line classes removed Original Code Average TCC w/o lazy classes Average TCC all classes Refactoring
29 Response for a class (RFC) line classes removed line classes removed line classes removed line classes removed line classes removed line classes removed Original Code Average RFC w/o lazy classes Average RFC all classes Refactoring
30 Limitations “Data” and “process” objects instantiated dynamically at runtime based on input datafile “Data” and “process” objects instantiated dynamically at runtime based on input datafile These class uses not counted by static analysis toolsThese class uses not counted by static analysis tools Only (1) system analyzed Only (1) system analyzed Only (1) smell refactored Only (1) smell refactored Only (1) model test dataset Only (1) model test dataset
31 Future Work Data analysis Data analysis Box plots, distribution plotsBox plots, distribution plots Investigate other smells in ACRU2000 Investigate other smells in ACRU2000 Long Method, Large Class, Duplicate Code, Long Parameter ListLong Method, Large Class, Duplicate Code, Long Parameter List Investigate lazy class smell in other systems Investigate lazy class smell in other systems Investigate other smells in other systems Investigate other smells in other systems What hypotheses can be stated from exploratory studies? What hypotheses can be stated from exploratory studies? What relationships exist?What relationships exist?
32 Questions
33 References 1) Chidamber, S., Kemerer, C., A metrics suite for object oriented design. IEEE Transactions on Software Engineering, 20(6); , ) Fowler, M., et al., Refactoring: Improving the Design of Existing Code: Addison-Wesley, ) Emden, E., Moonen, L., Java Quality Assurance by Detecting Code Smells, in Proc. of the 9th Working Conference on Reverse Engineering (WCRE '02), , ) Moser, R., Sillitti, A., Abrahamsson, P., Succi, G., Does refactoring improve reusability?, in Proc. 9th International Conference on Software Reuse (ICSR 2006), Lecture Notes in Computer Science, 4039: , ) Price, M., Needham, D., Demurjian, S., Producing Reusable Object- Oriented Components: A Domain-and-Organization-Specific Perspective, in Proc. ACM Symposium on Software Reusability (SSR 2001), 41-50, ) Mens, T., Deursen, A. V., Refactoring: Emerging Trends and Open Problems, in Proc. 1st International Workshop on Refactoring: Achievements, Challenges and Effects (REFACE 2003), 1-4, 2003.
34 References - 2 1) Munro, M., Product Metrics for Automatic Identification of Bad Smell Design Problems in Java Source-Code, in Proc. 11th IEEE International Software Metrics Symposium (METRICS 2005), 15-23, ) Mantyla, M., Vanhanen, J., Lassenius, C., in Proc. 20th IEEE International Conference on Software Maintenance (ICSM '04), “Bad Smells – Humans as Code Critics”, , ) Washizaki, H., Yamamoto, H., Fukazawa, Y., A Metrics Suite for Measuring Reusability of Software Components, in Proc. of the 9th International Software Metrics Symposum (METRICS '03), ) Etzkorn, L. H., Hughes, W. E., Davis, C. G., Automated reusability quality analysis of OO legacy software, Information and Software Technology, Vol. 43, No. 5, Elsevier, 2001, pp ) Barnard, J., A new reusability metric for object-oriented software, Software Quality Journal, Vol. 7, pp , ) Dandashi, F., Rine, D., A Method for Assessing the Reusability of Object- Oriented Code Using a Validated Set of Automated Measurements, in Proc. of the ACM Symposium on Applied Computing (SAC 2002), 2002.
35 Questions