Share
Embed
Detecting and Managing Code Smells – Research and Practice
Slides of the technical briefing presented in ICSE 2018 on May 29, 2018.
Tushar Sharma
May 29, 2018
Transcript
76 slides
-
Detecting and Managing Code Smells – Research and Practice Tushar
Sharma [email protected] This work is funded by SENECA project under Marie-Skłodowska Curie Actions Innovative Training Networks ITN-EID. Grant agreement number 642954. -
• Code smells research • Characteristics • Examples • Causes
and effects • Smell detection – Research and practice • Various detection approaches • Demo - Designite • Learning from developing smells detection tools • Avoiding, detecting, and eradicating smells in practice • Pragmatic strategies Overview -
• Continuing Growth • All in-use software systems will grow
• Increasing Complexity • As a software system evolves, the complexity increases unless efforts are made to contain the complexity • Declining Quality • The quality of the software will decline unless it is rigorously maintained Lehman’s laws of software evolution -
Code Quality: Why to care? Impacted Quality ▪ Changeability ▪
Understandability ▪ Extensibility ▪ … Product Quality Code/Design Quality Quality issues /Smells ▪ Time to market ▪ Reliability: Impacted by poor understandability ▪ … Indicators ▪ Rigidity & Fragility ▪ Immobility & Opacity ▪ Needless complexity ▪ Needless repetition ▪ … Productivity -
What is a smell? …certain structures in the code that
suggest (sometimes they scream for) the possibility of refactoring. - Kent Beck 20 Definitions of smells: http://www.tusharma.in/smells/smellDefs.html -
• Indicator (of a deeper design problem) • Poor solution
(a suboptimal or poor solution) • Violates best practices (of the domain) • Impacts quality (make it difficult for a software system to evolve and maintain) • Recurrence Smells’ Characteristics -
Types of smells • Code smells • Implementation smells •
Architecture smells • Design smells • Test smells • Performance smells • Configuration smells • Database smells • Models smells • Usability smells • Web smells • … -
Implementation Smells public Boolean drawImage(Image image, int x1Dest, int y1Dest,
int x2Dest, int y2Dest, int x1Source, int y1Source, int x2Source, int y2Source, Color color, ImageObserver obs) -
Insufficient Modularization This smell arises when an abstraction exists that
has not been completely decomposed and a further decomposition could reduce its size, implementation complexity, or both. -
Broken Hierarchy This smell arises when a supertype and its
subtype conceptually do not share an “IS-A” relationship resulting in broken substitutability. -
Architecture Smells Assumes, UriUtil IHostFactories, IRequireHostF actories MachineKey Util IEmbeddedResourc
eRetrieval, Util, Logger, Reporting, RequiresEx, Strings -
Feature Concentration A feature concentration architecture smell occurs when a
component is realizing more than one architectural concern/feature. In other words, the component is not cohesive. Akin to LCOM (Lack of Cohesion of Methods) that is applicable to classes, we can compute LCC (Lack of Component Cohesion) to measure the component cohesion. LCC = 0.36 in the previous example -
Classification of smells Our meta-classification • Effect-based smell classification: For
instance, Mantyla’s classification include bloaters, couplers, and change preventers. • Principle-based smell classification: Classification based on the primary object- oriented design principle that the smells violate - abstraction, modularization, encapsulation, and hierarchy. • Artifact characteristics-based smell classification: Classification based on characteristics of the types such as data, interfaces, responsibility, and unnecessary complexity by W. Wake. • Granularity-based smell classification: Moha classified smells using two-level classification. -
Classification of smells Properties of an ideal classification of smells
• Exhaustive: classify all the considered smells, • Simple: classify smells within the scope and granularity effortlessly • Consistent: produce a consistent classification even if it carried out by different people, and • Coherent: produce clearly distinguishable categories without overlaps. -
Causes of smells Actor-based classification assigns the responsibility of the
causes to specific actor(s). Role C1 C2 C3 … Manager ☑ Technical lead ☑ ☑ Developer ☑ -
Effects of smells Effects Artifacts Processes People Morale and motivation
Maintainability Effort/Cost Reliability Change proneness Testability Performance Productivity -
Metrics-based smell detection < > <!> Code (or source artifact)
Source model Smells Metrics -
Heuristics-based smell detection < > <!> Code (or source artifact)
Source model Smells Metrics -
History-based smell detection < > <!> Code (or source artifact)
Smells History information Detection model < > < > < > -
Machine learning-based smell detection < > <!> Code (or source
artifact) Smells Machine learning algorithm Existing examples Source model Populated model f(x) f(x) f(x) f(x) < > <!> < > -
Optimization-based smell detection < > <!> Code (or source artifact)
Source model Smells Metrics Optimization algorithm Existing examples < > <!> < > -
Source code (or source artifact) Historical information Mathema -tical model
Existing examples Populated model Machine learning algorithm Detection model Source code model Rules/Heuristics Metrics Optimization algorithm Smells Existing examples 1.1 1.2 1.3 2.1 2.2 2.3 3.1 3.2 3.3 4.1 4.2 4.3 4.4 4.5 4.6 5.1 5.2 5.3 5.4 5.5 2.4 Metric-based Rule/Heuristic-based History-based Machine learning-based Optimization-based 1.s 2.s 4.s 5.s 3.s <!> < > < > < > < > < > <!> < > -
• Relatively easier to implement • Thresholds selection • Not
all smells can be detected Expands the horizon of metrics-based detection Very few smells show evolutionary characteristics • Depend heavily on training data • Still unknown whether algorithms can scale to the large number of known smells Depends on metric data and corresponding thresholds -
Method #Studies Target language #Smells Metrics-based 19 Java – 13,
JS – 2, C/C++ - 2, Others - 2 Rules/Heuristics-based 15 Java – 7, C/C++ - 2, Others - 6 History-based 2 Java - 2 Machine learning-based 6 Java - 6 Optimization-based 4 Java – 3, XML - 1 -
Designite is a software design quality assessment tool for C#.
Key features • Detects 7 architecture smells • Detects 19 design smells • Detects 11 implementation smells • Computes various object-oriented code metrics • Provides smell trend analysis of Git repositories • Many visualization aids including smell treemap, and smell sunburst • Detects code duplication • Allows customization of analysis • Smart tracking of detected smells http://www.designite-tools.com Academic license is free! -
Metrics-based #Studies: 19 Target language: Java – 13, JS –
2, C/C++ - 2, Others - 2 Rules-based #Studies: 15 Target language: Java – 7, C/C++ - 2, Others - 6 History-based #Studies: 2 Target language: Java – 2 Machine learning-based #Studies: 6 Target language: Java – 6 #Studies: 4 Target language: Java – 3, XML - 1 Optimization-based Smells detection methods at a glance -
Opportunities 1. False positives and lack of context • Dependence
on metrics thresholds • Existing methods do not consider the context. It is not easy to define, specify, and capture context. -
Opportunities 2. Limited detection support for known smells 0 2
4 6 8 10 12 14 1 2 3 4 5 6 7 8 10 11 13 17 30 NO OF STUDIES NO OF SMELLS DETECTED -
Opportunities 2. Limited detection support for known smells Target languages
for the 46 smell detection methods • Java – 31 • Models – 6 • C – 2 • C++ - 2 • JavaScript – 2 • C#, XML, REST APIs – 1 each -
Opportunities 3. Inconsistent smell definitions and detection methods • Based
on their description and interpretation, their detection methods also differ significantly and they detect smells inconsistently. • Metrics tools show inconsistent results even for well- known metrics such as LCOM, CC, and LOC. -
Opportunities 4. Impact of smells on productivity Smells Maintainability Productivity
Impacts (established) Impacts (believed but not yet established rigorously) -
Tools Puppeteer Configuration smell detection tool for Puppet code Software
design quality assessment tool for C# DbDeo Database schema smell detection tool Augur Change impact analysis tool for C# https://github.com/tushartushar/DbDeo http://www.designite-tools.com https://github.com/tushartushar/Puppeteer -
Learning Make sure the availability of artifacts and their documentation
A tool that lives only on the papers of its creator, is useless. -
Design consideration Parsing mechanism Each parsing mechanism comes with its
benefits and challenges AST library • License • Features • Cost • Community support • Future proof-ness Selection criteria -
Design consideration Plug-in or independent application • Degree of interaction
• Target user • Usage workflow • Value for the time -
Learning Refactor often Console application • Duplicate the whole codebase
or refactor out the backend Extensibility • Smell detection logic must be extensible. i.e., new rules can be added without any change in source code analysis logic and user interface Architecture refactoring is expensive but effective and highly fruitful! -
Learning Customization Every user is different! Give them the freedom
by easy customization. -
Awareness Does the development team is well aware about: •
Smells and resultant technical debt • Their impact on their project • Code quality, clean code practices Train • Training • Books • Workshops/conferences • … -
Processes Employ pragmatic processes to bring discipline Review process •
Code reviews Architecture governance • Adherence to collection of guidelines and controls People care about things that you measure and reward! -
Detecting and refactoring smells: strategies 1. Identify and track •
Detect smells using tools or by manual review • Track them -
Detecting and refactoring smells: strategies 2. Prioritize • Not all
smells are same! • Prioritize based on factors such as potential impact of the smell • It’s ok to live with some smells. There is no software with zero smells. -
Detecting and refactoring smells: strategies 3. Refactoring installments • Amortize
small number of smells’ refactoring into each iteration -
Detecting and refactoring smells: strategies 4. Follow the ‘Boy’s scout
rule’ • Leave the class/file much more cleaner than you got it -
Detecting and refactoring smells: strategies 5. Motivate • Motivate and
reward people for maintaining quality People care about things that you measure and reward! -
Detecting and refactoring smells: strategies 6. Periodically, aim for large-scale
refactoring • Architecture refactoring – often high gain, high risk • Mitigate the risks by planning and communication -
Detecting and refactoring smells: strategies 7. Refactor, except • Prototype
(that’s not going to be converted into a product) • Product near end of life • Planned migration