Software design and clean code

Stage 1 · Lesson 6 of 8

Composition over inheritance

Object-oriented programming properly

Composition means building a class by holding other objects and passing work to them, a relationship, instead of extending a parent. The book, which catalogued the classic , put it as "favour object composition over class ".

A Stack that has a list exposes only push and pop. A Notifier that has a list of channels can send by email and SMS without a class for every combination. Parts can be swapped at run time, tested on their own and reused in other classes. Most of the design patterns later in this roadmap are composition in a named shape.

Common trapturning the rule into "never inherit": a shallow hierarchy for a true is-a, such as UI widgets in a framework, is still fine.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • Free exercises in your language with optional human mentoring on design, not just on passing tests

Keep handy

  • Microsoft's C# guide: the object-oriented programming and records sections

Also worth your time

Mark this stage as not done?