Software Metrics
description
Transcript of Software Metrics
Software MetricsSoftware Metrics
Mei-Huei TangMei-Huei Tang
October 25, 2000October 25, 2000
Computer Science DepartmentComputer Science Department
SUNY AlbanySUNY Albany
Software MetricsSoftware Metrics
Units of measurementUnits of measurement Characterize essential features of software quantitatively.Characterize essential features of software quantitatively.
• Software products, Software products,
• Software processes, andSoftware processes, and
• Software people.Software people.
Quantitative measures, enable gaining insight ofQuantitative measures, enable gaining insight of efficiency of software process,efficiency of software process, productivity and quality of software projectproductivity and quality of software project
Characterize, evaluate, predict, and improve.Characterize, evaluate, predict, and improve.
Software Metrics CategoriesSoftware Metrics Categories
Generally software metrics can be divided Generally software metrics can be divided into two categoriesinto two categories
Project Metrics (Process Metrics)Project Metrics (Process Metrics)• Predict project needsPredict project needs• Measure dynamic changesMeasure dynamic changes
Design Metrics (Product Metrics)Design Metrics (Product Metrics)• Measure the static state of project at a particular Measure the static state of project at a particular
point in time.point in time.
Object-Oriented Project Metrics Object-Oriented Project Metrics (Lorenz and Kidd)(Lorenz and Kidd)
Application SizeApplication Size Number of Scenario Scripts (NSS)Number of Scenario Scripts (NSS) Number of Key Classes (NKC)Number of Key Classes (NKC) Number of Subsystems (NOS)Number of Subsystems (NOS)
Staffing SizeStaffing Size Person-days per class (PDC)Person-days per class (PDC) Classes per developer (CPD)Classes per developer (CPD)
SchedulingScheduling Number of major iterations (NMI)Number of major iterations (NMI) Number of contracts completed (NCC)Number of contracts completed (NCC)
Chidamber & Kemerer (CK) MetricsChidamber & Kemerer (CK) Metrics
Weighted Methods per Class (WMC)Weighted Methods per Class (WMC)
Depth of Inheritance Tree (DIT)Depth of Inheritance Tree (DIT)
Number of Children (NOC)Number of Children (NOC)
Coupling Between Object Classes(CBO)Coupling Between Object Classes(CBO)
Response For a Class (RFC)Response For a Class (RFC)
Lack of Cohesion in Methods (LCOM)Lack of Cohesion in Methods (LCOM)
Weighted Methods Per Class (WMC)Weighted Methods Per Class (WMC)
Consider a Class Consider a Class C1 , with methods , with methods M1, …, , …, Mn that are defined in the class. Let that are defined in the class. Let c1, …, , …, cn be the complexity of the methods. Then: be the complexity of the methods. Then:
WMC = WMC =
If all method complexities are considered to If all method complexities are considered to be unity, then be unity, then
WMC = WMC = nn
n
iic
1
WMC ExampleWMC ExampleClass fooClass foo { {
int local_var;int local_var;int *pInt;int *pInt;
public:public:foo(){local var = 1;foo(){local var = 1; pInt = new int;}pInt = new int;}int f1(){return local_var;}int f1(){return local_var;}int f2(){return int f2(){return +++local_var+local_var;};}int f3(){return 1/f1();}int f3(){return 1/f1();}
}}
Assume all methods Assume all methods with the same with the same complexity: complexity:
WMC(foo) = 4WMC(foo) = 4
Depth of Inheritance Tree (DIT)Depth of Inheritance Tree (DIT)
Depth of inheritance of the class is the DIT Depth of inheritance of the class is the DIT metric for the class. metric for the class.
DIT = DIT = depth of the class in depth of the class in
the inheritance treethe inheritance tree
If multiple inheritance is allowed, If multiple inheritance is allowed,
DIT = DIT = the maximum length the maximum length
from the node to the root of the treefrom the node to the root of the tree
DIT ExampleDIT Example
DIT(A) = DIT (B) = 2DIT(A) = DIT (B) = 2
Number of Children (NOC)Number of Children (NOC)
NOC = NOC = number of immediate subclasses number of immediate subclasses subordinated to a class subordinated to a class
in the class hierarchyin the class hierarchy
NOC ExampleNOC Example
NOC(C) = 2NOC(C) = 2
Coupling Between Objects (CBO)Coupling Between Objects (CBO)
A count of the number of other classes to A count of the number of other classes to which it is coupled.which it is coupled. An object is coupled to another object if one of An object is coupled to another object if one of
them acts on the other.them acts on the other. Two classes are coupled when methods Two classes are coupled when methods
declared in one class use methods or instance declared in one class use methods or instance variables defined by the other class.variables defined by the other class.
CBO ExampleCBO Example
Class A {Class A {
::
M1(){M1(){
::
B.M1();B.M1();
C.M2();C.M2();
: }: }
}}
CBO (A) = 2CBO (A) = 2
Class B {Class B { ::M1(){M1(){
: }: } }}
Class C {Class C { ::M2(){M2(){
: }: }}}
Response for a Class (RFC)Response for a Class (RFC)
RFC = | RS | where RFC = | RS | where RS RS is the is the
response set for the classresponse set for the class
RS = {RS = {MM} } all iall i { {RRii} where} where
{{RRii} = set of methods called by method I and} = set of methods called by method I and
{{MM} = set of all methods in the class} = set of all methods in the class
RFC ExampleRFC Example
Class C1{M1(){…};M2 (){…};M3(){… C2.M1()};}
Class C2{M1(){…};}
RFC = 3 + 1 = 4
Lack of Cohesion in Methods (LCOM)Lack of Cohesion in Methods (LCOM)
• Consider a Class Consider a Class C1 , with , with nn methods methods M1, , …, …, Mn. Let . Let {Ij}= set of instance variables used by method Mi. There are There are n such sets such sets {{I1}, …, {}, …, {In}. Let P = {(}. Let P = {(Ii, , Ij)| )| Ii Ij = = } } and Q = {(and Q = {(Ii, , Ij)| )| Ii Ij }. If all }. If all n sets sets {{I1}, …, {}, …, {In} are } are then let P = then let P = ..
LCOM = | P | - | Q | , if | P | > | Q | LCOM = | P | - | Q | , if | P | > | Q | = 0 , otherwise= 0 , otherwise
LCOM ExampleLCOM Example
• Consider a Class Consider a Class C with 3 methods with 3 methods M1, , M1
and and M3. Let . Let {I1}= {a, b, c, d, e}, {I2}= {a, b, c} and {I3}= {x, y, z}.
• {I1} {I2} is nonempty, but {I1} {I3} and {I2} {I3} are null sets.
LCOM = | P | - | Q | | P | - | Q | = 2 – 1 = 1
New MetricsNew Metrics
Inheritance Coupling (IC)Inheritance Coupling (IC)
Coupling Between Methods (CBM)Coupling Between Methods (CBM)
Number of Object/Memory Allocation Number of Object/Memory Allocation (NOMA)(NOMA)
Average Method Complexity (AMC)Average Method Complexity (AMC)
Inheritance Coupling (IC)Inheritance Coupling (IC)
One of its inherited methods One of its inherited methods uses a variable uses a variable (or data (or data member) that is member) that is defined in a new/redefined methoddefined in a new/redefined method. .
One of its inherited methods One of its inherited methods calls a redefined methodcalls a redefined method and and uses the return valueuses the return value of the redefined method. of the redefined method.
One of its inherited methods is One of its inherited methods is called by a redefined called by a redefined methodmethod and and uses a parameteruses a parameter that is defined in the that is defined in the redefined method. redefined method.
One of its inherited methods uses a variable X, and One of its inherited methods uses a variable X, and the value of X depends on the value of a variable Y the value of X depends on the value of a variable Y which is defined in a new/redefined method.which is defined in a new/redefined method.
The number of The number of parent classesparent classes to which a given to which a given class is coupled.class is coupled.
New Metrics ExampleNew Metrics Example
Class fooClass foo { {
int local_var;int local_var;
int *pInt;int *pInt;
public:public:
foo(){local var = 1;foo(){local var = 1;
L2:L2: pInt = new int;} pInt = new int;}
int f1(){return local_var;}int f1(){return local_var;}
int f2(){return int f2(){return +++local_var+local_var;};}
int f3(){return 1/f1();}int f3(){return 1/f1();}
}}
Class foo_c1Class foo_c1 :: Public foo{ :: Public foo{
int f1(){return int f1(){return --local_var--local_var;};}
int f4(){return local_var++;}int f4(){return local_var++;}
}}
Class foo_c2Class foo_c2 :: Public foo{ :: Public foo{
int f2(){return 0;}int f2(){return 0;}
}}
Metrics Value: foo foo_c1 foo_c2 IC 0 1 0 CBM 0 3 0 NOMA 1 0 0 AMC 1.25 1 1
Coupling Between Methods (CBM)Coupling Between Methods (CBM)
Inherited method uses a Inherited method uses a variablevariable (or data member) (or data member) that is defined in a new/redefined method. that is defined in a new/redefined method.
Inherited method Inherited method calls a redefined methodcalls a redefined method and and uses the return valueuses the return value of the redefined method. of the redefined method.
Inherited method is Inherited method is called by a redefined methodcalled by a redefined method and and uses a parameteruses a parameter that is defined in the that is defined in the redefined method. redefined method.
Inherited method uses a variable X, and the value Inherited method uses a variable X, and the value of X depends on the value of a variable Y which is of X depends on the value of a variable Y which is defined in a new/redefined method.defined in a new/redefined method.
The total number of The total number of new/re-defined methodsnew/re-defined methods to to which all the which all the inherited methodsinherited methods are coupled. are coupled.
New Metrics ExampleNew Metrics Example
Class fooClass foo { {
int local_var;int local_var;
int *pInt;int *pInt;
public:public:
foo(){local var = 1;foo(){local var = 1;
L2:L2: pInt = new int;} pInt = new int;}
int f1(){return local_var;}int f1(){return local_var;}
int f2(){return int f2(){return +++local_var+local_var;};}
int f3(){return 1/int f3(){return 1/f1()f1();};}
}}
Class foo_c1Class foo_c1 :: Public foo{ :: Public foo{
int f1(){return int f1(){return --local_var--local_var;};}
int f4(){return int f4(){return local_var+local_var+++;};}
}}
Class foo_c2Class foo_c2 :: Public foo{ :: Public foo{
int f2(){return 0;}int f2(){return 0;}
}} Metrics Value: foo foo_c1 foo_c2 IC 0 1 0 CBM 0 3 0 NOMA 1 0 0 AMC 1.25 1 1
New MetricsNew Metrics
Number of Object/Memory Allocation Number of Object/Memory Allocation (NOMA)(NOMA) The total number of statements that allocate The total number of statements that allocate
new objects or memories in a class.new objects or memories in a class.
Average Method Complexity (AMC)Average Method Complexity (AMC) The average method size of a class.The average method size of a class.
Inheritance Coupling (IC)Inheritance Coupling (IC)Coupling Between Methods (CBM)Coupling Between Methods (CBM)
New Metrics ExampleNew Metrics Example
Class fooClass foo { {
int local_var;int local_var;
int *pInt;int *pInt;
public:public:
foo(){local var = 1;foo(){local var = 1;
L2:L2: pInt = pInt = new intnew int;};}
int f1(){return local_var;}int f1(){return local_var;}
int f2(){return +int f2(){return ++local_var;}+local_var;}
int f3(){return 1/f1();}int f3(){return 1/f1();}
}}
Class foo_c1Class foo_c1 :: Public foo{ :: Public foo{
int f1(){return --local_var;}int f1(){return --local_var;}
int f4(){return local_var++;}int f4(){return local_var++;}
}}
Class foo_c2Class foo_c2 :: Public foo{ :: Public foo{
int f2(){return 0;}int f2(){return 0;}
}}
Metrics Value: foo foo_c1 foo_c2 IC 0 1 0 CBM 0 3 0 NOMA 1 0 0 AMC 1.25 1 1
New MetricsNew Metrics
Number of Object/Memory Allocation Number of Object/Memory Allocation (NOMA)(NOMA) The total number of statements that allocate The total number of statements that allocate
new objects or memories in a class.new objects or memories in a class.
Average Method Complexity (AMC)Average Method Complexity (AMC) The average method size of a class.The average method size of a class.
Inheritance Coupling (IC)Inheritance Coupling (IC)Coupling Between Methods (CBM)Coupling Between Methods (CBM)
New Metrics ExampleNew Metrics Example
Class fooClass foo { {
int local_var;int local_var;
int *pInt;int *pInt;
public:public:
foo(){local var = 1;foo(){local var = 1;
L2:L2: pInt = new int;} pInt = new int;}
int f1(){return local_var;}int f1(){return local_var;}
int f2(){return +int f2(){return ++local_var;}+local_var;}
int f3(){return 1/f1();}int f3(){return 1/f1();}
}}
Class foo_c1Class foo_c1 :: Public foo{ :: Public foo{
int f1(){return --local_var;}int f1(){return --local_var;}
int f4(){return local_var++;}int f4(){return local_var++;}
}}
Class foo_c2Class foo_c2 :: Public foo{ :: Public foo{
int f2(){return 0;}int f2(){return 0;}
}}
Metrics Value: foo foo_c1 foo_c2 IC 0 1 0 CBM 0 3 0 NOMA 1 0 0 AMC 1.25 1 1