Edit on GitHub

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
TermDefinition
DIT > 0Class has at least one parent (is a subclass)
override_ratio < 0.33Less than a third of its methods override the parent
NOOMMethods with the explicit override keyword
implicit_overridesConcrete methods whose names match an abstract method in the parent
NOMTotal 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

Related strategies

God Class ยท Brain Method

Related metrics

DIT ยท NOM / NOAM / NOOM ยท WMC