Shelves / Software Architecture
SA·00
Start Here: Code That Is Cheap to Change
For readers who write working code but haven't thought much about its structure: what software design and architecture are for, one checkout function written two ways and then changed for a new requirement (with complexity measured and tests compared), the ideas the volume builds on (cohesion, coupling, dependencies pointing inward, decisions recorded), the vocabulary, and how to study it.
Start here: how to use this volume
Two reading paths
This volume is about structuring code and systems so they stay cheap to understand and change: clean code, the SOLID principles, design patterns, domain-driven design, architecture styles, modular monoliths, microservices, how architectural decisions are made and recorded, and low-level design problems. Every chapter has the same shape: a plain-language Start here section with small, verified examples; a How deep to go ladder; the full-depth sections; a Lab of real measurements; and a Revision sheet.
- New to software design? Read this chapter, then the chapters in order. The labs use Python, Go and Java with free tools: pytest, Hypothesis, radon and lizard for complexity, import-linter for dependency rules.
- Experienced? Skip this chapter. In each chapter read the revision sheet first, take its quiz, then use How deep to go to choose the sections and lab experiments to study.
Mastery levels
Every chapter uses the same six levels:
| Level | Name | You can… | Example for coupling |
|---|---|---|---|
| L1 | Recognition | define it and spot it | "this function knows about every payment provider" |
| L2 | Working | apply it in your code | inject a dependency instead of creating it inside |
| L3 | Internals | explain why it works | explain how dependency direction controls what a change touches |
| L4 | Debugging | find and fix problems | find the module that forces every change to ripple |
| L5 | Design | shape a whole system | draw module boundaries and dependency rules for a product |
| L6 | Production | run it at scale | set architecture standards and fitness functions for many teams |
What design is for
Software is changed far more often than it is written. Design (and, at a larger scale, architecture) is the set of decisions that decide how expensive those changes are: how much code you must read to understand a part, how many places a change touches, how confidently you can test it, and how many people must coordinate to ship it.
One checkout, written two ways
The tiffin centre's checkout adds up items, applies a member discount and takes payment by card, UPI or cash. Version A does it in one function:
def checkout(order, method, user):
total = 0
for item in order["items"]:
if item["type"] == "thali": ...
elif item["type"] == "drink": ... # third drink free
if user.get("member"): ... # 10% over ₹500, else 5%
if method == "card":
fee = total * 0.02; total += fee
print("charging card", user["card"], total) # stands in for the real gateway call
status = "paid"
elif method == "upi": ...
elif method == "cash": ...
return {"total": round(total, 2), "status": status}
Version B splits the same behaviour by responsibility: pricing functions (line_total, member_discount, order_total), one small class per payment method (Card, Upi, Cash), each given its gateway rather than calling it directly, and a short checkout that combines them. Money uses Decimal.
Measured with radon (cyclomatic complexity counts the independent paths through a function):
Version A: 1 function, complexity 11 (grade C)
Version B: 12 small blocks, average complexity 1.9 (grade A), largest 3
Then the requirement changes
The business adds wallet payments: refuse if the balance is too low, otherwise debit and credit 5% cashback.
Version A: +8 lines inside checkout(); its complexity rises 11 → 13; every payment method's code path is in the edited function
Version B: +7 lines, a new Wallet class; checkout.py, pricing.py, Card, Upi and Cash unchanged
The line counts are almost the same. The difference is where the lines went. In A, adding wallets meant editing the one function every checkout runs through, so a slip could break card payments too. In B, the new behaviour arrived as a new piece, and nothing that already worked was touched.
And testing it
Version B was tested with fake gateways and a fake ledger passed in:
test_card_adds_fee_and_charges_once test_member_discount
test_wallet_cashback test_wallet_insufficient
4 passed in 0.04s
test_card_adds_fee_and_charges_once asserts the gateway was called exactly once with ₹306.00. Version A can't be tested that way: the "payment" is a call buried inside the function (here a print, in real life an HTTP request), so a test either really charges a card or has to patch internals. Code that is easy to test is almost always code with its dependencies passed in, which is the same property that made the change cheap.
How to study software design
A routine that works
- Make the change you're asked for, then ask what made it hard: which files, which functions, which tests.
- Measure: complexity (radon, lizard), dependency graphs, change frequency per file (
git log --stat), and which files change together. - Refactor in small steps with tests green, so structure improves without changing behaviour.
- Learn each principle and pattern with its trade-off: when it helps and when it's ceremony.
- Write down decisions (ADRs) so the next person knows why the structure is the way it is.
Warm-up exercises
- Run
radon cc -s -a(orlizard) on a project you work on and look at its five most complex functions. - Find the file in your project that
git logshows changing most often. Why does it change so much? - Pick one function that calls a database or HTTP API directly and pass that dependency in instead; write one test with a fake.
- Add a fourth payment method to Version B of the checkout and its test.
Glossary
- Cohesion: how closely the parts of a module belong together.
- Coupling: how much one module depends on another's details.
- Cyclomatic complexity: number of independent paths through code.
- Dependency injection: passing collaborators in rather than creating them inside.
- Refactoring: changing structure without changing behaviour.
- Architecture: the decisions that are expensive to change later.
- ADR: architecture decision record.