Class-level

Overview

Weighted Methods per Class (WMC) is a class-level metric that captures overall class complexity by summing the complexity of every method. Classes with high WMC are harder to understand, test, and maintain. A very high WMC may indicate a God Class.

Formula

WMC = ∑ CC(m)   for each method m in the class

Each method's contribution equals its cyclomatic complexity (minimum 1). A class with three trivial methods and no branching has WMC = 3.

What tsmetrics includes in WMC

tsmetrics sums the CC of the following method kinds:

Arrow function class fields are not included as WMC contributors, because they are tracked as functions in the function table rather than as class methods.

Thresholds

SeverityDefault valueConfig key
warning20wmc.warning
error50wmc.error
tsmetrics.yaml
thresholds:
  wmc:
    warning: 15
    error:   40

Interpretation

WMC rangeInterpretation
1 – 19Reasonable class complexity
20 – 49High complexity; consider splitting responsibilities
50+Very high complexity; strong refactoring candidate

Example

TypeScript
class OrderProcessor {
  constructor() {}              // CC=1

  validate(order: Order) {     // CC=3 (2 ifs)
    if (!order.items.length) return false;
    if (!order.customer) return false;
    return true;
  }

  process(order: Order) {      // CC=4 (3 branches)
    if (!this.validate(order)) return;
    if (order.isPriority) { /* ... */ }
    else { /* ... */ }
  }
}
// WMC = 1 + 3 + 4 = 8

Usage in detection strategies

JSON field

In JSON output, the field is named wmc on each class record.

References

Edit on GitHub