What it measures

Module Cohesion (MC) measures how related a module's exported functions are to one another. Two exports are connected if they share at least one import source — that is, they both use identifiers that come from the same import statement.

A module with MC = 1.0 has high cohesion: all its exports work with the same dependencies. A module with MC = 0.0 is a grab-bag of unrelated functions that happen to live in the same file.

How it's computed

MC = connected_export_pairs / total_possible_pairs connected_export_pairs = pairs of exports sharing ≥1 import source total_possible_pairs = N × (N-1) / 2 where N = number of exports Special cases: 0 or 1 export → MC = 1.0 (vacuously cohesive)

Examples

TypeScript — MC = 1.0 (cohesive)
import { logger } from './logger';

export function greet(name: string) {
  logger.info(`Hello ${name}`);
}
export function farewell(name: string) {
  logger.info(`Goodbye ${name}`);
}
// Both use logger → connected → MC = 1.0
TypeScript — MC = 0.0 (should be split)
import { db } from './database';
import { email } from './email';

export function processOrder(id: number) { db.query(id); }
export function formatEmail(to: string) { email.format(to); }
// No shared imports → MC = 0.0 → split into two modules
Note: Barrel files (export { foo } from './foo') have no bodies to scan and are counted as vacuously cohesive (MC = 1.0).
Edit on GitHub