Refused Bequest
Class Strategy | Multi-metric
A subclass that inherits from a parent but overrides very few of its methods โ "refusing" the behaviour bequeathed by the parent. This often signals that the subclass should not be a subclass at all (prefer composition over inheritance).
Detection Rule
DIT > 0 AND override_ratio < 0.33
override_ratio = (NOOM + implicit_overrides) / NOM
| Term | Definition |
|---|---|
| DIT > 0 | Class has at least one parent (is a subclass) |
| override_ratio < 0.33 | Less than a third of its methods override the parent |
| NOOM | Methods with the explicit override keyword |
| implicit_overrides | Concrete methods whose names match an abstract method in the parent |
| NOM | Total concrete methods in the subclass |
TypeScript Example
abstract class BaseRepository<T> {
abstract findById(id: string): Promise<T>;
abstract save(entity: T): Promise<void>;
abstract delete(id: string): Promise<void>;
}
class UserReportService extends BaseRepository<User> {
findById(id: string) { ... } // implicit override +1
save(u: User) { ... } // implicit override +1
delete(id: string) { ... } // implicit override +1
generatePdf() { ... } // added method
emailReport() { ... } // added method
scheduleReport() { ... } // added method
buildQuery() { ... } // added method
formatRows() { ... } // added method
}
// NOM=8, overrides=3 โ ratio=0.375 (borderline)
class UserCacheService extends BaseRepository<User> {
findById(id: string) { ... } // implicit override +1
// save/delete not implemented (not abstract-forced in TS)
warmCache() { ... }
invalidate() { ... }
getStats() { ... }
prefetchUser() { ... }
prefetchAll() { ... }
}
// NOM=6, overrides=1 โ ratio=0.167 < 0.33 ๐จ Refused Bequest
Remediation
- Replace inheritance with composition โ the subclass probably wants to use the parent, not be the parent.
- Extract interface โ if only partial behaviour is needed, implement a narrower interface instead of extending the full base class.
- Push down inheritance โ if some subclasses do use the parent, leave them; move the refusing subclass out of the hierarchy.
Related strategies
Related metrics
DIT ยท NOM / NOAM / NOOM ยท WMC