Project Metrics Help – Cohesion metrics

LCOM1, LCOM2 and LCOM3 — less suitable for VB

LCOM1, LCOM2 and LCOM3 are not as suitable for Visual Basic projectsas LCOM4. They are less accurate especially as they don’t consider theimpact of property accessors and procedure calls, which are bothfrequently used to access the values of variables in a cohesive way.They may be more appropriate to other object-oriented languages such asC++. We provide these metrics for the sake of completeness. You can usethem as complementary metrics in addition to LCOM4.

LCOM1 Chidamber & Kemerer

LCOM1 was introduced in the Chidamber & Kemerer metrics suite. It’s also called LCOM or LOCOM, and it’s calculated as follows:

Take each pair of methods in the class. If they access disjoint sets of instance variables, increase P by one. If they share at least one variable access, increase Q by one.

LCOM1 =
P – Q
, if P > QLCOM1 =
0
otherwise

LCOM1 = 0 indicates a cohesive class.

LCOM1 > 0 indicates that the class needs or can be split into two or more classes, since its variables belong in disjoint sets.

Classes with a high LCOM1 have been found to be fault-prone.

A high LCOM1 value indicates disparateness in the functionality provided by the class. This metric can be used to identify classes that are attempting to achieve many different objectives, and consequently are likely to behave in less predictable ways than classes that have lower LCOM1 values. Such classes could be more error prone and more difficult to test and could possibly be disaggregated into two or more classes that are more well defined in their behavior. The LCOM1 metric can be used by senior designers and project managers as a relatively simple way to track whether the cohesion principle is adhered to in the design of an application and advise changes.

LCOM1 critique

LCOM1 has received its deal of critique. It has been shown to have a number of drawbacks, so it should be used with caution.

First, LCOM1 gives a value of zero for very different classes. To overcome that problem, new metrics, LCOM2 and LCOM3, have been suggested (see below).

Second, Gupta suggests that LCOM1 is not a valid way to measure cohesiveness of a class. That’s because its definition is based on method-data interaction, which may not be a correct way to define cohesiveness in the object-oriented world. Moreover, very different classes may have an equal LCOM1.

Third, as LCOM1 is defined on variable access, it’s not well suited for classes that internally access their data via properties. A class that gets/sets its own internal data via its own properties, and not via direct variable read/write, may show a high LCOM1. This is not an indication of a problematic class. LCOM1 is not suitable for measuring such classes.

Implementation details. The definition of LCOM1 deals with instance variables but all methods of a class. Class variables (Shared variables in VB.NET) are not taken into account. On the contrary, all the methods are taken into account, whether Shared or not.

Project Analyzer assumes that a procedure in a class is a method if it can have code in it. Thus, Subs, Functions and each of Property Get/Set/Let are methods, whereas a DLL declare or Event declaration are not methods. What is more, empty procedure definitions, such as abstract MustOverride procedures in VB.NET, are not methods.

Readings for LCOM1

LCOM2 and LCOM3 (Henderson-Sellers, Constantine & Graham)

To overcome the problems of LCOM1, two additional metrics have been proposed: LCOM2 and LCOM3.

A low value of LCOM2 or LCOM3 indicates high cohesion and a well-designed class. It is likely that the system has good class subdivision implying simplicity and high reusability. A cohesive class will tend to provide a high degree of encapsulation. A higher value of LCOM2 or LCOM3 indicates decreased encapsulation and increased complexity, thereby increasing the likelihood of errors.

Which one to choose, LCOM2 or LCOM3? This is a matter of taste. LCOM2 and LCOM3 are similar measures with different formulae. LCOM3 varies in the range [0,1] while LCOM2 is in the range [0,2]. LCOM2>=1 indicates a very problematic class. LCOM3 has no single threshold value.

It is a good idea to remove any dead variables before interpreting the values of LCOM2 or LCOM3. Dead variables can lead to high values of LCOM2 and LCOM3, thus leading to wrong interpretations of what should be done.

Definitions used for LCOM2 and LCOM3

m number of procedures (methods) in class
a number of variables (attributes) in class
mA number of methods that access a variable (attribute)
sum(mA) sum of mA over attributes of a class

Implementation details. m is equal to WMC. a contains all variables whether Shared or not. All accesses to a variable are counted.

LCOM2

LCOM2 = 1 – sum(mA)/(m*a)

LCOM2 equals the percentage of methods that do not access a specific attribute averaged over all attributes in the class. If the number of methods or attributes is zero, LCOM2 is undefined and displayed as zero.

LCOM3 alias LCOM*

LCOM3 = (m – sum(mA)/a) / (m-1)

LCOM3 varies between 0 and 2. Values 1..2 are consideredalarming.

In a normal class whose methods access the class’s own variables, LCOM3varies between 0 (high cohesion) and 1 (no cohesion). When LCOM3=0, each method accesses all variables. This indicates thehighest possible cohesion. LCOM3=1indicates extreme lack of cohesion. In this case, the class should besplit.

When there are variables that are not accessed by any of the class’smethods, 1 < LCOM3 <= 2. This happens if the variables are dead orthey are only accessed outside the class. Both cases represent adesign flaw. The class is a candidate for rewriting as a module.Alternatively, the class variables should be encapsulated with accessormethods or properties. There may also be some dead variables toremove.

If there are no more than one method in a class, LCOM3 is undefined.If there are no variables in a class, LCOM3 is undefined. An undefinedLCOM3 is displayed as zero.

Readings for LCOM2/LCOM3

  • Henderson-Sellers, B, L, Constantine and I, Graham , ‘Coupling and Cohesion (Towards a Valid Metrics Suite for Object-Oriented Analysis and Design)’, Object-Oriented Systems, 3(3), pp143-158, 1996.
  • Henderson-Sellers, 1996, Object-Oriented Metrics: Measures of Complexity, Prentice Hall.
  • Roger Whitney: Course material. CS 696: Advanced OO. Doc 6, Metrics. Spring Semester, 1997. San Diego State University. http://www.eli.sdsu.edu/courses/spring97/cs696/notes/metrics/metrics.html

Leave a Reply

Your email address will not be published. Required fields are marked *