Lack of Cohesion in Methods

Cohesion is an important concept in OO programming. It indicates whether a class represents a single abstraction or multiple abstractions. The idea is that if a class represents more than one abstraction, it should be refactored into more than one class, each of which represents a single abstraction.

Despite its importance, it is difficult to establish a clear mechanism for measuring it. This is probably due to the fact that good abstractions have deep semantics and a class that is clearly cohesive when viewed from a semantic point of view may not be so when viewed from a purely symbolic point of view.

As an aside, the somewhat inelegant name is due to the wish to have lower metric values representing a 'better' situation.

I have selected three definitions of lack of cohesion. That of Chidamber and Kemerer, that of Henderson and Sellers, and one I invented myself.

Chidamber and Kemerer

Chidamber and Kemerer define Lack of Cohesion in Methods as the number of pairs of methods in a class that don't have at least one field in common minus the number of pairs of methods in the class that do share at least one field. When this value is negative, the metric value is set to 0.

Henderson-Sellers

Henderson-Sellers defines Lack of Cohesion in Methods as follows. Let:

M be the set of methods defined by the class
F be the set of fields defined by the class
r(f) be the number of methods that access field f, where f is a member of F
<r> be the mean of r(f) over F.

Then:

Lack of Cohesion in Methods = <r> - |M|
1 - |M|

Note 1: I have only included methods in M if they access at least one field. The reason for this is that methods that do not access fields are often required to be non-static for reasons of polymorphic dispatch. However, these kinds of methods skew the value of this metric in a way that is not helpful.

Note 2: I have only included fields in F if they are accessed by at least one method in the class. A good compiler can tell you if fields are unused, and leaving them in the calculation of this metric skews the value.