Slide 1

Slide 1 text

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.

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

• 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

Slide 4

Slide 4 text

• 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

Slide 5

Slide 5 text

Evolution of JUnit 3.7 4.0 4.5 4.11

Slide 6

Slide 6 text

Which one is easy to work with?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

• 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

Slide 13

Slide 13 text

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 • …

Slide 14

Slide 14 text

Smells: Types http://www.tusharma.in/smells/

Slide 15

Slide 15 text

Let us see some examples

Slide 16

Slide 16 text

Granularity of code smells - Implementation - Design - Architecture

Slide 17

Slide 17 text

Implementation smells

Slide 18

Slide 18 text

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)

Slide 19

Slide 19 text

Implementation Smells if (sum > 13.05689) { //do something }

Slide 20

Slide 20 text

Design Smells

Slide 21

Slide 21 text

Example

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

Example

Slide 24

Slide 24 text

Broken Hierarchy This smell arises when a supertype and its subtype conceptually do not share an “IS-A” relationship resulting in broken substitutability.

Slide 25

Slide 25 text

Architecture Smells

Slide 26

Slide 26 text

Architecture Smells Assumes, UriUtil IHostFactories, IRequireHostF actories MachineKey Util IEmbeddedResourc eRetrieval, Util, Logger, Reporting, RequiresEx, Strings

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Dense Structure Average degree = # ∗|&| |'|

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Causes of smells

Slide 32

Slide 32 text

Causes of smells Actor-based classification assigns the responsibility of the causes to specific actor(s). Role C1 C2 C3 … Manager ☑ Technical lead ☑ ☑ Developer ☑

Slide 33

Slide 33 text

Effects of smells Effects Artifacts Processes People Morale and motivation Maintainability Effort/Cost Reliability Change proneness Testability Performance Productivity

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Code smell detection – Research and practice

Slide 37

Slide 37 text

Metrics-based smell detection < > Code (or source artifact) Source model Smells Metrics

Slide 38

Slide 38 text

Heuristics-based smell detection < > Code (or source artifact) Source model Smells Metrics

Slide 39

Slide 39 text

History-based smell detection < > Code (or source artifact) Smells History information Detection model < > < > < >

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Optimization-based smell detection < > Code (or source artifact) Source model Smells Metrics Optimization algorithm Existing examples < > < >

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

• 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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Demo

Slide 46

Slide 46 text

Demo http://www.designite-tools.com

Slide 47

Slide 47 text

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!

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Opportunities for researchers

Slide 50

Slide 50 text

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.

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

Opportunities 4. Impact of smells on productivity Smells Maintainability Productivity Impacts (established) Impacts (believed but not yet established rigorously)

Slide 55

Slide 55 text

Learning from developing smells detection tools

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Learning Make sure the availability of artifacts and their documentation A tool that lives only on the papers of its creator, is useless.

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Design consideration Plug-in or independent application • Degree of interaction • Target user • Usage workflow • Value for the time

Slide 60

Slide 60 text

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!

Slide 61

Slide 61 text

Learning Customization Every user is different! Give them the freedom by easy customization.

Slide 62

Slide 62 text

Managing smells in practice

Slide 63

Slide 63 text

Managing smells Prevent Detect and refactor Awareness Processes Strategies

Slide 64

Slide 64 text

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 • …

Slide 65

Slide 65 text

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!

Slide 66

Slide 66 text

Detecting and refactoring smells: Key strategies

Slide 67

Slide 67 text

Detecting and refactoring smells: strategies 1. Identify and track • Detect smells using tools or by manual review • Track them

Slide 68

Slide 68 text

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.

Slide 69

Slide 69 text

Detecting and refactoring smells: strategies 3. Refactoring installments • Amortize small number of smells’ refactoring into each iteration

Slide 70

Slide 70 text

Detecting and refactoring smells: strategies 4. Follow the ‘Boy’s scout rule’ • Leave the class/file much more cleaner than you got it

Slide 71

Slide 71 text

Detecting and refactoring smells: strategies 5. Motivate • Motivate and reward people for maintaining quality People care about things that you measure and reward!

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Let us conclude

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Thank you!! Courtesy: spikedmath.com