COHESION MEASUREMENTS (April, 2004) By Abubakar Adam

Download Report

Transcript COHESION MEASUREMENTS (April, 2004) By Abubakar Adam

COHESION MEASUREMENTS
By
Abubakar Adam
(April, 2004)
Outline
•
•
•
•
Introduction
Cohesion metrics in the literature
Classification for cohesion metrics
New Metric
Introduction
• Cohesion determines the extent to
which the individual components of a
module are required to perform the
same task
• While cohesion addresses intra-module
connectedness, coupling addresses
inter-module connectedness
Introduction (Cont..)
System
Subsystem1
Subsystem2
Introduction
• A cohesive module:
– Has all its components tailored towards
one functionality
– Does not rely on other modules
– Is easy to maintain
• Cohesion should be maximized
• Coupling should be minimized
Cohesion metrics
#
Metric
1
LCOM1
2
LCOM2
3
LCOM3
4
LCOM4
5
Co
6
LCOM5
Let P be the pairs of methods without shared instance variables, and Q be the pairs of methods with shared instance variables. Then
| P |  | Q |, if | P || Q |
LCOM2 = 
0, otherwise
7
LCC
8
TCC
9
RCI
10
CCM
11
ECCM
12
CBMC
13
OCC
14
PCC
1 a
  (Aj )  m
a i 1
1 m
LCOM1
• LCOM1 is the number of pairs of methods with no common
attributes references
• From the example given Below, LCOM1 = 2
C
m1
A1
m2
A2
m3
A3
A4
LCOM2
•
Consider a class C with methods M1, M2,….., Mn. Let {Ii} = set of
instance variables used by method Mi. There are n such sets, i.e., {1i},
{I2},….., {In}. Let P = { (Ii, Ij ) | Ii  Ij = } and Q = {(Ii, Ij ) | Ii  Ij 
}. If all n sets {1i}, {I2},….., {In} are  then let P = .
| P |  | Q |, if | P || Q |


LCOM2 = 

0, otherwise
C
m1
A1
m2
A2
m3
A3
A4
P = 2 and Q = 1 thus LCOM2 = 1
LCOM3
• Consider an undirected graph G where the vertices are the
methods of a class, and there is an edge between two
vertices if the corresponding methods share at least one
instance variable.
• LCOM3=|connected components of G|
C
m1
A1
m2
A2
LCOM3 = 2
m3
A3
A4
Gx
m1
m2
m3
LCOM4
• Like LCOM3, where graph G additionally has an
edge between vertices representing methods Mi and
Mj, if Mi invokes Mj or vice versa.
C
Method invocation
m1
A1
m2
A2
LCOM4 = 1
m3
A3
A4
Gx
m1
m2
m3
The Connectivity Metric
Co(C) =
2
| Ec | (| Vc | 1)
(| Vc | 1).(| Vc | 2)
C
Method invocation
m1
A1
m2
A2
m3
A3
A4
Gx
m1
m2
m3
From the example given above, we have Ec = 2 and Vc = 3. Hence, Co(C) = 0
The TCC and LCC Metrics
TCC (C ) 
LCC (C ) 
NP(C) =
NDC (C )
NP(C )
m1
NDC (C )  NIC (C )
NP(C )
A1
m2
A2
m3
A3
A4
N * ( N  1)
2
NDC(C) = 2, NIC(C) = 1 and NP(C) = 3,
Thus,
TCC = 2/3 and LCC = 1
The LCOM5 Metric
LCOM5 =
1 a
 ( Aj )  m

a j 1
1 m
C
m1
m2
m3
Mi
A1
A2
A3
Aj
(A1) = 1
(A1) = 2
(A1) = 2
From the example, we have: m = 3 and a = 3,
Thus,
LCOM5 = 2/3
The RCI Metric
RCI (C ) 
| Cl (C ) |
| Max (C ) |
CI means cohesive interactions = the set of all DD- and DM-interactions present in the class C
Max(C) = the set of all possible DD- and DM-interactions that can be established in class C
C
m1
m2
m3
From the example we have,
|CI(C)| = 8
m4
|Max(C)| = 30
Hence,
A1
A2
A3
A4
A5
RCI = 8/30 = 4/15
The CBMC Metrc
1 n
CBMC (C )  Fc (Gr (C ))  Fs (Gr (C ))  Fc (Gr (C ))   CBMC (Gri (C ))
n i 1
Fc (Gr (C )) 
| M g (Gr ) |
| M n (Gr ) |
1 n
Fs (Gr (C ))   CBMC (Gri )
n i 1
Mg = the set of glue methods
Mn = the set normal methods
Glue methods are the minimum number of methods without which the reference graph will
be divided into sub-graphs.
It is one of the n children of Gr in the structure tree; CBMC denotes the cohesion of a
component .
Example of CBMC
m1
m3
m2
A1
A2
m4
A3
m5
A4
m1
m3
m2
A1
A2
m4
A3
m5
A4
m3
m1
A1
A2
m4
A3
m4
A4
m5
A2
A3
A4
m5
A3
A4
m5
Example of CBMC (Cont..)
1 n
CBMC (C )  Fc (Gr (C ))  Fs (Gr (C ))  Fc (Gr (C ))   CBMC (Gri (C ))
n i 1


