Skip to content

Commit 7bb56d3

Browse files
dennisdoomenDennis DoomenCopilotbkoelman
authored
Add AV0110: Prefer composition over class inheritance (#302)
* Add AV0110 guideline Split from #298. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update _rules/0110.md Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com> --------- Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>
1 parent 6899e40 commit 7bb56d3

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

_rules/0110.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
rule_id: 0110
3+
rule_category: general
4+
title: Prefer composition over inheritance
5+
severity: 2
6+
---
7+
Inheritance creates a tight coupling between a base class and its derived classes. Any change to the base class can unexpectedly break derived classes, and the inheritance hierarchy can become difficult to understand as it grows. Composition — building behavior by combining small, focused objects or interfaces — avoids these problems.
8+
9+
Prefer composition when:
10+
- The relationship is "has-a" rather than "is-a".
11+
- You want to reuse behavior without exposing or depending on the internals of another class.
12+
- You need to vary behavior at runtime (e.g. through the [Strategy pattern](https://en.wikipedia.org/wiki/Strategy_pattern)).
13+
14+
**Exception:** Inheritance is appropriate when a true "is-a" relationship exists and when derived classes genuinely extend (rather than replace) the behavior of their base class. Always follow the [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) when using inheritance.

0 commit comments

Comments
 (0)