Is our code Cohesive

LCOM(Lack of cohesion in methods) measures cohesion of a class. It’s the sum of set of methods not shared via sharing fields and the formula is

LCOM_Formula.PNG

Consider a class with private fields PF1 , PF2 , PF3 and methods M1() , M2 () , M3 () and the dependencies between private fields and methods illustrated in the below diagram

LCOM_ClassStructure.PNG

Class A all private variables are shared by all the methods which is a good cohesion.

Class B each private variables are shared by only one method which is bad cohesion .Each of the method and its dependent private field could be moved to its own separate class

Class C is a Okay cohesive class , still m1 method and its private field pf1 could be moved to its own separate class.

Consider LCOM while you refactor your code next time.

pitt.edu/~ckemerer/CK%20research%20papers/M..