65
composition over inheritance
Has-a, not is-a. Assemble small pieces. Do not grow a base class the model keeps subclassing.
What is composition over inheritance?
Composition builds behaviour by holding other objects. Inheritance builds it by subclassing. Deep hierarchies share code and also share surprises. Composition shares only what you pass in.
Why does composition over inheritance matter when vibe coding?
Models reach for `BaseService` and `AbstractRepository` on turn one. By turn five you have a diamond and no one knows which `save()` runs. Say composition or you inherit a framework you did not order.
How do you do composition over inheritance?
Ban new base classes unless an existing one is already the pattern. Prefer a function or a small collaborator. If two classes share ten lines, extract a function, not a parent.
How do you ask a model for composition over inheritance?
Use composition, not inheritance. Do not add a base class. Share behaviour with a function or an injected collaborator.
What goes wrong with composition over inheritance?
A “mixin” pile that is inheritance with extra steps. If you cannot construct the piece alone, it is not composition.