CBMC (C ) 
1 1
 CBMC (G 1 )  CBMC (G 2 )
5 2
CBMC (C ) 
1
1  FC  FS   1 1  1  1 CBMC (G 21 )  CBMC (G 22 ) 
10
10  3 2

CBMC (C ) 
1  1
 1  1 1 1


1

1

F

F
)
1  1   CBMC (G 221 )  CBMC (G 222 )
C
S  


10  6
 10  6  2 2
CBMC (C ) 
1  1 1
 1  1  1
 1




1

1

1

F

F

1

1

1

1

1



 
C
S
 10  6
10  6  4
4


 8




The CCM and ECCM Metrics
CCM (C ) 
NC (C )
NMP(C )  NCC (C )
ECCM (C ) 
NC (C )
 (1  PenaltyFactor(C ))
NMP(C ).NCC(C )
ECCM (C )  CCM (C )  (1  PenaltyFactor(C ))
PenaltyFactor(C ) 
NORM (C )
NOIM (C )
NORM(C) is the number of re-implemented methods and NOIM(C) is
the number of inherited methods
CCM and ECCM Example
m1
m3
m2
m4
m5
m1
m2
m3
A1
A2
A3
A4
m5
m4
NC(C) = 5, NMP(C) = 10 and NCC(C) = 1
hence CCM = 2/5
Note:
ECCM = CCM because in this example no
specification is given for the inherited
methods
The OCC and PCC

 | Rw(mi) | 
max
, n  1

OCC (C )  i 1,..., n  n  1 
0, n  1


 | Rs (mi) | 
max
, n  1

PCC(C )  i 1,..., n  n  1 
0, n  1

General Examples
#
Metric
Example1
m1
m2
A1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
LCOM1
LCOM2
LCOM3
LCOM4
Co
LCOM5
LCC
TCC
RCI
CCM
ECCM
CBMC
OCC
PCC
m3
A2
Example2
m4
A3
m5
A4
6
6-3=3
2
2
N/A
13/16 = 0.81
0.6
0.3
7/30 = 0.23
3/20 = 0.15
N/A
0
¾ = 0.75
N/A
m1
m2
A1
m3
A2
Example3
m4
A3
6
6-4=2
1
1
0
¾ = 0.75
1
0.4
8/30 = 0.27
4/10 = 0.4
N/A
1/8 = 0.13
4/4 = 1
N/A
m5
A4
m1
m2
A1
m3
A2
Comment
m4
A3
0
0
1
1
1
7/4 = 1.75
1
1
13/30 = 0.43
1
N/A
1
4/4 = 1
N/A
m5
A4
Classification for Cohesion Metrics
• There are two types of classification criteria
– Factor
– Characteristic
• Factors: these criteria identify the things
that may affect the cohesiveness of a
module.
• Characteristics: these criteria, as the name
implies, describe the characteristics of the
cohesion metric i.e the features of the metric.
Classification (Cont..)
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Classification Criteria
Underlying Approach
Cohesion Criteria
Coverage Co mp leteness
Granularity
Soundness/Validity
Sensitivity
Ease of Imp l. and use
Interpretation
Validation
Availability
Source
Connection Type
Special Methods
Inheritance
Indirect Connections
Connected Components
Description
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Characteristic
Factor
Factor
Factor
Factor
Factor
Source
New
Briand
New
Briand
New
New
New
New
Briand
New
Briand
Briand
Briand
Briand
Briand
New
Remark
Similar to Briand’s objectivity criteria
Similar to Braind’s usable criteria
Briand called this known problems
In the reference graph
Evaluating The LCOM1 Metrics
#
1
Attribute
Underlying Approach
2
3
4
5
Cohesion Criteria
Coverage Co mp leteness
Granularity
Soundness/Validity
6
7
8
9
10
11
12
13
14
15
Sensitivity
Ease of Imp l. and use
Interpretation
Validation
Availability
Source
Connection Type
Special Methods
Inheritance
Indirect Connection
Approach
The underlying approach for the proposed metric is the notion of degree
of similarity of methods initially proposed by Bunge
Attribute sharing
Measure cohesion at class level
The approach does not correctly capture the cohesion of a class though it
gives an idea of how cohesive a class is. The metric is ambiguous bec ause
two classes that are, intuitively, o f different cohesion will have the same
value for the measure
Can be implemented
Can be automated but difficult to interpret the measure
Not validated
No consideration was given for special methods in this approach
N/A