Build Better: Embrace Composition Over Inheritance!
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][2]
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
- Ask "has-a": Does a Car have an Engine? Compose it in.
- Skip deep chains: Limit inheritance to 1-2 levels max.
- Delegate behaviors: Car calls Engine.start(), not overrides.
- Test combos: Mix pieces freely, verify they play nice.
- 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
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][4]
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
- Classify calls: Returns data? Query. Modifies state? Command.
- No overlap: Queries return values only—no saves.
- Name clearly: getBalance() queries; withdraw() commands.
- Chain safely: Queries feed commands without tricks.
- 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
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][6]
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
- Validate inputs: Check nulls, ranges at entry points.
- Assert boldly: Throw on invalid states—don't guess.
- Propagate up: Let exceptions bubble unless handled.
- Test aggressively: Fail tests on edge cases immediately.
- 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
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!