Tight Class Cohesion (TCC)
Tight class cohesion is a quantitative metric that measures how well the methods of a class are related to each other by sharing access to at least one common instance field declared in that class.
Calculated as the ratio of connected method pairs to all possible pairs among declared methods (formula: TCC = NDC / NP, where NP = N * (N - 1) / 2 (N = declared methods) and NDC = pairs of methods accessing ≥1 common instance field).
Low cohesion indicates classes with several unrelated responsibilities.
Edge Cases and Clarifications
- Only instance methods of a class, mixin or enum contribute to the number of declared methods.
- Methods without the implementation, external methods, operators, boilerplate methods (e.g.
toString) are excluded. - Stateful (usually have no methods other than
createState) and stateless (do not imply the presence of a state by design) widgets are excluded. - Static field usages and inherited field usages do not count as accessing the shared state.
- Accessing
widgetcounts as accessing the shared state. - If a class has only one instance method, TCC depends on whether that method access instance fields.
- Method calls do not contribute to TCC connectivity.
Why Be Cautious
Low tight class cohesion can lead to:
- Increased Complexity: A class performing multiple unrelated actions is more difficult to understand and maintain.
- Maintainability Challenges: When a class has low cohesion, its responsibilities are intermixed, making it difficult to change one part without unintentionally impacting another.
- Reduced Reusability: To reuse one specific functionality from a non-cohesive class, you often must import unrelated code and dependencies that come with it.
- Difficulty in Testing: Testing one behavior may require setting up an extensive, unrelated state because that state is tightly interwoven within the class.
How to Address Low Tight Class Cohesion?
If a method does not access instance members (fields/methods/etc.), consider making it static or moving it out of the class/mixin/enum.
For other cases, split the declaration into several smaller declarations each focused on its own responsibilities.
Config Example
dcm:
metrics:
tight-class-cohesion:
threshold: 0.33
To set multiple threshold or other common config options, refer to the Configuring Metrics page.
Example
To view what contributes to the metric value, generate an HTML report. For this metric, the report will highlight shared fields.
❌ Bad: Low Tight Class Cohesion
// TCC: 0.167, 4 methods, 6 possible pair, only 1 is connected (updateProfile and sendLoginNotification)
class UserAccount {
String username;
String email;
String password;
DateTime lastLogin;
void updateProfile(String newUsername, String newEmail) {
username = newUsername;
email = newEmail;
}
void changePassword(String newPassword) {
password = newPassword;
}
void sendLoginNotification() {
print("Email sent to $email regarding login activity.");
}
void logLastLogin() {
lastLogin = DateTime.now();
}
}
✅ Good: High Tight Class Cohesion
// TCC: 1
class ProfileManager {
String username;
String email;
String password;
void updateProfile({String? newUsername, String? newEmail, String? newPassword}) {
username = newUsername ?? username;
email = newEmail ?? email;
password = newPassword ?? password;
}
}
class NotificationService {
static void sendLoginNotification(String email) {
print("Email sent to $email regarding login activity.");
}
}
class LogService {
static void logLastLogin(DateTime lastLogin) {
print("Last login recorded at $lastLogin");
}
}