# Lack of Cohesion in Methods

### From Maisqual Wiki

**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]}.

## Contents |

## Definition

Consider a Class *C*_{1} with n methods *M*_{1},*M*_{2}...,*M*_{n}. Let {*I*_{j}} = set of instance variables used by method *M*_{i}.

There are n such sets {*I*_{1}},...{*I*_{n}}. Let and .

If all n sets {*I*_{1}},...{*I*_{n}} are then let .

*L**C**O**M* = | *P* | − | *Q* | ,*i**f* | *P* | > | *Q* |

- = 0 otherwise
^{[2]}

Example: Consider a class *C* with three methods *M*_{1},*M*_{2} and *M*_{3}. Let {*I*_{1}} = {*a*,*b*,*c*,*d*,*e*} and {*I*_{2}} = {*a*,*b*,*e*} and {*I*_{3}} = {*x*,*y*,*z*}. is non-empty, but and 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 *M*_{1} and
*M*_{2} in class *C*_{1} is given by:

where {*I*_{1}} and {*I*_{2}} are the sets of instance variables used by *M*_{1} and *M*_{2}.

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.

## See also

Glossary:

Papers:

- Original paper from Chidamber and Kemerer : A_Metrics_Suite_for_Object_Oriented_Design
- Subramaniam et al. compared it to other metrics in some contexts: Empirical Study of CK metrics for object-oriented design complexity: Implications for software defects

## References

- ↑ A Metrics Suite for Object Oriented Design
- ↑ 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.