Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Maintainable Code

Writing Maintainable Code

The presentation summarizes the importance of writing maintainable code. It discusses code smells in brief and provides an introduction to software metrics.


Tushar Sharma

February 28, 2018


  1. Software Engineering in Practice Writing Maintainable Code Tushar Sharma tushar@aueb.gr

  2. #include <stdio.h> main() { int a,b,c; int count = 1;

    for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\ TFy!QJu ROo TNn(ROo)SLq SLq ULo+\ UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\ NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\ HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\ T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\ Hq!WFs XDt!" [b+++21]; ) for(; a-- > 64 ; ) putchar ( ++c=='Z' ? c = c/ 9:33^b&1); return 0; } Any guess… what this program does?
  3. Which one is easy to work with?

  4. Evolution of JUnit 3.7 4.0 4.5 4.11

  5. Code Quality: Why to care? Impacted Quality ▪ Reusability ▪

    Changeability ▪ Understandability ▪ Extensibility ▪ … Product Quality Code/Design Quality Smells Impacted Quality ▪ Maintainability: Affected by changeability & extensibility ▪ Reliability: Impacted by poor understandability ▪ … Indicators ▪ Rigidity & Fragility ▪ Immobility & Opacity ▪ Needless complexity ▪ Needless repetition ▪ …
  6. Software Metrics Part-1

  7. Software Metrics - What & Why? • A software metric

    is a measure of some property of a software system. Why? “Measurement is the first step that leads to control and eventually to improvement. If you can’t measure something, you can’t understand it.” (by James Harrington)
  8. Object-oriented Metrics Size Complexity Cohesion & Coupling Project Namespace Type

  9. OO Metrics - Size • Large entities make the comprehension

    difficult. • Large entities indicate presence of various smells. LOC Lines Of Code NON Number Of Namespaces NOC Number Of Classes NOM Number Of Methods NOF Number Of Fields
  10. OO Metrics - Size • The response set of a

    class is a set of methods that can potentially be executed in response to a message received by an object of that class. • Larger the number of methods that can be invoked, the greater the complexity of the class. • If a large number of methods can be invoked in response to a message, the testing and debugging of the class becomes more complicated . RFC Response For Class
  11. OO Metrics - Size (Hierarchy) • Deep inheritance trees make

    the comprehension difficult. • The deeper a class is in the hierarchy, the greater the number of methods it is likely to inherit, making it more complex to predict its behaviour. • Again, wide inheritance trees make the comprehension difficult. • A wide hierarchy indicates missing intermediate inheritance level that in turn may lead to code duplication. DIT Depth of Inheritance Tree NC Number of Children
  12. OO Metrics - Cohesion and Coupling Consider a class C

    with n methods M1, M2..., Mn. Let {Ij} = set of instance variables used by method Mi. There are n such sets I1 ,…, In • P = {(Ii, Ij) | (Ii ∩ Ij ) = ∅} • Q = {(Ii, Ij) | (Ii ∩ Ij ) ≠ ∅} LCOM = |P| - |Q|, if |P| > |Q| = 0, otherwise • High LCOM indicate the violation of the Single Responsibility Principle. LCOM Lack of Cohesion Of Methods
  13. OO Metrics - Cohesion and Coupling CBO for a class

    is a count of the number of other classes to which it is coupled. • Excessive coupling between object classes is detrimental to modular design and prevents reuse. The more independent a class is, the easier it is to reuse it in another application. • A measure of coupling is useful to determine how complex the testing of various parts of a design are likely to be. The higher the inter-object class coupling, the more rigorous the testing needs to be. CBO Coupling Between Object classes
  14. OO Metrics - Complexity • CC - It is a

    quantitative measure of the number of linearly independent paths through a program's source code. • WMC – Sum of CC of each method in the class • The number of methods and the complexity of methods involved is a predictor of how much time and effort is required to develop and maintain the class. CCCyclomatic Complexity WMC Weighted Methods per Class
  15. Tools C/C++ CppDepend Source Monitor Source Meter C# Designite Ndepend

    Source Meter Java Source Meter JDepend Source Monitor
  16. Smells Part-2

  17. None
  18. “Jugaad” “Jugaad is a colloquial Hindi word that can mean

    an innovative fix or a simple work-around, used for solutions that bend rules, or a resource that can be used as such, or a person who can solve a complicated issue.” …a work-around or hack जुगाड़
  19. “Jugaad”

  20. “Jugaad”

  21. “Jugaad”

  22. “Jugaad”

  23. “Jugaad”

  24. Smells are Jugaads in Software

  25. Smells Definition certain structures in the code that suggest (sometimes

    they scream for) the possibility of refactoring. Defining Characteristics • Indicator • Poor solution • Violates best practices • Impacts quality • Recurrence
  26. Implementation Smells • Long Method • Complex Method • Long

    Parameter List • Long Identifier • Long Statement • Complex Conditional • Empty Catch Block • Magic Number • Duplicate Code
  27. Design Smells

  28. Example

  29. Broken Modularization This smell arises when members of an abstraction

    are broken and spread across multiple abstractions (when ideally they should have been localized into a single abstraction).
  30. Example

  31. 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.
  32. Example

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

    subtype conceptually do not share an “IS-A” relationship resulting in broken substitutability.
  34. Architecture Smells • Cyclic Dependency: This smell arises when two

    or more architecture components depend on each other directly or indirectly. • Unstable Dependency: This smell arises when a component depends on other components that are less stable than itself. • Ambiguous Interface: This smell arises when a component offers only a single, general entry-point into the component. • God Component: This smell occurs when a component is excessively large either in the terms of LOC or number of classes.
  35. Architecture Smells • Feature Concentration: This smell occurs when a

    component realizes more than one architectural concern/feature. • Scattered Functionality: This smell arises when multiple components are responsible for realizing the same high-level concern. • Dense Structure: This smell arises when components have excessive and dense dependencies without any particular structure.
  36. Resources http://www.tusharma.in/smells

  37. Resources http://www.designite-tools.com/designite/does- your-architecture-smell/ Does Your Architecture Smell?

  38. Resources http://www.designite-tools.com/designite/action- hub-your-friend-in-tracking-and-refactoring-smells/ Action Hub – Your Friend in Tracking

    and Refactoring Smells
  39. Resources http://www.designite-tools.com/ Designite’s web site

  40. Resources

  41. Assignment 1. Install Visual Studio (VS) 2017 (Community or higher)

    2. Install Designite from ‘Extensions and updates’ menu within Tools menu in Visual Studio. 3. Download any C# project (moderate size) 4. Open the project within VS and analyze it using Designite. 5. Tag each detected smell (well.. min 70; 2 of each kind). Tagging a smell means changing the smell status from ‘Identified’ to either ‘Refactor’, ‘Wrong’, or ‘Drop’ based on the semantics of the smell. 6. Cover all kinds of smells that are detected in your analysis
  42. Assignment 7. Record your tagging decision in an excel sheet

    or a document with your user-id. Please note that user-id is not your department roll number. It is the id generated by the tool. You can find the id in this file: "C:\Users\<user name>\AppData\Roaming\Designite\.config\user.dat" 8. Excel sheet should have the following columns: <Smell name>, <Smell status>, <rationale – why you chose this smell status; one sentence per smell is enough> 9. Your grading depends on the smell tagging and the rationale you provide. 10. Send it by April 10, 2018.
  43. Assignment 11. Obviously, feel free to come and meet me

    if you want to understand any smell or you have any confusion related to the assignment. 12. To help you in this assignment, I will set up a machine with • Visual Studio 2017 installed • Designite installed • A few C# projects downloaded You may come to our lab and use the machine (well, don’t come together)
  44. Thank you!! Courtesy: spikedmath.com