๐Ÿ›๏ธ
๐Ÿ›๏ธ Software Design & Architecture ยท ARCH-101 ยท Session 5 of 6

Composing Behaviour & Reversibility

Two more powerful ideas: build complex behaviour by composing simple pieces, and design actions to be reversible. Both make systems more flexible, understandable, and forgiving.

โฑ 9 min read๐Ÿ“… Updated 2026-08-06๐Ÿ“Š Tables, charts & examples

Two more design ideas round out your toolkit, both about flexibility. First, composition: building complex behaviour by combining small, simple pieces rather than writing one big monolith - the same 'small parts, combined' theme you've seen throughout, applied as a design strategy. Second, reversibility: designing actions so they can be undone, which makes systems more forgiving and enables features like undo/redo. This session covers composing behaviour and making actions reversible.

Compose small pieces

Prefer many small, focused, combinable pieces over one large one. Complex behaviour then emerges from arranging simple parts - which are each easy to understand, test, and reuse.

๐ŸŽฏ
Build big behaviour by composing small pieces, not by writing one giant thing. A pipeline of small transformations (clean, then parse, then validate, then format) is easier to understand, test and rearrange than one enormous function doing it all. Small composable pieces can be combined in different ways for different needs, reused across the app, and tested individually. This 'composition over monolith' instinct - favouring small parts you assemble - runs through all good design, from functions calling functions to whole systems built of focused components. Complexity you compose is manageable; complexity in one blob is not.

Design for reversibility

Wherever practical, design actions so they can be undone. Reversible actions make software forgiving (mistakes aren't permanent) and enable powerful features.

from larzcommand import History, command

# each action knows how to do AND undo itself
hist = History()
hist.do(command(lambda: doc.append(x), lambda: doc.pop()))
hist.undo()   # take it back
hist.redo()   # and forward again
Reversible design gives you...
Undo / redoThe trick: actions that can undo themselves
Forgiving softwareMistakes aren't permanent
Safer operationsTry something, revert if wrong
AuditabilityA history of what happened

The <b>command pattern</b> (via <a href='/stack/larzcommand'>larzcommand</a>) captures each action as something that can do and undo itself, kept in a history. Beyond literal undo buttons, thinking about reversibility makes you design safer, more forgiving operations - a mark of thoughtful software. Not everything can be reversible, but a lot more can than you'd first think.

Flexible and forgiving by design

  • Compose small pieces - build complex behaviour by combining simple, focused parts.
  • Small parts are easier to understand, test, reuse and rearrange than one monolith.
  • Design for reversibility - actions that can undo themselves make software forgiving.
  • The command pattern (larzcommand) captures do/undo in a history - undo/redo for free.

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • Composition: build complex behaviour by combining small, focused pieces rather than one giant monolith.
  • Small composable parts are each easy to understand, test, reuse and rearrange - complexity you compose is manageable.
  • Reversibility: design actions so they can be undone, making software forgiving and enabling undo/redo.
  • The command pattern (larzcommand) captures each action's do and undo in a history - powering undo/redo and safer operations.

Frequently asked

What does 'composition over inheritance' mean - I've heard it's important?

It's a well-known design guideline that captures the composition idea in the context of object-oriented code, and it recommends building objects' capabilities by combining (composing) smaller pieces rather than by inheriting from big class hierarchies - because composition tends to be more flexible and less tangled. To unpack it: inheritance is when a class is built by extending another class, taking on its behaviour (a Dog class inheriting from an Animal class); it's a way to reuse and share behaviour, and it has its place, but deep or complex inheritance hierarchies cause problems - a class inherits everything from its parents whether it wants it or not, changes to a base class ripple to everything below it (tight coupling), and trying to model varied combinations of behaviours through a single inheritance tree gets rigid and awkward (you can't easily mix and match capabilities). Composition is the alternative: instead of a class BEING a kind of something via inheritance, it HAS pieces that provide capabilities - you build up an object's behaviour by giving it smaller focused components/objects that each do one thing, and combining them. This is more flexible because you can mix and match the pieces freely (an object can be given exactly the capabilities it needs by composing the relevant parts), changes to one component don't ripple through an inheritance tree, and each piece stays small and focused. The guideline 'favour composition over inheritance' means: when deciding how to reuse and combine behaviour, lean toward composing small pieces rather than building tall inheritance hierarchies, because composition generally keeps things more flexible, loosely coupled, and changeable. It's the same 'small combinable pieces' theme from this session, applied to how you structure objects and their capabilities. It doesn't mean inheritance is always wrong - inheritance is genuinely useful for real 'is-a' relationships and simple, shallow hierarchies, and has its uses - it means don't reach for deep inheritance as your default tool for sharing and combining behaviour, because it often leads to rigid, entangled designs, whereas composing focused pieces stays more adaptable. For a beginner, the practical takeaway is: prefer building things out of small, combinable parts (composition) over elaborate class hierarchies (inheritance) when you have the choice, because composed designs are usually easier to change, test, and understand - which is exactly the composition-over-monolith instinct at the object level. You don't need to master the deep object-oriented theory now; just carry the bias toward assembling small pieces rather than building tall inheritance trees, and you'll tend toward more flexible designs. As you encounter object-oriented code more, the specific reasons (the coupling and rigidity of deep inheritance versus the flexibility of composition) will become concrete through experience. It's a widely-repeated guideline precisely because favouring composition leads to the flexible, changeable designs that good architecture is about.

