The document discusses defect prevention software tools for analyzing source code quality and design. It provides examples of static analysis violations detected in sample Java code for a CommaHyphenString class, including unused variables, short variable names, and lack of documentation. It also includes maintainability and complexity metrics for the sample class calculated by the VizzMaintenance tool.
1. DEFECT PREVENTION SOFTWARE
• Tools for static analysis of source code: PMD Marker,
FindBugs, Metrics Marker
• Tools for determining software quality metrics for source
code: VizzMaintenance
• Tools for UML design critics: ArgoUML
• Tools for generating test cases from UML model of your code:
Smartesting
See static analysis report sample and software quality metrics
for sample class on following pages…
2. Here is sample CommaHyphenString.java source code
1 /**
2 *
3 * CommaHyphenString parses .properties file. It eliminates commas or low lines
4 * from .properties file so items in it( such as categories) could be properly
5 * showed on screen
6 */
7 import java.util.*;
8
9 public class CommaHyphenString {
10
11 //String s;
12 int lastIndex;
13 CommaHyphenString(){}
14
15
16 /** Eliminates commas, returns array of strings with low line
17 * @return String[]
18 */
19 String[] getWithLowLine(String s) {
20 int lastIndex = defineLast(s);
21 if(lastIndex==0){
22 String[] ss = new String[1];
23 ss[0] = s;
24 return ss;
25 } else {
26 int start = -1;
27 Vector v = new Vector();
28 int current = s.indexOf(',');
29 do {
30 v.addElement(s.substring(start+1,current));
31 start = current;
32 current = s.indexOf(',',current+1);
33 } while (start!=lastIndex);
34
35 String[] ss = new String[v.size()+1];
36 for(int i=0; i<v.size(); i++){
37 ss[i] = (String)v.elementAt(i);
38 }
4. 79 public int defineLast(String s){
80 int lastIndex = 0;
81 char c[] = s.toCharArray();
82 for (int i=0; i<c.length; i++){
83 if(c[i]==',') lastIndex=i;
84 }
85 return lastIndex;
86 }
87
88 }
Here are static analysis violations detected in CommaHyphenString.java
Description of static analysis violation Line Tool
All classes and interfaces must belong to a named package 9 PMD Marker
Unused field: CommaHyphenString.lastIndex 12 FindBugs
Use explicit scoping instead of the default package private level 12 PMD Marker
Document empty constructor 13 PMD Marker
Lines of Code in Method is 27 16
Metrics
Marker
Number of Statements is 28 16
Metrics
Marker
Avoid variables with short names like s 19 PMD Marker
Parameter 's' is not assigned and could be declared final 19 PMD Marker
Use explicit scoping instead of the default package private level 19 PMD Marker
Local variable 'lastIndex' could be declared final 20 PMD Marker
Avoid variables with short names like ss 22 PMD Marker
A method should have only one exit point, and that should be the last statement in the
method 24 PMD Marker
Avoid variables with short names like v 27 PMD Marker
Consider replacing this Vector with the newer java.util.List 27 PMD Marker
Local variable 'v' could be declared final 27 PMD Marker
Use ArrayList instead of Vector 27 PMD Marker
Avoid variables with short names like ss 35 PMD Marker
Lines of Code in Method is 16 44
Metrics
Marker
Avoid variables with short names like s 47 PMD Marker
5. Parameter 's' is not assigned and could be declared final 47 PMD Marker
Use explicit scoping instead of the default package private level 47 PMD Marker
Avoid using if statements without curly braces 53 PMD Marker
CommaHyphenString.getStringsOnly(String) concatenates strings using + in a loop 54 FindBugs
Prefer StringBuffer over += for concatenating strings 54 PMD Marker
Parameter 's' is not assigned and could be declared final 65 PMD Marker
Use explicit scoping instead of the default package private level 65 PMD Marker
Parameter 's' is not assigned and could be declared final 72 PMD Marker
Use explicit scoping instead of the default package private level 72 PMD Marker
Parameter 's' is not assigned and could be declared final 79 PMD Marker
Local variable 'c' could be declared final 81 PMD Marker
Avoid using if statements without curly braces 83 PMD Marker
Here are VizzMaintenance software quality metrics for CommaHyphenString.java class
Class Name Maintainability CBO CYC_Classes DAC DIT ILCOM LCOM LD LEN LOC LOD_Class MPC NAM NOC NOM
CommaHyphenString 0,136637872 0 1 0 0 0 25 0 17 80 0,167 0 6 0 5
6. Here are full names of VizzMaintenance software quality metrics (click on links for details)
Complexity
o Size
Lines of Code (LOC)
o Interface Complexity
Number of Attributes and Methods (SIZE2)
Number Of local Methods (NOM)
o Structural Complexity
McCabe Cyclomatic Complexity (CC)
Weighted Method Count (WMC)
Response For a Class (RFC)
Architecture and Structure
o Inheritance
Depth of Inheritance Tree (DIT)
Number Of Children (NOC)
o Coupling
o Afferent Coupling (Ca)
7. Coupling Between Objects (CBO)
Change Dependency Between Classes (CDBC)
Change Dependency Of Classes (CDOC)
Efferent Coupling (Ce)
Coupling Factor (CF)
Data Abstraction Coupling (DAC)
Instability (I)
Locality of Data (LD)
Message Passing Coupling (MPC)
Package Data Abstraction Coupling (PDAC)
o Cohesion
Lack of Cohesion in Methods (LCOM)
Improvement of LCOM (ILCOM)
Tight Class Cohesion (TCC)
Design Guidelines and Code Conventions
o Documentation
Lack Of Documentation (LOD)
o Code Conventions
8. Follow me on Facebook page
https://www.facebook.com/AutomatedTesting