Cohesion Metrics in Software Testing
Cohesion Metrics in Software Testing
In software engineering, especially in object-oriented programming and design, writing modular, maintainable, and understandable code is essential. One of the ways to measure how closely related and focused the responsibilities of a single module (like a class or component) are is by using *Cohesion Metrics*. These metrics help identify whether a class is doing one well-defined job or is overloaded with unrelated functionalities. Higher cohesion generally leads to better software quality.
What is Cohesion Metrics?
Cohesion metrics are used to measure the degree to which the elements inside a module belong together. In object-oriented systems, these metrics assess how strongly the methods and attributes of a class are related to each other. High cohesion indicates that a class has a single, well-focused purpose. Low cohesion may be a sign that the class is trying to do too much and might need to be broken into smaller, more manageable parts.
Below is a list of commonly used cohesion metrics in object-oriented software systems:
Metric | Full Form | Description |
---|---|---|
LCOM | Lack of Cohesion in Methods | This metric measures the dissimilarity of methods in a class based on the instance variables or attributes they access.
A high LCOM value indicates low cohesion, suggesting that the class should possibly be split into multiple classes. |
TCC | Tight Class Cohesion | Calculates the percentage of method pairs that are directly connected through shared attributes. A higher TCC indicates better cohesion among methods. |
LCC | Loose Class Cohesion | Measures the indirect connections between methods through transitive attribute sharing. LCC complements TCC by identifying additional cohesion not immediately visible. |
ICH | Information-flow-based Cohesion | Evaluates cohesion based on the flow of information between methods within a class. It considers both method calls and shared data as indicators of cohesion. |