How can I make actions reversible - surely a lot of things can't be undone?

You're right that not everything can be undone, and you shouldn't force reversibility where it doesn't fit - but more can be made reversible than beginners assume, and even where literal undo isn't possible, thinking about reversibility leads to safer, more forgiving design. First, how to make actions reversible when you can: the command pattern is the key technique - instead of just performing an action directly, you capture it as a command that knows both how to do the action and how to undo it (the inverse), and you keep a history of executed commands. To undo, you call the undo of the most recent command; to redo, you re-do it. For an action to be reversible this way, you need to be able to define its inverse - what change reverses it. Many in-app actions have natural inverses: adding an item undoes by removing it, deleting undoes by restoring it (if you kept what was deleted), changing a value undoes by restoring the old value (so you save the previous state), moving undoes by moving back. The pattern is to capture enough information (the old value, the removed item) to reverse the change, which is often straightforward for operations on your app's own data. So editing operations, state changes, and manipulations of in-memory or stored data are frequently reversible with the command pattern, which is why undo/redo is common in editors, design tools, and many apps. Now, the genuine limits: some actions can't be truly undone because they have external, irreversible effects - once you've sent an email, charged a payment, launched a rocket, or told an external system something, you can't un-send or un-charge it (you can only compensate: send a correction, issue a refund - which isn't undoing but doing a counteracting action). Actions that lose information (if you didn't save what was deleted, you can't restore it) or that interact with the outside world in one-way fashions resist true reversal. For those, forcing an undo mechanism is inappropriate. But here's the valuable part even when you can't fully undo: thinking about reversibility improves design regardless. It pushes you toward safer operations - preferring actions that can be reversed or that ask for confirmation before irreversible steps; keeping information (like what was deleted) so recovery is possible; separating the reversible parts from the irreversible commitment (do everything undoable first, commit the irreversible action last, so a mistake before the final step costs nothing); and providing 'soft' reversibility like marking something deleted rather than truly erasing it, so it can be restored. Even 'compensating actions' (a refund for a charge) are a form of reversibility-thinking applied where true undo is impossible. So the guidance is: make actions genuinely reversible where you can, using the command pattern to capture do/undo (which covers a lot of in-app data operations and gives you undo/redo), and where actions are inherently irreversible (external effects, lost information), don't force it but do apply reversibility-thinking to make operations safer and more forgiving - confirmations before irreversible steps, keeping recovery information, deferring the point of no return, and compensating actions. Not everything can be undone, correctly, but designing with reversibility in mind - making the reversible things reversible and handling the irreversible things carefully - is a mark of thoughtful, forgiving software. Reach for the command pattern for the reversible cases (it's more of your app than you'd think), and let reversibility-thinking guide safer handling of the rest.

Session 5 of 6 complete

Next up: Designing for Change (Without Over-Engineering)

Continue to Session 6 โ†’

Related lessons

More in this course

Course home → · Browse all courses →