# Build Better: Embrace Composition Over Inheritance!

Newsy: Dev Concepts Weekly
Published: 2026-04-20T13:00:00.216Z
Source: https://newsyai.com/edition/1fe283ab-e63d-4800-9872-c21d82a198d2
Archive: https://newsyai.com/newsy/249754d5-9f20-4825-9aeb-daeec7c365c9.md

---

# The Pros Know the Fundamentals Better!
**Weekly Edition #53**

Three high-impact topics (about 90 seconds each) to compound your growth. Plain English, vivid metaphors, practical moves.

---

## 1) Composition over Inheritance: Lego Bricks over Family Trees
Build flexible software by combining objects like Lego pieces ("has-a" relationships) instead of stacking rigid family hierarchies ("is-a"). Favor composition for reusable, adaptable designs that evolve without breaking everything.[[1]](https://medium.com/@Masoncoding/composition-over-inheritance-a-design-principle-for-improving-software-architecture-c55fefaba465)[[2]](https://dev.to/lovestaco/composition-over-inheritance-a-flexible-design-principle-4ehh)

**Why this compounds**  
- Swappable parts: Change one brick without toppling the tower.  
- Fewer surprises: No deep inheritance chains hiding bugs.  
- Team speed: Easier to understand and tweak assemblies.

**Metaphor**  
Family tree trap: Inherit your grandpa's old car? Stuck with the squeaky brakes forever! Lego way: Snap on new wheels anytime—build a truck or plane on the fly.

**How to do it**  
1. Ask "has-a": Does a Car *have* an Engine? Compose it in.  
2. Skip deep chains: Limit inheritance to 1-2 levels max.  
3. Delegate behaviors: Car calls Engine.start(), not overrides.  
4. Test combos: Mix pieces freely, verify they play nice.  
5. Refactor forward: Spot rigid "is-a"? Switch to "has-a."

**Try this today**  
Find a class inheriting multiple behaviors. Extract to composable objects, wire them up. Run tests—feel the freedom!

**Further reading**  
- [Medium: Composition Principle](https://medium.com/@Masoncoding/composition-over-inheritance-a-design-principle-for-improving-software-architecture-c55fefaba465)  
- [Dev.to: Flexible Design](https://dev.to/lovestaco/composition-over-inheritance-a-flexible-design-principle-4ehh)  
- [Medium: System Design](https://medium.com/@debby1ee/composition-over-inheritance-in-system-design-f9f714626931)

---

## 2) Command-Query Separation: Do or Ask—Never Both
Keep methods pure: Either *change* something (command) or *fetch* info (query), but split them to avoid sneaky side effects and confusing code.[[3]](https://dev.to/wallacefreitas/command-query-separation-cqs-a-guide-to-better-software-design-2jab)[[4]](https://khalilstemmler.com/articles/oop-design-principles/command-query-separation)

**Why this compounds**  
- Predictable flow: Know if a call tweaks data or just peeks.  
- Fewer bugs: No hidden changes during "harmless" reads.  
- Debug ease: Trace commands separately from queries.

**Metaphor**  
Swiss Army knife gone wrong: One tool that cuts *and* measures? Slip and oops! Better: Dedicated knife for slicing, ruler for sizing—grab the right one every time.

**How to do it**  
1. Classify calls: Returns data? Query. Modifies state? Command.  
2. No overlap: Queries return values only—no saves.  
3. Name clearly: getBalance() queries; withdraw() commands.  
4. Chain safely: Queries feed commands without tricks.  
5. Review ruthlessly: Mixed method? Split it now.

**Try this today**  
Spot a method that both reads and writes (like "processOrder()"). Break into queryOrderDetails() and submitOrder(). Cleaner magic!

**Further reading**  
- [Dev.to: CQS Guide](https://dev.to/wallacefreitas/command-query-separation-cqs-a-guide-to-better-software-design-2jab)  
- [Khalil Stemmler: OOP Principle](https://khalilstemmler.com/articles/oop-design-principles/command-query-separation)  
- [Martin Fowler: CommandQuerySeparation](https://martinfowler.com/bliki/CommandQuerySeparation.html)

---

## 3) Fail Fast: Scream Early, Fix Quick
Spot problems right away—halt and alert instead of limping on. This catches issues upstream, saving hours of downstream pain in development and runtime.[[5]](https://medium.com/@christian.ppl/introduction-to-the-fail-fast-principle-in-software-development-865ccab28979)[[6]](https://www.testmuai.com/learning-hub/fail-fast)

**Why this compounds**  
- Time saver: Bad data dies young, not in production.  
- Clear signals: Obvious errors pinpoint fixes fast.  
- Robust systems: Forces solid inputs from the start.

**Metaphor**  
Cooking flop: Taste the milk *before* baking the cake? Sour? Trash it quick! Pour anyway? Ruined oven and tears. Early check = happy dinner.

**How to do it**  
1. Validate inputs: Check nulls, ranges at entry points.  
2. Assert boldly: Throw on invalid states—don't guess.  
3. Propagate up: Let exceptions bubble unless handled.  
4. Test aggressively: Fail tests on edge cases immediately.  
5. Monitor loud: Logs scream on first whiff of trouble.

**Try this today**  
Add upfront checks to a key function (nulls, bad formats). Watch it fail a test early—fix the root, not symptoms!

**Further reading**  
- [Medium: Fail Fast Intro](https://medium.com/@christian.ppl/introduction-to-the-fail-fast-principle-in-software-development-865ccab28979)  
- [TestMu: Fail Fast in Dev](https://www.testmuai.com/learning-hub/fail-fast)  
- [Enterprise Craftsmanship: Fail Fast](https://enterprisecraftsmanship.com/posts/fail-fast-principle)

---

### Quick Recap
- **Composition**: Lego "has-a" for flexible builds.  
- **CQS**: Do *or* ask—pick your tool.  
- **Fail Fast**: Yell early, heal quicker.

Keep stacking wins: These strategies turn brittle code into bendy powerhouses. Pros build to last *and* adapt!

---
