# Lack of Cohesion in Methods

Lack of Cohesion in Methods

The LCOM is an object-oriented metric used to measure the cohesiveness of a class. It has been introduced by Chidamber and Kemerer[1].

## Definition

Consider a Class C1 with n methods M1,M2...,Mn. Let {Ij} = set of instance variables used by method Mi.

There are n such sets {I1},...{In}. Let $P = \{ (I_i,I_j) | I_i \cap I_j = \varnothing \}$ and $Q = \{ (I_i,I_j) | I_i \cap I_j \_ \varnothing \}$.

If all n sets {I1},...{In} are $\varnothing$ then let $P = \varnothing$.

LCOM =  | P | − | Q | ,if | P | > | Q |

= 0 otherwise[2]

Example: Consider a class C with three methods M1,M2 and M3. Let {I1} = {a,b,c,d,e} and {I2} = {a,b,e} and {I3} = {x,y,z}. $\{I_1\} \cap \{I_2\}$ is non-empty, but $\{I_1\} \cap \{I_3\}$ and $\{I_2\} \cap \{I_3\}$ are null sets. LCOM is the (number of null intersections - number of non-empty intersections), which in this case is 1.

## Theoretical basis

This uses the notion of degree of similarity of methods. The degree of similarity for two methods M1 and M2 in class C1 is given by:

$\sigma () = \{I_1\} \cap \{I_2\}$ where {I1} and {I2} are the sets of instance variables used by M1 and M2.

The LCOM is a count of the number of method pairs whose similarity is 0 (i.e. σ() is a null set) minus the count of method pairs whose similarity is not zero. The larger the number of similar methods, the more cohesive the class, which is consistent with traditional notions of cohesion that measure the inter-relatedness between portions of a program. If none of the methods of a class display any instance behavior, i.e. do not use any instance variables, they have no similarity and the LCOM value for the class will be zero. The LCOM value provides a measure of the relative disparate nature of methods in the class. A smaller number of disjoint pairs (elements of set P) implies greater similarity of methods. LCOM is intimately tied to the instance variables and methods of a class, and therefore is a measure of the attributes of an object class.

## Viewpoints

• Cohesiveness of methods within a class is desirable, since it promotes encapsulation.
• Lack of cohesion implies classes should probably be split into two or more sub-classes.
• Any measure of disparateness of methods helps identify flaws in the design of classes.
• Low cohesion increases complexity, thereby increasing the likelihood of errors during the development process.

Glossary:

Papers:

## References

1. A Metrics Suite for Object Oriented Design
2. Note that the LCOM metric for a class where | P |  =  | Q | will be zero. This does not imply maximal cohesiveness, since within the set of classes with LCOM = 0, some may be more cohesive than others.