|Table of Contents|
What is LCOM4?
Cohesion is the degree to which the methods of a single class are tight together. When two methods in a class do not use a common attribute or a common method, it means that they share nothing and should probably not belong to the same class to respect the Single Responsibility Principle. In other words you can split your class into several new classes to gain modularity at class level.
Sonar computes the LCOM4 (Lack of Cohesion of Methods) metric based on Hitz & Montazeri. Here is a good introduction: http://www.aivosto.com/project/help/pm-oo-cohesion.html#LCOM4.
The best value for this metric is 1.
How to Hunt for Bad LCOM4?
Add the Chidamber & Kemerer widget on your dashboard:
Project with one single class: Client.
The LCOM4 has a value of 2:
It shows there should be two different objects: Client and Address.
Good LCOM4 after Refactoring
After creating a new Address class and creating an Address object in Client:
The LCOM4 is down to the best value: 1.
See the following blog post: Clean Up Design at Class Level with Sonar.