Detecting and Managing Code Smells – Research and Practice

Detecting and Managing Code Smells – Research and Practice

Slides of the technical briefing presented in ICSE 2018 on May 29, 2018.

16a26342d4ff4724dfe59b0dc947dead?s=128

Tushar Sharma

May 29, 2018
Tweet

Transcript

  1. Detecting and Managing Code Smells – Research and Practice Tushar

    Sharma tushar@aueb.gr This work is funded by SENECA project under Marie-Skłodowska Curie Actions Innovative Training Networks ITN-EID. Grant agreement number 642954.
  2. None
  3. • 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
  4. • 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
  5. Evolution of JUnit 3.7 4.0 4.5 4.11

  6. Which one is easy to work with?

  7. 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
  8. None
  9. None
  10. 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
  11. None
  12. • 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
  13. 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 • …
  14. Smells: Types http://www.tusharma.in/smells/

  15. Let us see some examples

  16. Granularity of code smells - Implementation - Design - Architecture

  17. Implementation smells

  18. 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)
  19. Implementation Smells if (sum > 13.05689) { //do something }

  20. Design Smells

  21. Example

  22. 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.
  23. Example

  24. Broken Hierarchy This smell arises when a supertype and its

    subtype conceptually do not share an “IS-A” relationship resulting in broken substitutability.
  25. Architecture Smells

  26. Architecture Smells Assumes, UriUtil IHostFactories, IRequireHostF actories MachineKey Util IEmbeddedResourc

    eRetrieval, Util, Logger, Reporting, RequiresEx, Strings
  27. 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
  28. Dense Structure Average degree = # ∗|&| |'|

  29. 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.
  30. 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.
  31. Causes of smells

  32. Causes of smells Actor-based classification assigns the responsibility of the

    causes to specific actor(s). Role C1 C2 C3 … Manager ☑ Technical lead ☑ ☑ Developer ☑
  33. Effects of smells Effects Artifacts Processes People Morale and motivation

    Maintainability Effort/Cost Reliability Change proneness Testability Performance Productivity
  34. None
  35. None
  36. Code smell detection – Research and practice

  37. Metrics-based smell detection < > <!> Code (or source artifact)

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

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

    Smells History information Detection model < > < > < >
  40. 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) < > <!> < >
  41. Optimization-based smell detection < > <!> Code (or source artifact)

    Source model Smells Metrics Optimization algorithm Existing examples < > <!> < >
  42. 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 <!> < > < > < > < > < > <!> < >
  43. • 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
  44. 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
  45. Demo

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

  47. 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!
  48. 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
  49. Opportunities for researchers

  50. 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.
  51. 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
  52. 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
  53. 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.
  54. Opportunities 4. Impact of smells on productivity Smells Maintainability Productivity

    Impacts (established) Impacts (believed but not yet established rigorously)
  55. Learning from developing smells detection tools

  56. 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
  57. Learning Make sure the availability of artifacts and their documentation

    A tool that lives only on the papers of its creator, is useless.
  58. 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
  59. Design consideration Plug-in or independent application • Degree of interaction

    • Target user • Usage workflow • Value for the time
  60. 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!
  61. Learning Customization Every user is different! Give them the freedom

    by easy customization.
  62. Managing smells in practice

  63. Managing smells Prevent Detect and refactor Awareness Processes Strategies

  64. 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 • …
  65. 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!
  66. Detecting and refactoring smells: Key strategies

  67. Detecting and refactoring smells: strategies 1. Identify and track •

    Detect smells using tools or by manual review • Track them
  68. 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.
  69. Detecting and refactoring smells: strategies 3. Refactoring installments • Amortize

    small number of smells’ refactoring into each iteration
  70. Detecting and refactoring smells: strategies 4. Follow the ‘Boy’s scout

    rule’ • Leave the class/file much more cleaner than you got it
  71. Detecting and refactoring smells: strategies 5. Motivate • Motivate and

    reward people for maintaining quality People care about things that you measure and reward!
  72. 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
  73. 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
  74. Let us conclude

  75. None
  76. Thank you!! Courtesy: spikedmath.com