AlgoViz

Separation of concerns

medium 32 min

Give 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

0:00 / 4:04

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

handlerSignUpHandlerquestions6dbSqlConnectionrows1mailMailClientsent0logLogFilelines1write(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

EdgeRulesStorageWrappers
XML tooopenedstaysstaysstays
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

    critical

    A job or a test must now build a web request.

    Wrong
    def join(self, request):    wid = request.json["workshop"]
    Right
    def join(self, workshop_id, person):    ...
  • Storage that decides

    major

    Another store would need the same rule again.

    Wrong
    def save(self, w):    if len(w.people) > w.seats: return
    Right
    after = workshop.join(person)   # decidesstore.save(after)               # keeps
  • Layers that only forward

    minor

    More files, nothing protected.

    Wrong
    class JoinManager:    def join(self, *a):        return self.service.join(*a)
    Right
    page -> SignUp -> Workshops
  • Catching everything deep inside

    major

    A bug looks like a normal answer, and the outer wrapper never hears of it.

    Wrong
    try: store.save(w)except Exception: pass
    Right
    store.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