On this page

Design Principles

Module 1 was about deciding which classes exist. This module is about deciding how they should be shaped: what each one is responsible for, how they depend on each other, what they expose, how they fail, and how much design is enough. These are the judgments an LLD interviewer is really scoring when they read your class diagram, and they're what decides whether the "now add X" extension question takes two minutes or twenty.

The module has two parts. The first is SOLID, the five principles most interviewers will name. The second is the set of principles that sit around SOLID and fill its gaps. SOLID tells you where boundaries go, but it says little about when to stop abstracting, how to handle errors, or what makes a class pleasant to call. Each of the newer pages has runnable before-and-after code in Java, Python and TypeScript, a section on when the principle goes too far, and the interview questions that probe it.

#LessonWhat you'll get from it
1SOLID Principles: overviewThe five principles at a glance and how they interrelate
2Single ResponsibilitySplitting classes by reason to change
3Open/ClosedAdding behaviour without editing stable code
4Liskov SubstitutionWhat a subtype must promise, and the rectangle-square trap
5Interface SegregationRole interfaces instead of fat ones
6Dependency InversionDepending on abstractions, and the composition root
7DRY, KISS & YAGNI — and When They ConflictTrue versus incidental duplication, the wrong abstraction, and when a seam is justified
8Composition over InheritanceThe fragile base class, delegation, strategies, mixins, and when inheritance is right
9Cohesion, Coupling & the Law of DemeterKinds of coupling, connascence, train wrecks and tell-don't-ask
10Designing for TestabilitySeams, injected clocks and randomness, test doubles, and a pure core with an imperative shell
11Immutability, Value Objects & Defensive CopyingRecords, frozen dataclasses and readonly, equality contracts, copies versus views
12Error Handling in Object DesignInvariants, fail fast, exceptions versus result types, validation at boundaries, null handling
13Interfaces & API Design at Class LevelNaming, minimal surface, builders, static factories, fluent APIs, CQS and evolving APIs

Plan on roughly 20 to 30 minutes per new lesson if you read the code, longer if you run it.

Why this order

The order follows the dependencies between the ideas, not their fame.

  • SOLID comes first because the vocabulary is everywhere, and the later pages build on it. They link back rather than repeat it.
  • DRY, KISS and YAGNI come next because they're the dials on everything else. They decide how much of SOLID to apply in a given situation. Reading them early stops the rest of the module from turning into "add an interface everywhere."
  • Composition over inheritance follows Liskov because it's largely a practical consequence of it: inheritance promises substitutability, so when you can't keep that promise, compose.
  • Cohesion and coupling generalise Single Responsibility and Interface Segregation, and add a finer vocabulary (connascence) and the Law of Demeter.
  • Testability builds on Dependency Inversion and coupling. A seam is an inverted dependency, and hard-to-test code is almost always over-coupled code.
  • Immutability, error handling and API design are about the inside and the surface of a single class: what it guarantees, how it fails, and how it's called. They come last because they draw on everything before them. For example, a good API uses value objects, fails fast and keeps commands and queries apart.
flowchart LR
    S["SOLID"] --> D["DRY, KISS, YAGNI"]
    S --> C["Composition over inheritance"]
    S --> K["Cohesion and coupling"]
    K --> T["Testability"]
    S --> T
    K --> I["Immutability and value objects"]
    I --> E["Error handling"]
    I --> A["Class-level API design"]
    E --> A
    D --> A

How the module shows up in interviews

Interviewers rarely ask "define the Law of Demeter." They read your design and ask questions that test whether you applied these principles on purpose:

  • "Why is this an interface?" tests YAGNI and Open/Closed.
  • "Why not just extend Vehicle?" tests composition over inheritance.
  • "How would you test this?" tests testability, and usually exposes a hidden now() or a hard-wired dependency.
  • "What happens if payment fails halfway?" tests error handling and invariants.
  • "Can a caller break this through getSpots()?" tests minimal surface and defensive copying.

Every page ends with a section on exactly these moments and ten interview questions with model answers. The Common LLD Mistakes page in Module 4 is a good companion. Most of its ten mistakes are one of these principles ignored or overdone.

Before and after this module

Before: you should be comfortable with classes, interfaces and inheritance from Object-Oriented Programming, and with entities and value objects from From Nouns to Classes.

After: Module 3 covers design patterns one at a time. Many patterns are these principles packaged for a recurring problem. Strategy is composition plus Open/Closed, Decorator is delegation, Builder is API design for complex construction, and Null Object is error handling without null checks. If you know why the principles matter, you'll know when each pattern earns its place and when it doesn't.

Running the code

The new pages' samples are complete programs, not fragments. Each was run with Java 24, Python 3.12 and TypeScript 5.8 (compiled with tsc --strict, then executed with tsx), and each checks its own claims with assertions. The Java samples use a small check helper rather than assert, so they fail loudly even without the -ea flag. Copy any tab into a file and run it to see the "before" problem and the "after" fix for yourself.