Percent Lack Of Cohesion Modified is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.
Percent Lack Of Cohesion Modified
API ID: PercentLackOfCohesionModifiedLanguages: Basic, C#, Java, Pascal
Targets: Classes
100% minus the average cohesion for class data members, modified for accessor methods.
Same as Percent Lack Of Cohesion, except a method that only touches an instance variable indirectly — by calling one of the class's own accessor methods rather than referencing the variable directly — is still credited with touching it, instead of being treated as disconnected from the class's data.
For example:
namespace MetricsDemo
{
public class CohesionDemo
{
private int a;
private int b;
public int GetA() { return a; }
public void SetA(int val) { a = val; }
public int GetB() { return b; }
public void SetB(int val) { b = val; }
public void Combine()
{
SetA(GetA() + GetB());
}
}
}
GetA()/SetA() and GetB()/SetB() each touch one instance variable directly. Combine() never references a or b directly — it only calls the accessors. The base Percent Lack Of Cohesion doesn't follow that call to credit Combine() with touching a/b, giving a lack-of-cohesion score of 60. PercentLackOfCohesionModified follows the calls and recognizes Combine() as using both variables (through the accessors it calls), raising the measured cohesion and giving a lower score of 40.
- Basic: Class, Struct
- C#: Class, Struct
- Java: Class, Interface
- Pascal: Class, Interface
- This metric can be disabled from "Project Configuration/Metrics/Object Orientated" with the "Show coupling and cohesion metrics" option.