Separation of concerns
medium 32 minGive each question a program must answer its own place, and let the places meet through small openings.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
Watch a sign-up as one handler, then through layers, then inside wrappers.
Operations
One handler does all
Six questions in one function.
Step 1 of 5
Start
Watching comes first, by hand.
Step 1: Start
def handle(request): log.write("start") if request.token != "t1": return 401 n = db.execute("select count(*) ...") if n >= 2: return 409 db.execute("insert ...") mail.send("Asha", "booked") log.write("end") return 200- Good for
- A first sketch
- Costs
- Parts spoil each other
Count questions, not lines.
In the real world
The same separation of concerns doing real jobs. Pick one and step through it.
Which places does each change open?
Step 1 of 4
Answer in XML too
| Edge | Rules | Storage | Wrappers | |
|---|---|---|---|---|
| XML too | opened | stays | stays | stays |
| new database | ||||
| log format | ||||
| waiting list |
A format. Only the edge is opened.
Step 1: Answer in XML too
Common mistakes
The bugs interviewers watch for, with the fix beside each.
The request object in the rules
criticalA job or a test must now build a web request.
Wrongdef join(self, request): wid = request.json["workshop"]Rightdef join(self, workshop_id, person): ...Storage that decides
majorAnother store would need the same rule again.
Wrongdef save(self, w): if len(w.people) > w.seats: returnRightafter = workshop.join(person) # decidesstore.save(after) # keepsLayers that only forward
minorMore files, nothing protected.
Wrongclass JoinManager: def join(self, *a): return self.service.join(*a)Rightpage -> SignUp -> WorkshopsCatching everything deep inside
majorA bug looks like a normal answer, and the outer wrapper never hears of it.
Wrongtry: store.save(w)except Exception: passRightstore.save(w) # safe() at the edge reports
Interview tips
Name the questions
- Show, decide, keep
- Guard, watch
- Then draw boxes
Say what crosses
- Plain values in
- Own errors out
- No JSON, no rows
Order the wrappers
- Log outside
- Sign-in before cache
- Retry around transaction
Do not overbuild
- A view function is enough
- No layer that forwards
- No store unless asked