Slide 1

Slide 1 text

Software Engineering in Practice Writing Maintainable Code Tushar Sharma [email protected]

Slide 2

Slide 2 text

#include 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?

Slide 3

Slide 3 text

Which one is easy to work with?

Slide 4

Slide 4 text

Evolution of JUnit 3.7 4.0 4.5 4.11

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Software Metrics Part-1

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

Object-oriented Metrics Size Complexity Cohesion & Coupling Project Namespace Type Method LOC NOC NON LOC NOC LOC NOM NOF DIT NC RFC LOC NP WMC CC LCOM CBO Fan-in/Fan- out

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Tools C/C++ CppDepend Source Monitor Source Meter C# Designite Ndepend Source Meter Java Source Meter JDepend Source Monitor

Slide 16

Slide 16 text

Smells Part-2

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

“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 जुगाड़

Slide 19

Slide 19 text

“Jugaad”

Slide 20

Slide 20 text

“Jugaad”

Slide 21

Slide 21 text

“Jugaad”

Slide 22

Slide 22 text

“Jugaad”

Slide 23

Slide 23 text

“Jugaad”

Slide 24

Slide 24 text

Smells are Jugaads in Software

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Implementation Smells • Long Method • Complex Method • Long Parameter List • Long Identifier • Long Statement • Complex Conditional • Empty Catch Block • Magic Number • Duplicate Code

Slide 27

Slide 27 text

Design Smells

Slide 28

Slide 28 text

Example

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Example

Slide 31

Slide 31 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 32

Slide 32 text

Example

Slide 33

Slide 33 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 34

Slide 34 text

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.

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

Resources http://www.tusharma.in/smells

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Resources http://www.designite-tools.com/ Designite’s web site

Slide 40

Slide 40 text

Resources

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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\\AppData\Roaming\Designite\.config\user.dat" 8. Excel sheet should have the following columns: , , 9. Your grading depends on the smell tagging and the rationale you provide. 10. Send it by April 10, 2018.

Slide 43

Slide 43 text

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)

Slide 44

Slide 44 text

Thank you!! Courtesy: spikedmath.com